package com.naz.sdkdemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.naz.sdkdemo.R;
import com.naz.sdkdemo.adaapter.FullApplyTrademarkSelectAdapter;
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.OrderListInfo;
import com.naz.sdkdemo.bean.TrademarkInfo;
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.RecycleViewDivider;
import com.naz.sdkdemo.weight.UserCache;
import com.naz.sdkdemo.weight.XToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class WaitActivity extends BaseActivity {
   private int mType;
   private  SerialPortManager serialPortManager;
   @Override
   protected int getLayoutResId() {
      return R.layout.activity_wait;
   }

   @Override
   protected void initView() {
      mType = getIntent().getIntExtra("type", 1);
      ApiHelper.operateApply(this, mType, new CallbackNew<OperateResult>() {
         @Override
         public void onResult(OperateResult operateResult) {

         }
      });

      ApiHelper.loopMonitionOpen(this, 0, new Callback() {
         @Override
         public void onSuccess(int code, String msg, Object data) {
            openDoor();
         }
      });
   }

   private void openDoor() {
      serialPortManager = new SerialPortManager();
      serialPortManager.init(this, UserCache.getSerialName(), UserCache.getSerialRate(), new SerialPortManager.SerialCallBack() {
         @Override
         public void dataCallBack(String data) {
            Log.e("--------->","------->data="+data);
            byte[] aar = SerialUtil.hexStringToByteArray(data);
            if (aar!=null && aar.length>1){
//               if (aar[1] == (byte) 0x03 && aar[4] == (byte)0x01){
//                  if (mTimer != null) {
//                     mTimer.cancel();
//                     mTimerTask.cancel();
//                  }
//                  operateRecord(0);
//               }else
               if (aar[1] == (byte) 0x06){
                  operateRecord(1);
                  new Handler().postDelayed(new Runnable() {
                     @Override
                     public void run() {
                        finish();
                     }
                  }, 10*1000);
               }
            }
         }

         @Override
         public void openCallBack(int code) {
            if (code == 1){
               serialPortManager.openDoor(3, 1);
//               loopDoorClose();
            }else {
               Toast.makeText(WaitActivity.this, "串口未打开", Toast.LENGTH_SHORT).show();
            }
         }

         @Override
         public void closeCallBack(int code) {

         }
      });
   }

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

//   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");
//            runOnUiThread(new Runnable() {
//               @Override
//               public void 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();
//            }
         }
      });
   }
}
