package com.naz.sdkdemo.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.naz.sdkdemo.R;
import com.naz.sdkdemo.adaapter.ScanMarkAdapter;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.base.CallbackNew;
import com.naz.sdkdemo.bean.ScanInfo;
import com.naz.sdkdemo.helper.SerialIPManager;
import com.naz.sdkdemo.http.Api;
import com.naz.sdkdemo.http.ApiHelper;
import com.naz.sdkdemo.okgo.callback.DialogCallback;
import com.naz.sdkdemo.okgo.module.BaseResponse2;
import com.naz.sdkdemo.weight.UserCache;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

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

public class SaveTrademarkActivity extends BaseActivity {
   private SerialIPManager serialIPManager;
   private List<ScanInfo> list = new ArrayList<>();
   private List<String> qrCodeList = new ArrayList<>();
   private RecyclerView rvMark;
   private ScanMarkAdapter mAdapter;
   private Button btnSure,btnCancel;
   @Override
   protected int getLayoutResId() {
      return R.layout.activity_save_trademark;
   }

   @Override
   protected void initView() {
      rvMark = findViewById(R.id.rv_content);
      rvMark.setLayoutManager(new GridLayoutManager(this, 2));
      mAdapter = new ScanMarkAdapter(list);
      rvMark.setAdapter(mAdapter);

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

      btnSure = findViewById(R.id.btn_sure);
      btnSure.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (list.size()>0){
               startActivity(new Intent(SaveTrademarkActivity.this, WaitActivity.class));
               finish();
            }else {
               Toast.makeText(SaveTrademarkActivity.this, "商标不能为空", Toast.LENGTH_SHORT).show();
            }
         }
      });
      btnCancel = findViewById(R.id.btn_cancel);
      btnCancel.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            finish();
         }
      });
   }

   private void getInfo(String data) {
      Log.e("--------->","-------->getInfo data="+data);
      JSONObject raw = new JSONObject();
      data = data.replaceAll("\r", "");
      try {
         raw.put("roll_qr_code", data);
      } catch (JSONException e) {

      }
      OkGo.<BaseResponse2<ScanInfo>>post(Api.POST_SCAN_CODE())
              .tag(this)
              .upJson(raw)
              .execute(new DialogCallback<BaseResponse2<ScanInfo>>(this) {
                 @Override
                 public void onSuccess(Response<BaseResponse2<ScanInfo>> response) {
                    ScanInfo detail = response.body().data;
                    if (detail != null){
                       list.add(detail);
                       mAdapter.notifyDataSetChanged();
                       exitBrand(response.body().data);
                    }
                 }
              });
   }

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

         }
      },1);
   }

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

         }
      });
   }
}
