package com.naz.sdkdemo.activity;

import android.util.Log;
import android.widget.Toast;

import com.naz.sdkdemo.R;
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.helper.SerialPortManager;
import com.naz.sdkdemo.http.ApiHelper;
import com.naz.sdkdemo.utils.SerialUtil;
import com.naz.sdkdemo.weight.UserCache;

import java.util.Timer;
import java.util.TimerTask;

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

   @Override
   protected void initView() {
      ApiHelper.operateApply(this, 1, new CallbackNew<OperateResult>() {
         @Override
         public void onResult(OperateResult operateResult) {
            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)0x00){
                  if (mTimer != null) {
                     mTimer.cancel();
                     mTimerTask.cancel();
                  }
                  operateRecord(0);
               }
            }
         }

         @Override
         public void openCallBack(int code) {
            if (code == 1){
               loopDoorClose();
            }else {
               Toast.makeText(WaitCloseDoorActivity.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();
      }
   }

   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();
            }
         }
      });
   }
}
