package com.naz.sdkdemo.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.naz.sdkdemo.R;
import com.naz.sdkdemo.adaapter.UpTrademarkScanAdapter;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.base.CallbackNew;
import com.naz.sdkdemo.bean.ScanInfo;
import com.naz.sdkdemo.bean.StockInfo;
import com.naz.sdkdemo.helper.SerialIPManager;
import com.naz.sdkdemo.http.ApiHelper;
import com.naz.sdkdemo.utils.MyUtils;
import com.naz.sdkdemo.weight.GridSpaceItemDecoration;
import com.naz.sdkdemo.weight.UserCache;

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

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

public class UpTrademarkScanActivity extends BaseActivity {
   private RecyclerView rvContent;
   private UpTrademarkScanAdapter mAdapter;
   private List<ScanInfo> mList = new ArrayList<>();
   private List<String> qrCodeList = new ArrayList<>();
   private SerialIPManager serialIPManager;
   private StockInfo mStockInfo;
   private TextView tvTips, tvQrCode;
   private Button btnCancel, btnSure;

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

   @Override
   protected void initView() {
      mStockInfo = (StockInfo)getIntent().getSerializableExtra("info");
      rvContent = findViewById(R.id.rv_content);
      mAdapter = new UpTrademarkScanAdapter(mList);
      rvContent.setAdapter(mAdapter);
      rvContent.setLayoutManager(new GridLayoutManager(this, 3));
      rvContent.addItemDecoration(new GridSpaceItemDecoration(MyUtils.dp2px(this, 12)));
      tvTips = findViewById(R.id.tv_tips);
      scanInfo();
      tvQrCode = findViewById(R.id.tv_qrcode);
      btnCancel = findViewById(R.id.btn_cancel);
      btnSure = findViewById(R.id.btn_sure);
      btnSure.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (mList.size() > 0){
//               startActivity(new Intent(UpTrademarkScanActivity.this, WaitCloseDoorActivity.class));
//               finish();
               btnSure.setClickable(false);
               Toast.makeText(UpTrademarkScanActivity.this, "确认中，请稍后", Toast.LENGTH_SHORT).show();
               new Handler().postDelayed(new Runnable() {
                  @Override
                  public void run() {
                     finish();
                  }
               }, 10*1000);
            }else {
               Toast.makeText(UpTrademarkScanActivity.this, "商标不能为空", Toast.LENGTH_SHORT).show();
            }
         }
      });
      btnCancel.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            finish();
         }
      });
      btnSure.setVisibility(View.GONE);
   }


   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(UpTrademarkScanActivity.this, "请确认串口服务器正常开启", Toast.LENGTH_SHORT).show();
            }
         }

         @Override
         public void receiveData(String data) {
            Log.e("--------->","-------->receiveData="+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();
            tvTips.setVisibility(View.VISIBLE);
            if (null == mStockInfo) {
               return;
            }
            if (mList.size()>0){
               tvQrCode.setVisibility(View.GONE);
               btnSure.setVisibility(View.VISIBLE);
            }
            if (TextUtils.equals(mStockInfo.material_code, scanInfo.material_code)) {
               tvTips.setText("核对正确，请根据商标仓门灯光提示上"+mStockInfo.box_code_show);
               tvTips.setTextColor(Color.parseColor("#ff009e40"));
               tvTips.setBackgroundResource(R.drawable.bg_tip_success);
               callThird(scanInfo);
            } else {
               tvTips.setText("核对错误，请更换并重新扫描商标包装码");
               tvTips.setTextColor(Color.parseColor("#FF3500"));
               tvTips.setBackgroundResource(R.drawable.bg_tip_error);
            }
         }
      });
   }

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

         }
      },2);
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
      serialIPManager.close(new SerialIPManager.CloseCallBack() {
         @Override
         public void closeResult(int code) {

         }
      });
   }
}
