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

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkToolBarActivity;
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.DialogUtils;
import com.wd.workoffice.util.MathUtils;

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

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

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.rv_product)
    RecyclerView rvProduct;
    private List<TradingBean.RecordsBean> productList;
    private TradingProCarAdapter productAdapter;

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

    @Override
    protected void initData() {
        productList = new ArrayList<>();
        productAdapter = new TradingProCarAdapter(R.layout.item_trading_product_car, productList);
        productAdapter.bindToRecyclerView(rvProduct);
        productList.addAll(JSON.parseArray(getIntent().getStringExtra("pro"), TradingBean.RecordsBean.class));
        productAdapter.notifyDataSetChanged();
        changeNumAndPrice();
    }

    @Override
    protected void initEvent() {
        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 (TradingBean.RecordsBean recordsBean : productList) {
            allPrice = MathUtils.add(allPrice, MathUtils.multiply(recordsBean.getChangePro().getCarNum()
                    , MathUtils.converData(recordsBean.getChangePro().getInternalPrice(), 3), 3), 3);
        }
        startActivity(StoreTradingConfirmActivity.class, "pro", JSON.toJSONString(productList),
                "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 (TradingBean.RecordsBean recordsBean : productList) {
            allPrice = MathUtils.add(allPrice, MathUtils.multiply(recordsBean.getChangePro().getCarNum(), recordsBean.getChangePro().getInternalPrice() + "", 3),
                    3);
        }
        tvPrice.setText("总计: ¥ ".concat(allPrice));
    }

}
