package com.wd.workoffice.ui.fg;

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkBaseFg;
import com.wd.workoffice.bean.vo.BatLayoutVo;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.WorkOrderActivity;
import com.wd.workoffice.ui.activity.bat.approval.ApprovalDealActivity;
import com.wd.workoffice.ui.activity.bat.approval.WorkApprovalActivity;
import com.wd.workoffice.ui.activity.bat.order.product.ProductInsideOrderActivity;
import com.wd.workoffice.ui.activity.bat.order.product.ProductOutOrderActivity;
import com.wd.workoffice.ui.activity.bat.order.product.ProductSaleOrderActivity;
import com.wd.workoffice.ui.activity.bat.order.product.ProductSupplierOrderActivity;
import com.wd.workoffice.ui.activity.bat.order.sale.SaleCrucibleOrderActivity;
import com.wd.workoffice.ui.activity.bat.order.sale.SaleInsideOrderActivity;
import com.wd.workoffice.ui.activity.bat.order.sale.SaleOutOrderActivity;
import com.wd.workoffice.ui.activity.bat.order.sale.SaleProductOrderActivity;
import com.wd.workoffice.ui.activity.bat.order.sale.SaleSaleOrderActivity;
import com.wd.workoffice.ui.activity.bat.order.sale.SaleSupplierOrderActivity;
import com.wd.workoffice.ui.activity.bat.work.DeviceActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkContactsActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkDepActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkFinancialActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkInStockActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkProActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkWarehouseActivity;
import com.wd.workoffice.ui.adapter.BatLayoutAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
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 WorkFragment extends WorkBaseFg {
    @BindView(R.id.rv_function)
    RecyclerView rvFunction;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    @BindView(R.id.ll_submit)
    LinearLayout llSubmit;
    @BindView(R.id.ll_not_deal)
    LinearLayout llNotDeal;
    @BindView(R.id.ll_deal)
    LinearLayout llDeal;
    @BindView(R.id.tv_submit_num)
    TextView tvSubmitNum;
    @BindView(R.id.tv_not_deal_num)
    TextView tvNotDealNum;
    List<BatLayoutVo> workList = new ArrayList<>();
    int[] workImg = new int[]{R.mipmap.work_icon1, R.mipmap.work_icon2,
//            R.mipmap.work_icon3,
            R.mipmap.work_icon4, R.mipmap.work_icon5, R.mipmap.work_icon6, R.mipmap.work_icon7,
//            R.mipmap.work_icon8, R.mipmap.work_icon9,
            R.mipmap.work_icon10,
//            R.mipmap.work_icon11
    };
    String[] workName = new String[]{"产品", "往来",
//            "部门账户",
            "仓库", "部门", "财务中心", "设备",
//            "流程", "报表",
            "库存类别"};
    List<BatLayoutVo> saleList = new ArrayList<>();
    List<BatLayoutVo> productList = new ArrayList<>();
    int[] productImg = new int[]{R.mipmap.order_sale1, R.mipmap.order_sale2, R.mipmap.order_sale3, R.mipmap.order_sale4, R.mipmap.order_sale5, R.mipmap.order_sale6};
    String[] saleName = new String[]{"销售订单", "外采购订单", "供应订单", "内部订单", "组装订单", "坩埚订单"};
    int[] saleImg = new int[]{R.mipmap.order_product1, R.mipmap.order_product2, R.mipmap.order_product3, R.mipmap.order_product4, R.mipmap.order_product5, R.mipmap.order_product6};
    String[] productName = new String[]{"销售订单", "外采购订单", "供应订单", "内部订单", "组装订单", "坩埚订单"};
    @BindView(R.id.rv_sale)
    RecyclerView rvSale;
    @BindView(R.id.rv_product)
    RecyclerView rvProduct;

    @Override
    protected void initView() {
        ButterKnife.bind(this, getContentView());
        addDate(workList, workImg, workName);
        initRvLayout(rvFunction, workList);
        addDate(saleList, saleImg, saleName);
        addDate(productList, productImg, productName);
        initOrderRvLayout(rvSale, saleList);
        initOrderRvLayout(rvProduct, productList);
    }

    @Override
    protected void initData() {
        getNum();
    }

    private void getNum() {
        RtfUtils.getRtf().approvalCount().compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
            }

            @Override
            public void onSuccess(BaseBean data) {
                if (data.getCode() != 0) {
                    return;
                }
                JSONObject dataJson = JSON.parseObject(data.getData().toString());
                tvNotDealNum.setVisibility(dataJson.getIntValue("currentUserCanAudit") == 0 ? View.GONE : View.VISIBLE);
                tvSubmitNum.setVisibility(dataJson.getIntValue("otherUserCanAudit") == 0 ? View.GONE : View.VISIBLE);
                tvNotDealNum.setText(getNumValue(dataJson.getIntValue("currentUserCanAudit")));
                tvSubmitNum.setText(getNumValue(dataJson.getIntValue("otherUserCanAudit")));
            }
        });
    }

    private String getNumValue(int value) {
        return value > 99 ? "..." : String.valueOf(value);
    }


    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh();
        });
    }

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

    private void addDate(List<BatLayoutVo> saleList, int[] saleImg, String[] saleName) {
        for (int i = 0; i < saleImg.length; i++) {
            BatLayoutVo batLayoutVo = new BatLayoutVo();
            batLayoutVo.setImg(saleImg[i]);
            batLayoutVo.setName(saleName[i]);
            saleList.add(batLayoutVo);
        }

    }

    /**
     * 初始化布局
     *
     * @param rvSale
     * @param saleList
     */
    private void initRvLayout(RecyclerView rvSale, List<BatLayoutVo> saleList) {
        rvSale.setLayoutManager(new GridLayoutManager(getActivity(), 4, RecyclerView.VERTICAL,
                false));
        BatLayoutAdapter batLayoutAdapter = new BatLayoutAdapter(R.layout.item_bat_layout, saleList);
        batLayoutAdapter.bindToRecyclerView(rvSale);
        batLayoutAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                String[] workName = new String[]{"产品", "往来",
////            "部门账户",
//                        "仓库", "部门", "财务中心", "设备",
////            "流程", "报表",
//                        "库存类别", "订单"};
                switch (position) {
                    case 0:
                        if (!WorkUtils.hasPermission(PagePermissionType.PRODUCT.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        startActivity(WorkProActivity.class);
                        break;
                    case 1:
                        if (!WorkUtils.hasPermission(PagePermissionType.TRANSIT_PART_MANAGE_LIST.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        startActivity(WorkContactsActivity.class);
                        break;
//                    case 2:
//                        startActivity(WorkAccountActivity.class);
//                        break;
                    case 2:
                        if (!WorkUtils.hasPermission(PagePermissionType.STOCK_PAGE.getPermission())
                                && !WorkUtils.hasPermission(PagePermissionType.STOCK_QUANTITY_ORDER_PAGE.getPermission()) &&
                                !WorkUtils.hasPermission(PagePermissionType.STOCK_QUANTITY_PAGE.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        startActivity(WorkWarehouseActivity.class);
                        break;
                    case 3:
                        if (!WorkUtils.hasPermission(PagePermissionType.COMMON.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        startActivity(WorkDepActivity.class);
                        break;
                    case 4:
                        startActivity(WorkFinancialActivity.class);
                        break;
                    case 5:
                        startActivity(DeviceActivity.class);
                        break;
//                    case 7:
//                        toast("暂无数据");
//                        break;
//                    case 8:
//                        toast("暂无数据");
//                        break;
                    case 6:
                        if (!WorkUtils.hasPermission(PagePermissionType.STOCK_TYPE_LIST.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        startActivity(WorkInStockActivity.class);
                        break;
                    case 7:
                        startActivity(WorkOrderActivity.class);
                        break;
                }
            }
        });
    }

    @OnClick({R.id.ll_submit, R.id.ll_deal, R.id.ll_not_deal})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_submit:
                startActivity(WorkApprovalActivity.class, "type", "1", "status", "0", "title", "我发起的");
                break;
            case R.id.ll_deal:
                startActivity(ApprovalDealActivity.class);
                break;
            case R.id.ll_not_deal:
                startActivity(WorkApprovalActivity.class, "type", "2", "status", "0", "title", "待我审批");
                break;
        }
    }


    /**
     * 初始化布局
     *
     * @param rvSale
     * @param saleList
     */
    private void initOrderRvLayout(RecyclerView rvSale, List<BatLayoutVo> saleList) {
        rvSale.setLayoutManager(new GridLayoutManager(getActivity(), 4, RecyclerView.VERTICAL,
                false));
        BatLayoutAdapter batLayoutAdapter = new BatLayoutAdapter(R.layout.item_bat_layout, saleList);
        batLayoutAdapter.bindToRecyclerView(rvSale);
        switch (rvSale.getId()) {
            case R.id.rv_sale:
                batLayoutAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (position) {
                            case 0:
                                startActivity(SaleSaleOrderActivity.class, "identity", "1");
                                break;
                            case 1:
                                startActivity(SaleOutOrderActivity.class, "identity", "1");
                                break;
                            case 2:
                                startActivity(SaleSupplierOrderActivity.class, "identity", "1");
                                break;
                            case 3:
                                startActivity(SaleInsideOrderActivity.class, "identity", "1");
                                break;
                            case 4:
                                startActivity(SaleProductOrderActivity.class, "identity", "1");
                                break;
                            case 5:
                                startActivity(SaleCrucibleOrderActivity.class, "identity", "1");
                                break;
                        }
                    }
                });
                break;
            case R.id.rv_product:
                batLayoutAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (position) {
                            case 0:
                                startActivity(ProductSaleOrderActivity.class);
                                break;
                            case 1:
                                startActivity(ProductOutOrderActivity.class);
                                break;
                            case 2:
                                startActivity(ProductSupplierOrderActivity.class);
                                break;
                            case 3:
                                startActivity(ProductInsideOrderActivity.class);
                                break;
                            case 4:
                                startActivity(SaleProductOrderActivity.class, "identity", "2");
                                break;
                            case 5:
                                startActivity(SaleCrucibleOrderActivity.class, "identity", "2");
                                break;
                        }
                    }
                });
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getNum();
    }
}
