package com.naz.sdkdemo.activity;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.naz.sdkdemo.NewMainActivity;
import com.naz.sdkdemo.R;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.comm.Constants;
import com.naz.sdkdemo.helper.SerialPortManager;
import com.naz.sdkdemo.utils.SerialUtil;
import com.naz.sdkdemo.weight.UserCache;

import java.io.IOException;

import android_serialport_api.SerialPortFinder;
import tp.xmaihh.serialport.SerialHelper;
import tp.xmaihh.serialport.bean.ComBean;
import tp.xmaihh.serialport.utils.ByteUtil;

public class SettingActivity extends BaseActivity {
   private int markNum = 0;//0-12 十二个门
   private SerialPortManager serialPortManager;

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

   @Override
   protected void initView() {
      SerialPortFinder finder = new SerialPortFinder();
      TextView serialNameTxt = findViewById(R.id.serial_name);
      if (finder.getAllDevicesPath().length>0){
         StringBuilder path = new StringBuilder();
         for (int i = 0; i < finder.getAllDevicesPath().length; i++) {
            path.append(finder.getAllDevicesPath()[i]).append(", ");
         }
         serialNameTxt.setText(path);
      }

      EditText ipEt = findViewById(R.id.ip_et);
      EditText portEt = findViewById(R.id.port_et);
      EditText serialNameEt = findViewById(R.id.serial_name_et);
      EditText serialRateEt = findViewById(R.id.serial_rate_et);

      ipEt.setText(UserCache.getIP());
      portEt.setText(""+UserCache.getPort());
      serialNameEt.setText(UserCache.getSerialName());
      serialRateEt.setText(""+UserCache.getSerialRate());
      findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(SettingActivity.this, "保存中，请稍等！", Toast.LENGTH_SHORT).show();
            UserCache.saveSerialInfo(ipEt.getText().toString(), Integer.parseInt(portEt.getText().toString()),
                    serialNameEt.getText().toString(), Integer.parseInt(serialRateEt.getText().toString()));

            initSerial();
//            Toast.makeText(SettingActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
//            finish();
         }
      });
   }

   private void initSerial() {
      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 (markNum == 12){
                  Log.e("--------->","------->closeSerial");
                  toFinish();
               }else {
                  startSetSerial();
               }
            }
         }

         @Override
         public void openCallBack(int code) {
            if (code == 1){
               startSetSerial();
               new Handler().postDelayed(new Runnable() {
                  @Override
                  public void run() {
                     if (markNum == 1){
                        Log.e("--------->","------->closeSerial");
                        toFinish();
                     }
                  }
               }, 1000);
            }else {
               Toast.makeText(SettingActivity.this, "串口未打开", Toast.LENGTH_SHORT).show();
            }
         }

         @Override
         public void closeCallBack(int code) {
            Toast.makeText(SettingActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
            finish();
         }
      });
   }

   private void toFinish() {
      serialPortManager.close();

   }

   private void startSetSerial() {
      Log.e("--------->","------->startSetSerial markNum ="+markNum);
      switch (markNum){
         case 0://0110 0039 0003 06 01f4 0001 003c
            serialPortManager.preValue("01100001000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
//                serialPortManager.preValue("0110003900030601f40001003c");
            break;
         case 1:
            serialPortManager.preValue("01100008000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
         case 2:
            serialPortManager.preValue("0110000F000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
         case 3:
            serialPortManager.preValue("01100016000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
         case 4:
            serialPortManager.preValue("0110001D000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
         case 5:
            serialPortManager.preValue("01100024000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
         case 6:
            serialPortManager.preValue("0110002B000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
         case 7:
            serialPortManager.preValue("01100032000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
         case 8:
            serialPortManager.preValue("01100039000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
         case 9:
            serialPortManager.preValue("01100040000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
         case 10:
            serialPortManager.preValue("01100047000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
         case 11:
            serialPortManager.preValue("0110004E000306"+ Constants.mark_speed+ Constants.mark_num+Constants.mark_length);
            break;
      }
      markNum++;
   }
}
