package com.philisense.rdgo.ui.putOut;

import android.app.Activity;
import android.app.AlertDialog;
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.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;

import com.philisense.rdgo.R;
import com.philisense.rdgo.adapter.distributionAdapter;
import com.philisense.rdgo.bean.response.OrderDetailBean;
import com.philisense.rdgo.databinding.ActivityPutOutBinding;
import com.philisense.rdgo.http.cache.ACache;
import com.scandecode.ScanDecode;
import com.scandecode.inf.ScanInterface;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PutOutActivity extends AppCompatActivity {

    ActivityPutOutBinding mBinding;
    private ScanDecode scanDecode;
    ACache aCache;
    private List<OrderDetailBean.DataBean> distributions;
    private distributionAdapter adapter;
    private static int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_put_out);
        Toolbar toolbar = mBinding.include.toolbar;
        toolbar.setTitle("扫码出库");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                finish();
            }
        });

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

        scanDecode.getBarCode(new ScanInterface.OnScanListener() {
            @Override
            public void getBarcode(String s) {

                scanDecode.onDestroy();
                scanDecode=null;
                PutOutOrderActivity.start(PutOutActivity.this, s);

         //       finish();
            }
        });

        mBinding.distribution.setPullRefreshEnabled(false);

        mBinding.determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s=mBinding.edit.getText().toString().trim();
                PutOutOrderActivity.start(PutOutActivity.this, s);
                scanDecode.onDestroy();

                scanDecode=null;
            //   finish();
            }
        });

        aCache = ACache.get(PutOutActivity.this);

        distributions=(List<OrderDetailBean.DataBean>)aCache.getAsObject("distributions");
        if(distributions==null){
            distributions=new ArrayList<>();
        }

        mBinding.distribution.setLayoutManager(new LinearLayoutManager(this));
        adapter = new distributionAdapter(this);
        mBinding.distribution.setAdapter(adapter);
        adapter.setData(distributions);

        adapter.setOnClickListener(new distributionAdapter.OnItemClickListener() {
            @Override
            public void onClick(String orderId) {
                PutOutOrderActivity.start(PutOutActivity.this, orderId);
                scanDecode.onDestroy();

               scanDecode=null;
              //  finish();
            }

            @Override
            public void delte(int postion) {

                new AlertDialog.Builder(PutOutActivity.this).setTitle("确认要删除吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        distributions.remove(postion);
                        adapter.setData(distributions);
                        aCache.put("distributions", (Serializable) distributions);
                    }
                })
                .setNegativeButton("取消", null)
                .show();

            }
        });



    }

    public static void start(Activity context) {
        Intent intent = new Intent(context, PutOutActivity.class);
        ActivityCompat.startActivity(context, intent,null);
    }


    @Override
    protected void onResume() {

        if (scanDecode==null){
            try {
                scanDecode = new ScanDecode(this);
                scanDecode.initService("true");
            }catch (Exception e) {
                Snackbar.make(mBinding.putOutLayout,"本机没有扫码功能",Snackbar.LENGTH_LONG).show();
            }

            scanDecode.getBarCode(new ScanInterface.OnScanListener() {
                @Override
                public void getBarcode(String s) {

                    scanDecode.onDestroy();
                    scanDecode=null;
                    PutOutOrderActivity.start(PutOutActivity.this, s);


                }
            });

            mBinding.edit.setText("");
            distributions=(List<OrderDetailBean.DataBean>)aCache.getAsObject("distributions");
            if(distributions==null){
                distributions=new ArrayList<>();
            }
            adapter.setData(distributions);
        }

        super.onResume();
    }





    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            scanDecode.onDestroy();
        }catch (Exception e) {
        }
    }



}
