package com.wd.workoffice.ui.fg;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;
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.MsgBean;
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.NoticeActivity;
import com.wd.workoffice.ui.activity.bat.approval.ApprovalFromMeActivity;
import com.wd.workoffice.ui.activity.bat.approval.ApprovalToMeActivity;
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.store.StoreCrucibleActivity;
import com.wd.workoffice.ui.activity.bat.store.StoreFinancialAddActivity;
import com.wd.workoffice.ui.activity.bat.store.StoreOutsourcingActivity;
import com.wd.workoffice.ui.activity.bat.store.StoreSaleActivity;
import com.wd.workoffice.ui.activity.bat.store.StoreSupplierActivity;
import com.wd.workoffice.ui.activity.bat.store.StoreTradingActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkAccountActivity;
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.ui.adapter.VpDataAdapter;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
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 MainFragment extends WorkBaseFg {
    @BindView(R.id.tv_store)
    TextView tvStore;
    @BindView(R.id.rv_store)
    RecyclerView rvStore;
    @BindView(R.id.rv_sale)
    RecyclerView rvSale;
    @BindView(R.id.rv_product)
    RecyclerView rvProduct;
    @BindView(R.id.rv_approval)
    RecyclerView rvApproval;
    @BindView(R.id.rv_work)
    RecyclerView rvWork;
//    @BindView(R.id.srl_refresh)
//    SmartRefreshLayout srlRefresh;
    List<BatLayoutVo> saleList = new ArrayList<>();
    List<BatLayoutVo> storeList = new ArrayList<>();
    List<BatLayoutVo> approvalList = new ArrayList<>();
    List<BatLayoutVo> workList = new ArrayList<>();
    List<BatLayoutVo> productList = new ArrayList<>();
    int[] saleImg = new int[]{R.mipmap.bat_sale1, R.mipmap.bat_sale2, R.mipmap.bat_sale3, R.mipmap.bat_sale4, R.mipmap.bat_sale5, R.mipmap.bat_sale6};
    String[] saleName = new String[]{"销售订单", "外采购订单", "供应订单", "内部订单", "组装订单", "坩埚订单"};
    int[] storeImg = new int[]{R.mipmap.bat_store1, R.mipmap.bat_store2, R.mipmap.bat_store3, R.mipmap.bat_store4
            , R.mipmap.bat_store5, R.mipmap.bat_store6, R.mipmap.bat_store7};
    String[] storeName = new String[]{"销售", "外采购", "供应", "维修", "财税", "钳锅", "内部交易"};
    int[] approvalImg = new int[]{R.mipmap.bat_approval1, R.mipmap.bat_approval2};
    String[] approvalName = new String[]{"我审批的", "我发起的"};
    int[] workImg = new int[]{R.mipmap.bat_work1, R.mipmap.bat_work2, R.mipmap.bat_work3,
            R.mipmap.bat_work4, R.mipmap.bat_work5, R.mipmap.bat_work6, R.mipmap.bat_work7, R.mipmap.bat_work8, R.mipmap.bat_work9};
    String[] workName = new String[]{"产品", "往来", "部门账户", "仓库", "部门", "财务中心", "设备", "报表", "库存类别"};
    int[] productImg = new int[]{R.mipmap.bat_product1, R.mipmap.bat_product2, R.mipmap.bat_product3, R.mipmap.bat_product4, R.mipmap.bat_product5, R.mipmap.bat_product6};
    String[] productName = new String[]{"销售订单", "外采购订单", "供应订单", "内部订单", "组装订单", "坩埚订单"};
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.tv_notice)
    TextView tvNotice;
    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.vp_data)
    ViewPager vpData;
    private String tab[] = {
            "销售商店", "外采购商店", "供应商店", "财税商店",  "仓单交易"};
    private String status[] = {
            "1", "2", "3", "4", "6"};
    private FragmentManager sfm;

    @Override
    protected void initView() {
        ButterKnife.bind(this, getContentView());
        addDate(saleList, saleImg, saleName);
        addDate(storeList, storeImg, storeName);
        addDate(approvalList, approvalImg, approvalName);
        addDate(workList, workImg, workName);
        addDate(productList, productImg, productName);
        initRvLayout(rvSale, saleList);
        initRvLayout(rvStore, storeList);
        initRvLayout(rvApproval, approvalList);
        initRvLayout(rvWork, workList);
        initRvLayout(rvProduct, productList);
        tlTab.setupWithViewPager(vpData);
        sfm = getChildFragmentManager();
        List<Fragment> data = new ArrayList<>();
        for (int i = 0; i < tab.length; i++) {
            MainListFragment saleFragment = new MainListFragment();
            Bundle bundle = new Bundle();
            bundle.putString("type", status[i]);
            saleFragment.setArguments(bundle);
            data.add(saleFragment);
        }
        VpDataAdapter vpFgAdapter = new VpDataAdapter(sfm, data);
        vpData.setAdapter(vpFgAdapter);
        for (int i = 0; i < tab.length; i++) {
            tlTab.getTabAt(i).setText(tab[i]);
        }

    }

    @Override
    public void onStart() {
        super.onStart();
    }
//    @Nullable
//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        sex = getArguments().getString("sex");
//        return super.onCreateView(inflater, container, savedInstanceState);
//    }

    @Override
    protected void initData() {
        RtfUtils.getRtf().userInfo().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;
                }
                UserKeeper.getInstance().keepUserInfo(data.getData().toString());
            }
        });
        Map<String, Object> param = WorkUtils.pageKey();
        param.put("status", 1);
        RtfUtils.getRtf().notice(param).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                List<MsgBean.RecordsBean> getList = JSON.parseObject(data.getData().toString(), MsgBean.class).getRecords();
                if (getList != null && getList.size() != 0) {
                    MsgBean.RecordsBean recordsBean = getList.get(0);
                    tvNotice.setText(recordsBean.getTitle());
                }
            }
        });
    }


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

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

    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);
        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_store:
                batLayoutAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (position) {
                            case 0:
                                if (!WorkUtils.hasPermission(PagePermissionType.SALE_ORDER_START.getPermission())) {
                                    toast(R.string.permission);
                                    return;
                                }
                                startActivity(StoreSaleActivity.class);
                                break;
                            case 1:
                                if (!WorkUtils.hasPermission(PagePermissionType.PO_PURCHASE_START.getPermission())) {
                                    toast(R.string.permission);
                                    return;
                                }
                                startActivity(StoreOutsourcingActivity.class);
                                break;
                            case 2:
                                if (!WorkUtils.hasPermission(PagePermissionType.FO_SUPPLIER_START.getPermission())) {
                                    toast(R.string.permission);
                                    return;
                                }
                                startActivity(StoreSupplierActivity.class);
                                break;
                            case 3:
                                toast("暂无数据");
                                break;
                            case 4:
                                if (!WorkUtils.hasPermission(PagePermissionType.FINANCE_TAX.getPermission())) {
                                    toast(R.string.permission);
                                    return;
                                }
                                startActivity(StoreFinancialAddActivity.class);
                                break;
                            case 5:
                                if (!WorkUtils.hasPermission(PagePermissionType.CO_CRUCIBLE_START.getPermission())) {
                                    toast(R.string.permission);
                                    return;
                                }
                                startActivity(StoreCrucibleActivity.class);
                                break;
                            case 6:
                                if (!WorkUtils.hasPermission(PagePermissionType.IO_INSIDE_START.getPermission())) {
                                    toast(R.string.permission);
                                    return;
                                }
                                startActivity(StoreTradingActivity.class);
                                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;
            case R.id.rv_work:
                batLayoutAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (position) {
                            case 0:
                                startActivity(WorkProActivity.class);
                                break;
                            case 1:
                                startActivity(WorkContactsActivity.class);
                                break;
                            case 2:
                                startActivity(WorkAccountActivity.class);
                                break;
                            case 3:
                                startActivity(WorkWarehouseActivity.class);
                                break;
                            case 4:
                                startActivity(WorkDepActivity.class);
                                break;
                            case 5:
                                startActivity(WorkFinancialActivity.class);
                                break;
                            case 6:
                                toast("暂无数据");
                                break;
                            case 7:
                                toast("暂无数据");
                                break;
                            case 8:
                                startActivity(WorkInStockActivity.class);
                                break;
                        }
                    }
                });
                break;
            case R.id.rv_approval:
                batLayoutAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (position) {
                            case 0:
                                startActivity(ApprovalToMeActivity.class);
                                break;
                            case 1:
                                startActivity(ApprovalFromMeActivity.class);
                                break;
                        }
                    }
                });
                break;
        }
    }

    @OnClick(R.id.tv_more)
    public void onViewClicked() {
        startActivity(NoticeActivity.class);
    }
}
