package com.naz.sdkdemo.activity;

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

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

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.naz.sdkdemo.R;
import com.naz.sdkdemo.adaapter.ApplyTrademarkAdapter;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.base.Callback;
import com.naz.sdkdemo.base.CallbackNew;
import com.naz.sdkdemo.bean.OrderListInfo;
import com.naz.sdkdemo.bean.TrademarkInfo;
import com.naz.sdkdemo.helper.SerialIPManager;
import com.naz.sdkdemo.http.ApiHelper;
import com.naz.sdkdemo.utils.MyUtils;
import com.naz.sdkdemo.weight.RecycleViewDivider;
import com.naz.sdkdemo.weight.UserCache;

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

public class ApplyTrademarkActivity extends BaseActivity {

   private RecyclerView rvContent;
   private ApplyTrademarkAdapter mAdapter;
   private List<TrademarkInfo> mList;
   private int mPreSelectPosition = -1;
   private SerialIPManager serialIPManager;
   private EditText etSearch;
   private TextView tvSearch;

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

   @Override
   protected void initView() {
      getCodeInfo();
      rvContent = findViewById(R.id.rv_content);
      mAdapter = new ApplyTrademarkAdapter(mList);
      mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
         @Override
         public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
            baseQuickAdapter.notifyItemChanged(i, "select");
            if (mPreSelectPosition != -1) {
               TrademarkInfo info = (TrademarkInfo)baseQuickAdapter.getItem(mPreSelectPosition);
               info.select = false;
               baseQuickAdapter.notifyItemChanged(mPreSelectPosition, "unselect");
            }
            mPreSelectPosition = i;
         }
      });
      rvContent.setLayoutManager(new LinearLayoutManager(this));
      rvContent.setAdapter(mAdapter);
      rvContent.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL,
              MyUtils.dp2px(this, 8), R.color.transparent));
      findViewById(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (mPreSelectPosition==-1){
               Toast.makeText(ApplyTrademarkActivity.this, "请选择", Toast.LENGTH_SHORT).show();
               return;
            }
            next();
         }
      });
      findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            finish();
         }
      });

      etSearch = findViewById(R.id.et_search);
      tvSearch = findViewById(R.id.tv_search);
      tvSearch.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (etSearch.getText().toString().equals("")){
               Toast.makeText(ApplyTrademarkActivity.this, "请扫描派工单号或输入销售订单号", Toast.LENGTH_SHORT).show();
               return;
            }
            requestList(etSearch.getText().toString());
         }
      });
   }

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

         @Override
         public void receiveData(String data) {
            Log.e("--------->","-------->data="+data);
            etSearch.setText(data);
         }
      });
   }

   private void next() {
      if (serialIPManager!=null){
         serialIPManager.close(new SerialIPManager.CloseCallBack() {
            @Override
            public void closeResult(int code) {
               Intent intent = new Intent(ApplyTrademarkActivity.this, ApplyTrademarkSelectActivity.class);
               intent.putExtra("info", mAdapter.getItem(mPreSelectPosition));
               startActivity(intent);
               finish();
            }
         });
      }
   }

   private void requestList(String codeStr) {
      ApiHelper.getOrderBrandList(this, codeStr, new CallbackNew<OrderListInfo>() {
         @Override
         public void onResult(OrderListInfo orderListInfo) {
            if (null == mList) {
               mList = new ArrayList<>();
            }
            mList.clear();
            if (null != orderListInfo.content) {
               mList.addAll(orderListInfo.content);
            }
            mAdapter.setNewData(mList);
         }
      });
   }

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

            }
         });
      }
   }
}
