package com.naz.sdkdemo.activity;

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.naz.sdkdemo.R;
import com.naz.sdkdemo.adaapter.FullApplyTrademarkScanAdapter;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.base.CallbackNew;
import com.naz.sdkdemo.bean.OperateResult;
import com.naz.sdkdemo.bean.ScanInfo;
import com.naz.sdkdemo.bean.TrademarkInfo;
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 java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class FullApplyTrademarkScanActivity extends BaseActivity {
   private RecyclerView rvContent;
   private FullApplyTrademarkScanAdapter mAdapter;
   private List<ScanInfo> mList = new ArrayList<>();
   private List<String> qrCodeList = new ArrayList<>();
   private SerialIPManager serialIPManager;
   private TrademarkInfo mSelectInfo;
   @Override
   protected int getLayoutResId() {
      return R.layout.activity_full_apply_trademark_scan;
   }

   @Override
   protected void initView() {
      mSelectInfo = (TrademarkInfo)getIntent().getSerializableExtra("info");
      rvContent = findViewById(R.id.rv_content);
      mAdapter = new FullApplyTrademarkScanAdapter(mList);
      rvContent.setAdapter(mAdapter);
      rvContent.setLayoutManager(new GridLayoutManager(this, 2));
//      rvContent.addItemDecoration(new GridSpaceItemDecoration(MyUtils.dp2px(this, 12)));
      scanInfo();
//      getDoorState();
      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) {
            findViewById(R.id.btn_sure).setClickable(false);
            Toast.makeText(FullApplyTrademarkScanActivity.this, "确认中，请稍后", Toast.LENGTH_SHORT).show();
            new Handler().postDelayed(new Runnable() {
               @Override
               public void run() {
                  finish();
               }
            }, 10*1000);
         }
      });
   }

   private void scanInfo() {
      serialIPManager = new SerialIPManager();
      serialIPManager.init();
      serialIPManager.connect(UserCache.getIP(), UserCache.getPort()+12, new SerialIPManager.ConnectCallBack() {
         @Override
         public void connectResult(int code) {
            if (code == 0){
               Toast.makeText(FullApplyTrademarkScanActivity.this, "请确认串口服务器正常开启", Toast.LENGTH_SHORT).show();
            }
         }

         @Override
         public void receiveData(String data) {
            Log.e("--------->","-------->data="+data);
            if (qrCodeList.contains(data))
               return;
            qrCodeList.add(data);
            getInfo(data);
         }
      });
   }

   private void getInfo(String data) {
      ApiHelper.getScanInfo(data, this, new CallbackNew<ScanInfo>() {
         @Override
         public void onResult(ScanInfo scanInfo) {
            if (scanInfo == null){
               return;
            }
            mList.add(scanInfo);
            mAdapter.notifyDataSetChanged();
            callThird(scanInfo);
         }
      });
   }

   private void callThird(ScanInfo scanInfo) {
      if (null != mSelectInfo) {
         scanInfo.box_code = mSelectInfo.box_code;
         scanInfo.box_code_show = mSelectInfo.box_code_show;
         scanInfo.item_no = (qrCodeList.size()-1)*10+10;
      }
      ApiHelper.callThird(scanInfo, this, new CallbackNew<ScanInfo>() {
         @Override
         public void onResult(ScanInfo scanInfo) {

         }
      },3);
   }

//   private  SerialPortManager serialPortManager;
//   private void getDoorState() {
//      serialPortManager = new SerialPortManager();
//      serialPortManager.init(this, UserCache.getSerialName(), UserCache.getPort(), new SerialPortManager.SerialCallBack() {
//         @Override
//         public void dataCallBack(String data) {
//            Log.e("--------->","------->data="+data);
//            byte[] aar = SerialUtil.hexStringToByteArray(data);
//            if (aar!=null && aar.length>5){
//               if (aar[1] == (byte) 0x03 && aar[4] == (byte)0x00){
//                  if (mTimer != null) {
//                     mTimer.cancel();
//                     mTimerTask.cancel();
//                  }
//                  operateRecord(0);
//               }
//            }
//         }
//
//         @Override
//         public void openCallBack(int code) {
//            if (code == 1){
//               loopDoorClose();
//            }
//         }
//
//         @Override
//         public void closeCallBack(int code) {
//
//         }
//      });
//   }

//   private static final int PERIOD = 3 * 1000;
//   private static final int DELAY = 5000;
//   private Timer mTimer;
//   private TimerTask mTimerTask;
//   public void loopDoorClose(){
//      Log.e("---------->","-------->loopDoorClose");
//      mTimer = new Timer();
//      mTimerTask = new TimerTask() {
//         @Override
//         public void run() {
//            Log.e("---------->","-------->loopDoorClose=run");
//            serialPortManager.openDoor(1, 0);
//         }
//      };
//      mTimer.schedule(mTimerTask,DELAY,PERIOD);
//   }
//
//   private void operateRecord(int type) {
//      ApiHelper.operateRecord(this, type, new CallbackNew<OperateResult>() {
//         @Override
//         public void onResult(OperateResult operateResult) {
//            if (type == 0){
//               finish();
//            }
//         }
//      });
//   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
//      if (mTimer != null) {
//         mTimer.cancel();
//         mTimerTask.cancel();
//      }
//      if (serialPortManager!=null){
//         serialPortManager.close();
//      }
   }
}
