package com.wd.workoffice.ui.activity.bat.order;

import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.bean.OrderBean;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.ProCruibleAdapter;
import com.wd.workoffice.ui.adapter.SaleCruibleAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

/**
 * 坩埚订单搜索
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class CrucibleOrderSearchActivity extends WorkBaseActivity {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.tb_title)
    Toolbar tbTitle;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private Map<String, Object> param;
    private List<OrderBean> orderList;
    private String identity;
    private int page = 1;
    private SaleCruibleAdapter saleAdapter;
    private ProCruibleAdapter proAdapter;
    private String type;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        setSupportActionBar(tbTitle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void initData() {
        identity = getIntent().getStringExtra("identity");
        type = getIntent().getStringExtra("type");
        orderList = new ArrayList<>();
        if (TextUtils.equals(type, "1")) {
            etKey.setHint("坩埚销售订单:输入订单号");
            saleAdapter = new SaleCruibleAdapter(R.layout.item_crucible_sale, orderList);
            saleAdapter.bindToRecyclerView(rvData);
            saleAdapter.setEmptyView(R.layout.view_empty_order, rvData);
        } else {
            etKey.setHint("坩埚生产订单:输入订单号");
            proAdapter = new ProCruibleAdapter(R.layout.item_crucible_buy, orderList);
            proAdapter.bindToRecyclerView(rvData);
            proAdapter.setEmptyView(R.layout.view_empty_order, rvData);

        }
        param = WorkUtils.pageKey();
        param.put("current", page);
        param.put("identity", identity);
        param.put("searchKey", "orderId");
        if (!WorkUtils.hasPermission(PagePermissionType.CO_CRUCIBLE_LIST.getPermission())){
            toast(R.string.permission);
        }
    }

    @Override
    protected void initEvent() {
        if (TextUtils.equals(type, "1")) {
            saleAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    param.put("current", page);
                    getData();
                }
            }, rvData);
            saleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    startActivity(CrucibleOrderDetailActivity.class,
                            "identity", identity,
                            "type", "2",
                            "id", orderList.get(position).getId() + "",
                            "status", orderList.get(position).getStatus() + "");
                }
            });

        } else {
            proAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    param.put("current", page);
                    getData();
                }
            }, rvData);
            proAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (!WorkUtils.hasPermission(PagePermissionType.CO_CRUCIBLE_DETAILS.getPermission())){
                        toast(R.string.permission);
                        return;
                    }
                    startActivity(CrucibleOrderDetailActivity.class,
                            "identity", identity,
                            "type", "1",
                            "id", orderList.get(position).getId() + "",
                            "status", orderList.get(position).getStatus() + "");
                }
            });

        }
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_order_search;
    }


    @OnClick(R.id.iv_search)
    public void onViewClicked() {
        if (!WorkUtils.hasPermission(PagePermissionType.CO_CRUCIBLE_LIST.getPermission())){
            toast(R.string.permission);
            return;
        }
        String key = etKey.getText().toString();
        if (TextUtils.isEmpty(key)) {
            toast("请输入搜索关键字");
            return;
        }
        page = 1;
        param.put("current", page);
        param.put("searchValue", key);
        getData();
    }

    private void getData() {
        Observable<BaseBean> observable;
        if (TextUtils.equals(type, "1")) {
            observable = RtfUtils.getRtf().getCucibleSaleOrder(param);
        } else {
            observable = RtfUtils.getRtf().getCucibleOrder(param);
        }
        observable.compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                List<OrderBean> dataList = JSON.parseArray(JSON.parseObject(data.getData().toString()).getString("records"), OrderBean.class);

                if (page == 1) {
                    orderList.clear();
                    orderList.addAll(dataList);
                    if (TextUtils.equals(type, "1")) {
                        saleAdapter.notifyDataSetChanged();
                        saleAdapter.loadMoreComplete();
                    } else {
                        proAdapter.notifyDataSetChanged();
                        proAdapter.loadMoreComplete();
                    }
                } else {
                    if (TextUtils.equals(type, "1")) {
                        saleAdapter.loadMoreComplete();
                        orderList.addAll(dataList);
                        saleAdapter.notifyDataSetChanged();
                    } else {
                        proAdapter.loadMoreComplete();
                        orderList.addAll(dataList);
                        proAdapter.notifyDataSetChanged();
                    }
                }
                if (dataList.size() == 0) {
                    if (TextUtils.equals(type, "1")) {
                        saleAdapter.loadMoreEnd();
                    } else {
                        proAdapter.loadMoreEnd();
                    }
                } else {
                    page++;
                }
                toast("加载成功");
            }
        });
    }

}
