package com.naz.sdkdemo.activity;

import android.content.Intent;
import android.view.View;

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

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.naz.sdkdemo.R;
import com.naz.sdkdemo.adaapter.FullApplyTrademarkAdapter;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.bean.TrademarkInfo;
import com.naz.sdkdemo.http.Api;
import com.naz.sdkdemo.okgo.callback.DialogCallback;
import com.naz.sdkdemo.utils.MyUtils;
import com.naz.sdkdemo.weight.RecycleViewDivider;
import com.naz.sdkdemo.weight.UserCache;
import com.naz.sdkdemo.weight.XToast;

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

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

public class FullApplyTrademarkActivity extends BaseActivity {

   private RecyclerView rvContent;
   private FullApplyTrademarkAdapter mAdapter;
   private List<TrademarkInfo> mList;
   private int mPreSelectPosition = -1;

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

   @Override
   protected void initView() {
      rvContent = findViewById(R.id.rv_content);
      mAdapter = new FullApplyTrademarkAdapter(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) {
            next();
         }
      });
      findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            finish();
         }
      });
      requestList();
   }

   private void next() {
      if (mPreSelectPosition == -1) {
         XToast.showToast("请选择");
         return;
      }
      Intent intent = new Intent(FullApplyTrademarkActivity.this, FullApplyTrademarkSelectActivity.class);
      intent.putExtra("info", mAdapter.getItem(mPreSelectPosition));
      startActivity(intent);
      finish();
   }

   private void requestList() {
      JSONObject body = new JSONObject();
      try {
         body.put("device_code", UserCache.getDeviceId());
      } catch (JSONException e) {

      }
      OkGo.<List<TrademarkInfo>>post(Api.POST_FULL_APPLY_LIST())
              .tag(this)
              .upJson(body)
              .execute(new DialogCallback<List<TrademarkInfo>>(this) {
                 @Override
                 public void onSuccess(Response<List<TrademarkInfo>> response) {
                    if (null == mList) {
                       mList = new ArrayList<>();
                    }
                    mList.clear();
                    if (null != response.body()) {
                       mList.addAll(response.body());
                    }
                    mAdapter.setNewData(mList);
                 }
              });
   }
}
