package com.naz.sdkdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
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.ApplyTrademarkSelectAdapter;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.base.Callback;
import com.naz.sdkdemo.bean.OrderListInfo;
import com.naz.sdkdemo.bean.TrademarkInfo;
import com.naz.sdkdemo.http.ApiHelper;
import com.naz.sdkdemo.utils.MyUtils;
import com.naz.sdkdemo.weight.RecycleViewDivider;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ApplyTrademarkSelectActivity extends BaseActivity {

   private RecyclerView rvContent;
   private ApplyTrademarkSelectAdapter mAdapter;
   private List<TrademarkInfo> mList;
   private List<TrademarkInfo> mSelectList = new ArrayList<>();
   private TrademarkInfo mSelectInfo;

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

   @Override
   protected void initView() {
      mSelectInfo = (TrademarkInfo)getIntent().getSerializableExtra("info");
      rvContent = findViewById(R.id.rv_content);
      mAdapter = new ApplyTrademarkSelectAdapter(mList);
      mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
         @Override
         public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
            baseQuickAdapter.notifyItemChanged(i, "change");
         }
      });
//      mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
//         @Override
//         public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
//            baseQuickAdapter.notifyItemChanged(i, "change");
//         }
//      });
      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.tv_search).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {

         }
      });
      findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            finish();
         }
      });
      findViewById(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (mList.size()<=0){
               return;
            }
            mSelectList.clear();
            for (TrademarkInfo info:mList) {
               if (info.select){
                  mSelectList.add(info);
               }
            }
            if (mSelectList.size()<=0){
               Toast.makeText(ApplyTrademarkSelectActivity.this, "请选择", Toast.LENGTH_SHORT).show();
               return;
            }
            Intent intent = new Intent(ApplyTrademarkSelectActivity.this, ApplyTrademarkCompleteActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("info", (Serializable)mSelectList);
            intent.putExtras(bundle);
            intent.putExtra("data", mSelectInfo);
            startActivity(intent);
            finish();
         }
      });
      if (null != mSelectInfo) {
         TextView tvOrderId = findViewById(R.id.tv_order_id);
         tvOrderId.setText(mSelectInfo.no);
         TextView tvDealerName = findViewById(R.id.tv_dealer_name);
         tvDealerName.setText("");
         TextView tvCustomerName = findViewById(R.id.tv_customer_name);
         tvCustomerName.setText(mSelectInfo.customer_name);
      }
      requestList();
   }

   private void requestList(){
      String orderId = mSelectInfo.no;
      String codeStr = mSelectInfo.material_code;
      if (TextUtils.isEmpty(orderId)) {
         Toast.makeText(this, "订单号不能为空", Toast.LENGTH_SHORT).show();
         return;
      }
      if(codeStr == null){
         codeStr = "";
      }
      ApiHelper.getOrderList(orderId, codeStr, this,0 ,new Callback() {
         @Override
         public void onSuccess(int code, String msg, Object data) {
            if (null == mList) {
               mList = new ArrayList<>();
            }
            mList.clear();
            if (data instanceof OrderListInfo) {
               mList.addAll(((OrderListInfo) data).content);
            }
            mAdapter.setNewData(mList);
         }
      });
   }
}
