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

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.WarehoustBean;
import com.wd.workoffice.bean.event.CheckTicketEvent;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.store.StoreSaleClientActivity;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

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

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * 添加组装订单
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class ProductOrderAddActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.tv_client)
    TextView tvClient;
    @BindView(R.id.rl_client)
    LinearLayout rlClient;
    @BindView(R.id.tv_from_stock)
    TextView tvFromStock;
    @BindView(R.id.rl_from_stock)
    LinearLayout rlFromStock;
    @BindView(R.id.tv_to_stock)
    TextView tvToStock;
    @BindView(R.id.rl_to_stock)
    LinearLayout rlToStock;
    @BindView(R.id.tv_use_stock)
    TextView tvUseStock;
    @BindView(R.id.rl_use_stock)
    LinearLayout rlUseStock;
    private Map<String, Object> param;
    private Integer clientId;
    private List<String> stockNameList = new ArrayList<>();
    private List<WarehoustBean.RecordsBean> stockList;
    private QMUIDialog.MenuDialogBuilder fromStockDialog;
    private QMUIDialog.MenuDialogBuilder useStockDialog;
    private WarehoustBean.RecordsBean chooseFromStock;
    private WarehoustBean.RecordsBean useFromStock;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        param = WorkUtils.simpleParam();
        param.put("deptId", UserKeeper.getInstance().getUserDepId());
        param.put("sign", "SEND");
        fromStockDialog = DialogUtils.listDialog(this);
        useStockDialog = DialogUtils.listDialog(this);
    }

    @Override
    protected void initData() {
    }


    @Override
    protected void initEvent() {
    }

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


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001 && resultCode == 10001) {
            String clientName = data.getStringExtra("clientName");
            clientId = data.getIntExtra("id", 0);
            tvClient.setText(clientName);
            fromStockDialog = DialogUtils.listDialog(this);
            useStockDialog = DialogUtils.listDialog(this);
            tvUseStock.setText("");
            tvToStock.setText("");
            tvFromStock.setText("");
            chooseFromStock = null;
            useFromStock = null;
            param.put("customerId", clientId);
            getData();
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void refresh(CheckTicketEvent event) {
        finish();
    }


    @OnClick({R.id.rl_client, R.id.rl_from_stock, R.id.tv_next, R.id.rl_use_stock})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_client:
                startActivityForResult(StoreSaleClientActivity.class, 10001,
                        "processes", "PROCESS_PACKAGE",
                        "depId", UserKeeper.getInstance().getUserDepId(), "code", "CUSTOMER");
                break;
            case R.id.rl_from_stock:
                if (clientId == null) {
                    toast("请先选择客户");
                    return;
                }
                fromStockDialog.show();
                break;
            case R.id.tv_next:
                if (clientId == null) {
                    toast("请先选择客户");
                    return;
                }
                if (fromStockDialog == null) {
                    toast("请先选择来源仓库");
                    return;
                }
                if (useFromStock == null) {
                    toast("请先选择领用仓库");
                    return;
                }
                JSONObject base = new JSONObject();
                base.put("1", tvClient.getText().toString());
                base.put("2", tvFromStock.getText().toString());
                base.put("3", tvToStock.getText().toString());
                base.put("4", tvUseStock.getText().toString());
                startActivity(ProductOrderProActivity.class,
                        "base", base.toJSONString(),
                        "customerId", clientId.toString(),
                        "usedStockId", useFromStock.getId() + "",
                        "targetStockId", chooseFromStock.getId() + "");
                break;
            case R.id.rl_use_stock:
                if (clientId == null) {
                    toast("请先选择客户");
                    return;
                }
                useStockDialog.show();
                break;
        }
    }

    private void getData() {
        RtfUtils.getRtf().getStock(param).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<WarehoustBean.RecordsBean> getList = JSON.parseArray(data.getData().toString(), WarehoustBean.RecordsBean.class);
                stockList = getList;
                stockNameList.clear();
                for (WarehoustBean.RecordsBean process : stockList) {
                    stockNameList.add(process.getName());
                }
                fromStockDialog.addItems(stockNameList.toArray(new String[stockNameList.size()]), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        chooseFromStock = stockList.get(which);
                        tvFromStock.setText(stockNameList.get(which));
                        tvToStock.setText(stockNameList.get(which));
                        dialog.dismiss();
                    }
                });
                useStockDialog.addItems(stockNameList.toArray(new String[stockNameList.size()]), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        useFromStock = stockList.get(which);
                        tvUseStock.setText(stockNameList.get(which));
                        dialog.dismiss();
                    }
                });
            }
        });
    }
}