package com.naz.sdkdemo.activity;

import android.util.Log;
import android.view.View;
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.RecycleTrademarkSelectAdapter;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.base.Callback;
import com.naz.sdkdemo.base.CallbackNew;
import com.naz.sdkdemo.bean.ScanRecycleInfo;
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 RecycleTrademarkSelectActivity extends BaseActivity {

   private RecyclerView rvContent;
   private RecycleTrademarkSelectAdapter mAdapter;
   private List<ScanRecycleInfo> mList = new ArrayList<>();
   private boolean mSelectAll;
   private TextView tvSelectAll;

   private TrademarkInfo mSelectInfo;

   private SerialIPManager serialIPManager;
   private List<String> qrCodeList = new ArrayList<>();

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

   @Override
   protected void initView() {
//      mock();
      mSelectInfo = (TrademarkInfo)getIntent().getSerializableExtra("info");
      rvContent = findViewById(R.id.rv_content);
      mAdapter = new RecycleTrademarkSelectAdapter(mList);
      mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
         @Override
         public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
            baseQuickAdapter.notifyItemChanged(i, "change");
            ScanRecycleInfo info = (ScanRecycleInfo)baseQuickAdapter.getItem(i);
            if (info.select) {
               mSelectAll = false;
            } else {
               boolean all = true;
               for (int j = 0; j < baseQuickAdapter.getData().size(); j++) {
                  if (j != i) {
                     info = (ScanRecycleInfo)baseQuickAdapter.getItem(j);
                     if (!info.select) {
                        all = false;
                        break;
                     }
                  }
               }
               mSelectAll = all;
            }
            updateSelectAllUi();
         }
      });
      rvContent.setLayoutManager(new LinearLayoutManager(this));
      rvContent.setAdapter(mAdapter);
      rvContent.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL,
              MyUtils.dp2px(this, 8), R.color.transparent));
      tvSelectAll = findViewById(R.id.tv_select_all);
      tvSelectAll.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            mSelectAll = !mSelectAll;
            for (ScanRecycleInfo info : mList) {
               info.select = mSelectAll;
            }
            updateSelectAllUi();
            mAdapter.notifyDataSetChanged();
         }
      });

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

      List<ScanRecycleInfo> list = new ArrayList<>();
      findViewById(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            list.clear();
            for (ScanRecycleInfo info:mList) {
               if (info.select){
                  info.item_no = (list.size())*10+10;
                  list.add(info);
               }
            }
            ApiHelper.getBrandRecover(mSelectInfo.no, list, RecycleTrademarkSelectActivity.this, new Callback() {
               @Override
               public void onSuccess(int code, String msg, Object data) {
                  finish();
               }
            });
         }
      });
      findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            finish();
         }
      });
   }

   private void getInfo(String data) {
      ApiHelper.getBrandQrCodeInfo(data, this, new CallbackNew<ScanRecycleInfo>() {
         @Override
         public void onResult(ScanRecycleInfo scanRecycleInfo) {
            if (scanRecycleInfo != null){
               mList.add(scanRecycleInfo);
               mAdapter.setNewData(mList);
            }
         }
      });
   }

   private void updateSelectAllUi() {
      tvSelectAll.setCompoundDrawablesWithIntrinsicBounds(mSelectAll ? R.mipmap.icon_selected :
              R.mipmap.icon_unselect, 0, 0, 0);
   }
}
