package com.wd.workoffice.ui.fg;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
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.DepBean;
import com.wd.workoffice.bean.OrderBean;
import com.wd.workoffice.bean.event.CheckOrderEvent;
import com.wd.workoffice.bean.workEnum.OrderDeptPermission;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.bean.workEnum.PermissionType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.order.CrucibleOrderDetailActivity;
import com.wd.workoffice.ui.activity.bat.store.ChooseInfoActivity;
import com.wd.workoffice.ui.activity.bat.store.ChooseInsideActivity;
import com.wd.workoffice.ui.activity.bat.store.StoreFinancialAddActivity;
import com.wd.workoffice.ui.adapter.DepAdapter;
import com.wd.workoffice.ui.adapter.SaleCruibleAdapter;
import com.wd.workoffice.util.WorkUtils;

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

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

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
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 MainListFragment extends WorkBaseFg {

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private String type;
    private DepAdapter dataAdapter;
    private List<DepBean> dataList;
    String permission;

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


    @Override
    protected void initData() {
        switch (type) {
            case "1":
                permission = PermissionType.ES_SELL.getCode();
                break;
            case "2":
                permission = PermissionType.PURCHASE_SELL.getCode();
                break;
            case "3":
                permission = PermissionType.SUPPLY_SELL.getCode();
                break;
            case "4":
                break;
            case "5":
                permission = PermissionType.CRUCIBLE_SELL.getCode();
                break;
            case "6":
                permission = PermissionType.INSIDE_SELL.getCode();
                break;
        }
        dataList = new ArrayList<>();
        dataAdapter = new DepAdapter(R.layout.item_main_dep, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        dataAdapter.notifyDataSetChanged();
        if (!TextUtils.equals("4", type)) {
            getData();
        } else {
            DepBean depBean = new DepBean();
            depBean.setName("申请财税");
            dataList.add(depBean);
            dataAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            type = arguments.getString("type");
        }
    }

    private void getData() {
        RtfUtils.getRtf().getDepByPermission(permission).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<DepBean> getList = JSON.parseArray(data.getData().toString(), DepBean.class);
                dataList.clear();
                dataList.addAll(getList);
                dataAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initEvent() {
        dataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (type) {
                    case "1":
                        permission = PermissionType.ES_SELL.getCode();
                        if (!WorkUtils.hasOrderDeptPermission(OrderDeptPermission.SALE.getCode()) || !WorkUtils.hasPermission(PagePermissionType.SALE_ORDER_START.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        break;
                    case "2":
                        if (!WorkUtils.hasOrderDeptPermission(OrderDeptPermission.OUT_BUY.getCode()) ||!WorkUtils.hasPermission(PagePermissionType.PO_PURCHASE_START.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        permission = PermissionType.PURCHASE_SELL.getCode();
                        break;
                    case "3":
                        if (!WorkUtils.hasOrderDeptPermission(OrderDeptPermission.SUPPLIER.getCode()) ||!WorkUtils.hasPermission(PagePermissionType.FO_SUPPLIER_START.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        permission = PermissionType.SUPPLY_SELL.getCode();
                        break;
                    case "4":
                        if (!WorkUtils.hasOrderDeptPermission(OrderDeptPermission.FISCAL_TAX_BUY.getCode()) ||!WorkUtils.hasPermission(PagePermissionType.FINANCE_TAX.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        break;
                    case "5":
                        if (!WorkUtils.hasOrderDeptPermission(OrderDeptPermission.CRUCIBLE.getCode()) ||!WorkUtils.hasPermission(PagePermissionType.CO_CRUCIBLE_START.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        permission = PermissionType.CRUCIBLE_SELL.getCode();
                        break;
                    case "6":
                        if (!WorkUtils.hasOrderDeptPermission(OrderDeptPermission.IN.getCode()) ||!WorkUtils.hasPermission(PagePermissionType.IO_INSIDE_START.getPermission())) {
                            toast(R.string.permission);
                            return;
                        }
                        permission = PermissionType.INSIDE_SELL.getCode();
                        break;
                }
                if (TextUtils.equals("4", type)) {
                    startActivity(StoreFinancialAddActivity.class);
                    return;
                }
                if (TextUtils.equals("6", type)) {
                    startActivity(ChooseInsideActivity.class, "code", permission,
                            "type", type, "deptId", dataList.get(position).getId() + "");
                    return;
                }
                startActivity(ChooseInfoActivity.class, "code", permission,
                        "type", type, "deptId", dataList.get(position).getId() + "");
            }
        });
    }


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

}
