package com.philisense.rdgo.ui.Reset;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.xrecyclerview.XRecyclerView;
import com.philisense.rdgo.R;
import com.philisense.rdgo.adapter.QuareAdapter;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.response.PileInfoBean;
import com.philisense.rdgo.databinding.ActivityResetBinding;
import com.philisense.rdgo.http.RequestImpl;
import com.philisense.rdgo.viewModel.ResetViewModel;
import com.scandecode.ScanDecode;
import com.scandecode.inf.ScanInterface;

public class ResetActivity extends AppCompatActivity {

    public ActivityResetBinding mBinding;
    private ScanDecode scanDecode;
    private ResetViewModel viewModel;

    private QuareAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_reset);
        Toolbar toolbar = mBinding.include.toolbar;
        toolbar.setTitle("重置");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                finish();
            }
        });
        mBinding.boxRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new QuareAdapter(this);
        mBinding.boxRecyclerView.setAdapter(adapter);
        viewModel = new ResetViewModel(this);
        mBinding.boxRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mBinding.boxRecyclerView.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                mBinding.boxRecyclerView.refreshComplete();
            }
        });

        // 初始化扫描
        try {
            scanDecode = new ScanDecode(this);
            scanDecode.initService("true");
        } catch (Exception e) {
            Snackbar.make(mBinding.lineInRoot, "本机没有扫码功能", Snackbar.LENGTH_LONG).show();
        }

        scanDecode.getBarCode(new ScanInterface.OnScanListener() {
            @Override
            public void getBarcode(String s) {
                scanDecode.stopScan();
                if (!"".equals(s)) {
                    get(s);
                }

            }
        });

        mBinding.sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                queding();
            }
        });
    }


    public void get(String boxId) {
        viewModel.getPileInfo(boxId, new RequestImpl<PileInfoBean>() {
            @Override
            public void loadSuccess(PileInfoBean bean) {
                if (bean != null&&bean.getData().getStatus()==0) {
                    adapter.setData(bean.getData().getBoxIn());
                    mBinding.boxRecyclerView.refreshComplete();
                    mBinding.boxId.setText(boxId);
                }else{
                    Snackbar.make(mBinding.lineInRoot, "该垛已出库,无法重置!", Snackbar.LENGTH_LONG).show();

                }
            }

            @Override
            public void loadFailed(OError error) {


                Snackbar.make(mBinding.lineInRoot, error.getErrorMsg(), Snackbar.LENGTH_LONG).show();

            }
        });


    }

    private void queding(){
        new AlertDialog.Builder(this).setTitle("是否重置？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        res();
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    public void res() {
        String username = mBinding.account.getText().toString().trim();
        String pass = mBinding.password.getText().toString().trim();
        if ("".equals(username) || "".equals(pass)) {
            Snackbar.make(mBinding.lineInRoot, "请输入账号和密码！", Snackbar.LENGTH_LONG).show();

            return;
        }
        String boxId = mBinding.boxId.getText().toString().trim();
        if ("".equals(boxId)) {
            Snackbar.make(mBinding.lineInRoot, "请扫描箱码！", Snackbar.LENGTH_LONG).show();
            return;
        }
        final ProgressDialog dialog = ProgressDialog.show(ResetActivity.this, "提示", "正在录入系统..");
        viewModel.reset(username, pass, boxId, new RequestImpl<String>() {
            @Override
            public void loadSuccess(String bean) {
                dialog.dismiss();
                if (bean != null) {
                    finish();
                    Toast.makeText(ResetActivity.this, "重置成功！", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void loadFailed(OError error) {
                dialog.dismiss();
                Snackbar.make(mBinding.lineInRoot, error.getErrorMsg(), Snackbar.LENGTH_LONG).show();

            }
        });


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            scanDecode.onDestroy();

        } catch (Exception e) {
        }
    }

    /**
     * @param context activity
     */
    public static void start(Activity context) {
        Intent intent = new Intent(context, ResetActivity.class);
        ActivityCompat.startActivity(context, intent, null);
    }


}
