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

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.ui.adapter.AddCrucibleCarAdapter;
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 AddCrucibleCarActivity 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 AddCrucibleCarAdapter 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 AddCrucibleCarAdapter(R.layout.item_add_crucible_double, 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(AddCrucibleCarActivity.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.getCarDoubleNum() + "", recordsBean.getInternalPrice()+"", 2), 2);
        }
//        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);
        startActivity(AddCrucibleConfirmActivity.class,
                "pro", JSON.toJSONString(productList),
                "launchOrderAmount", allPrice,
                "stockId", getIntent().getStringExtra("stockId"),
                "procedureOrderId", getIntent().getStringExtra("procedureOrderId"),
                "customerId", getIntent().getStringExtra("customerId"),
                "code", getIntent().getStringExtra("code"));
    }

    @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.getCarDoubleNum() + "", recordsBean.getInternalPrice()+"", 2), 2);
        }
        tvPrice.setText("总计: ¥ ".concat(allPrice));
    }
}
