package com.naz.sdkdemo.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import androidx.recyclerview.widget.GridLayoutManager;
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.DeviceSearchAdapter;
import com.naz.sdkdemo.adaapter.ProductSearchAdapter;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.bean.OrderListInfo;
import com.naz.sdkdemo.bean.SearchResult;
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.GridSpaceItemDecoration;

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

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

public class StockSearchActivity extends BaseActivity {
    private RecyclerView rvProduct, rvDevice;
    private ProductSearchAdapter productAdapter;
    private DeviceSearchAdapter deviceAdapter;
    private List<TrademarkInfo> productSearchList;
    private List<TrademarkInfo> deviceSearchList;
    private int mPreProductSelectPosition = -1;
    private int mPreDeviceSelectPosition = -1;
    private Button btnSure;

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

    @Override
    protected void initView() {
        rvProduct = findViewById(R.id.rv_product);
        productAdapter = new ProductSearchAdapter(productSearchList);
        productAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                baseQuickAdapter.notifyItemChanged(i, "select");
                if (mPreProductSelectPosition != -1) {
                    TrademarkInfo info = (TrademarkInfo)baseQuickAdapter.getItem(mPreProductSelectPosition);
                    info.select = false;
                    baseQuickAdapter.notifyItemChanged(mPreProductSelectPosition, "unselect");
                }
                mPreProductSelectPosition = i;
                btnSure.setVisibility(View.VISIBLE);
            }
        });
        rvProduct.setAdapter(productAdapter);
        rvProduct.setLayoutManager(new GridLayoutManager(this, 3));
        GridSpaceItemDecoration itemDecoration = new GridSpaceItemDecoration(MyUtils.dp2px(this, 12), false);
        itemDecoration.setEndFromSize(0);
        rvProduct.addItemDecoration(itemDecoration);

        rvDevice = findViewById(R.id.rv_device);
        deviceAdapter = new DeviceSearchAdapter(deviceSearchList);
        deviceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                baseQuickAdapter.notifyItemChanged(i, "select");
                if (mPreDeviceSelectPosition != -1) {
                    TrademarkInfo info = (TrademarkInfo)baseQuickAdapter.getItem(mPreDeviceSelectPosition);
                    info.select = false;
                    baseQuickAdapter.notifyItemChanged(mPreDeviceSelectPosition, "unselect");
                }
                mPreDeviceSelectPosition = i;
            }
        });
        rvDevice.setAdapter(deviceAdapter);
        rvDevice.setLayoutManager(new GridLayoutManager(this, 2));
        GridSpaceItemDecoration itemDecoration2 = new GridSpaceItemDecoration(MyUtils.dp2px(this, 12), false);
        itemDecoration2.setEndFromSize(0);
        rvDevice.addItemDecoration(itemDecoration2);
        getList();
        btnSure = findViewById(R.id.btn_sure);
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toNotify();
            }
        });
    }

    private void toNotify() {
        Intent intent = new Intent(this, WaitActivity.class);
        intent.putExtra("type", 5);
        startActivity(intent);
        finish();
    }

    private void getList() {
        JSONObject body = new JSONObject();
        try {
            body.put("direction", "DESC");
            body.put("property", "id");
            body.put("fromClientType", "pc");
            body.put("number", 0);
            body.put("sorts", new JSONArray());
            body.put("rules", new JSONArray());
            body.put("specialConditions", new JSONArray());
            body.put("dynamicFormCode", "box_inventory");
            body.put("developmentSystemId", null);
        } catch (JSONException e) {

        }
        OkGo.<List<TrademarkInfo>>post(Api.stockSearchUrl())
                .tag(this)
                .upJson(body)
                .execute(new DialogCallback<List<TrademarkInfo>>(this) {
                    @Override
                    public void onSuccess(Response<List<TrademarkInfo>> response) {
                        setData(response.body());
                    }
                });

    }

    private void setData(List<TrademarkInfo> data) {
        productSearchList = new ArrayList<>();
        deviceSearchList = new ArrayList<>();
        for (TrademarkInfo info : data) {
            if (TextUtils.equals(info.box_code_show, "设备仓")) {
                deviceSearchList.add(info);
            } else {
                productSearchList.add(info);
            }
        }
        productAdapter.setNewData(productSearchList);
        deviceAdapter.setNewData(deviceSearchList);
    }
}
