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

import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.ProductBean;
import com.wd.workoffice.bean.TradingBean;
import com.wd.workoffice.bean.event.ModifyCarEvent;
import com.wd.workoffice.ui.adapter.TradingProCarAdapter;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.widget.AddAndReduceView;

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

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

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商店-内部交易-购物车
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class StoreTradingCarActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    @BindView(R.id.ll_bottom)
    RelativeLayout llBottom;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_card)
    TextView tvCard;
    @BindView(R.id.tv_card_name)
    TextView tvCardName;
    @BindView(R.id.tv_dept)
    TextView tvDept;
    @BindView(R.id.tv_apply_num)
    TextView tvApplyNum;
    @BindView(R.id.ar_use_num)
    AddAndReduceView arUseNum;
    @BindView(R.id.rv_product)
    RecyclerView rvProduct;
    private List<ProductBean.RecordsBean> productList;
    private TradingProCarAdapter productAdapter;
    private Map data;
    private TradingBean.RecordsBean pro;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvProduct.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }

    @Override
    protected void initData() {
        data = JSON.parseObject(getIntent().getStringExtra("pro"), Map.class);
        pro = JSON.parseObject(data.get("pro").toString(), TradingBean.RecordsBean.class);
        tvName.setText(pro.getProductName());
        tvCard.setText(pro.getProductSimpleCode());
        tvCardName.setText(pro.getProductSoleCode());
        tvApplyNum.setText(MathUtils.converData(pro.getQuantity(),3));
        tvDept.setText(pro.getDeptName());
        arUseNum.setMaxNum(pro.getQuantity());
        arUseNum.setNumber(pro.getChooseNum());
        productList = new ArrayList<>();
        productAdapter = new TradingProCarAdapter(R.layout.item_trading_product_car, productList);
        productAdapter.bindToRecyclerView(rvProduct);
        productList.addAll(JSON.parseArray(data.get("proDetail").toString(), ProductBean.RecordsBean.class));
        productAdapter.notifyDataSetChanged();
        changeNumAndPrice();
    }

    @Override
    protected void initEvent() {
        arUseNum.setOnNumberChangedListener(new AddAndReduceView.OnNumberChangedListener() {
            @Override
            public void OnNumberChanged(String vs) {
                pro.setChooseNum(vs);
            }
        });
//        productAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
//            @Override
//            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                QMUIDialog.MessageDialogBuilder dialog = DialogUtils.okAndCancelDialog(StoreTradingCarActivity.this);
//                dialog.setMessage("是否删除").addAction("取消", new QMUIDialogAction.ActionListener() {
//                    @Override
//                    public void onClick(QMUIDialog dialog, int index) {
//                        dialog.dismiss();
//                    }
//                }).addAction("确定", new QMUIDialogAction.ActionListener() {
//                    @Override
//                    public void onClick(QMUIDialog dialog, int index) {
//                        dialog.dismiss();
//                        productList.remove(position);
//                        productAdapter.notifyDataSetChanged();
//                        EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(productList)));
//                    }
//                }).show();
//            }
//        });
    }

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

    @OnClick(R.id.tv_ok)
    public void onViewClicked() {
        String allPrice = "";
        for (ProductBean.RecordsBean recordsBean : productList) {
            allPrice = MathUtils.add(allPrice, MathUtils.multiply(recordsBean.getCarNum()
                    , MathUtils.converData(recordsBean.getInternalPrice(),3), 3), 3);
        }
        data.put("proDetail",productList);
        data.put("pro",pro);
        startActivity(StoreTradingConfirmActivity.class, "pro",JSON.toJSONString(data),
                "subProcess", getIntent().getStringExtra("subProcess"),
                "supplierId", getIntent().getStringExtra("supplierId"),
                "inStockId", getIntent().getStringExtra("inStockId"),
                "customerId", getIntent().getStringExtra("customerId"),
                "receiveDeptId", getIntent().getStringExtra("receiveDeptId"),
                "code", getIntent().getStringExtra("code"),
                "launchOrderAmount", allPrice);
    }

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

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

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ModifyCarEvent event) {
        changeNumAndPrice();
    }

    private void changeNumAndPrice() {
        tvNum.setText("共 ".concat(productList.size() + "").concat(" 件"));
        String allPrice = "";
        for (ProductBean.RecordsBean recordsBean : productList) {
            allPrice = MathUtils.add(allPrice, MathUtils.multiply(recordsBean.getCarNum(), recordsBean.getInternalPrice() + "", 3),
                    3);
        }
        tvPrice.setText("总计: ¥ ".concat(allPrice));
    }

}
