package com.naz.sdkdemo.activity;

import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.naz.sdkdemo.R;
import com.naz.sdkdemo.adaapter.UpTrademarkAdapter;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.base.Callback;
import com.naz.sdkdemo.base.CallbackNew;
import com.naz.sdkdemo.bean.OperateResult;
import com.naz.sdkdemo.bean.StockInfo;
import com.naz.sdkdemo.helper.SerialIPManager;
import com.naz.sdkdemo.helper.SerialPortManager;
import com.naz.sdkdemo.http.ApiHelper;
import com.naz.sdkdemo.utils.MyUtils;
import com.naz.sdkdemo.utils.SerialUtil;
import com.naz.sdkdemo.weight.GridSpaceItemDecoration;
import com.naz.sdkdemo.weight.UserCache;
import com.naz.sdkdemo.weight.XToast;

import java.util.ArrayList;
import java.util.List;

public class UpTrademarkActivity extends BaseActivity {
   private RecyclerView rvContent;
   private UpTrademarkAdapter mAdapter;
   private List<StockInfo> mList;
   private int mPreSelectPosition = -1;

   @Override
   protected int getLayoutResId() {
      return R.layout.activity_up_trademark;
   }

   @Override
   protected void initView() {
      rvContent = findViewById(R.id.rv_content);
      rvContent.setLayoutManager(new GridLayoutManager(this, 2));
      GridSpaceItemDecoration itemDecoration = new GridSpaceItemDecoration(MyUtils.dp2px(this, 12), true);
      itemDecoration.setEndFromSize(0);
      rvContent.addItemDecoration(itemDecoration);

      mAdapter = new UpTrademarkAdapter(mList);
      mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
         @Override
         public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
            baseQuickAdapter.notifyItemChanged(i, "select");
            if (mPreSelectPosition != -1) {
               StockInfo info = (StockInfo)baseQuickAdapter.getItem(mPreSelectPosition);
               info.select = false;
               baseQuickAdapter.notifyItemChanged(mPreSelectPosition, "unselect");
            }
            mPreSelectPosition = i;
         }
      });
      rvContent.setAdapter(mAdapter);
      findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            finish();
         }
      });
      findViewById(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            next();
         }
      });
      requestStorehouseList();
   }

   private void requestStorehouseList() {
      ApiHelper.getStorehouseList(this, new CallbackNew<List<StockInfo>>() {
         @Override
         public void onResult(List<StockInfo> storehouseInfos) {
            if (null == mList) {
               mList = new ArrayList<>();
            }
            mList.clear();
            if (null != storehouseInfos) {
               mList.addAll(storehouseInfos);
            }
            mAdapter.setNewData(mList);
         }
      });
   }

   StockInfo info;
   private void next() {
      if (mPreSelectPosition == -1) {
         XToast.showToast("请选择");
         return;
      }
      info = mAdapter.getItem(mPreSelectPosition);
//      findViewById(R.id.btn_sure).setEnabled(false);
      ApiHelper.operateApply(this, 2, new CallbackNew<OperateResult>() {
         @Override
         public void onResult(OperateResult operateResult) {
            openDoor();
//            loopDoor();
         }
      });
   }

   private void loopDoor(int number, int type) {
      ApiHelper.loopMonitionOpen(this, type, new Callback() {
         @Override
         public void onSuccess(int code, String msg, Object data) {
            serialPortManager.openDoor(number, 1);
         }
      });
   }

   private  SerialPortManager serialPortManager;
   int value = 0;
   private void openDoor() {
      value = 0;
      if (serialPortManager == null){
         serialPortManager = new SerialPortManager();
         serialPortManager.init(UpTrademarkActivity.this, UserCache.getSerialName(), UserCache.getSerialRate(), new SerialPortManager.SerialCallBack() {
            @Override
            public void dataCallBack(String data) {
               Log.e("--------->","------->data="+data+", value="+value);
               byte[] aar = SerialUtil.hexStringToByteArray(data);
               if (aar!=null && aar.length>1){
                  if (aar[1] == (byte) 0x06){
                     if (value>=1){
                        startNext();
                        return;
                     }
                     value++;
                     loopDoor(3, 1);
                  }
               }
            }

            @Override
            public void openCallBack(int code) {
               if (code == 1){
                  loopDoor(1, 0);
               }else {
                  Toast.makeText(UpTrademarkActivity.this, "串口未打开", Toast.LENGTH_SHORT).show();
               }
            }

            @Override
            public void closeCallBack(int code) {

            }
         });
      }else {
         ApiHelper.cancelLoop();
         loopDoor(1, 0);
      }
   }

   private void startNext() {
      ApiHelper.operateRecord(this, 1, new CallbackNew<OperateResult>() {
         @Override
         public void onResult(OperateResult operateResult) {
            Intent intent = new Intent(UpTrademarkActivity.this, UpTrademarkScanActivity.class);
            intent.putExtra("info", info);
            startActivity(intent);
            finish();
         }
      });
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
      if (serialPortManager!= null){
         serialPortManager.close();
      }
      ApiHelper.cancelLoop();
   }
}
