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

import android.text.TextUtils;
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.ProductBean;
import com.wd.workoffice.bean.event.ModifyCarEvent;
import com.wd.workoffice.bean.workEnum.PermissionType;
import com.wd.workoffice.ui.adapter.StoreCarAdapter;
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 StoreSaleCarActivity 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<ProductBean.RecordsBean> productList;
    private StoreCarAdapter productAdapter;

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

    @Override
    protected void initData() {
        productList = new ArrayList<>();
        if (TextUtils.equals(PermissionType.PROCESS_SUPPLY.getCode(), getIntent().getStringExtra("code"))) {
            productAdapter = new StoreCarAdapter(R.layout.item_store_car, productList, 2);
        } else {
            productAdapter = new StoreCarAdapter(R.layout.item_store_car, productList);
        }
        productAdapter.bindToRecyclerView(rvProduct);
        String pro = getIntent().getStringExtra("pro");
        productList.addAll(JSON.parseArray(pro, ProductBean.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(StoreSaleCarActivity.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_bat_shopcar;
    }

    @OnClick(R.id.tv_ok)
    public void onViewClicked() {
        String allPrice = "";
        for (ProductBean.RecordsBean recordsBean : productList) {
            allPrice = MathUtils.add(allPrice, MathUtils.multiply(recordsBean.getCarNum() + "", recordsBean.getInternalPrice() + "", 2), 2);
        }
        if (productList.size() == 0) {
            toast("购物车里没有产品哦");
            return;
        }
        startActivity(StoreSaleConfirmActivity.class, "pro", JSON.toJSONString(productList),
                "subProcess", getIntent().getStringExtra("subProcess"),
                "supplierId", getIntent().getStringExtra("supplierId"),
                "subProcessId", getIntent().getStringExtra("subProcessId"),
                "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));
    }
}
