package com.unis.sxzcl.ui.house;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.unis.model.BasicModel;
import com.unis.model.DataCallback;
import com.unis.model.HouseModel;
import com.unis.model.bean.EnumBean;
import com.unis.model.bean.HouseBean;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.base.base_detail.BaseDetail;
import com.unis.sxzcl.base.base_detail.BaseDetailAdapter;
import com.unis.sxzcl.base.base_detail.BaseDetailBean;
import com.unis.sxzcl.databinding.FragmentBaseListBinding;
import com.unis.sxzcl.widget.BottomMultipleListDialog;
import com.unis.sxzcl.widget.MultipleBean;
import com.xyxsbj.mylibrary.tool.ToastTool;

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

/**
 * 包名: com.unis.sxzcl.ui.house
 * 类名: HouseDetailFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/2 14:36
 * 更新人:
 * 更新时间: 2020/12/2 14:36
 * 更新说明:
 * 版本号: 1.0
 */
public class HouseDetailFragment extends BaseListFragment<FragmentBaseListBinding> {

    private BottomMultipleListDialog mMultipleListDialog;

    private int mFoodTypeIndex = -1;
    private int mHouseStatusIndex = -1;
    private int mStorageIndex = -1;
    private BaseDetailAdapter mAdapter;

    private final String T_FOOD_TYPE = "粮食品种";
    private final String T_HOUSE_STATUS = "仓房状态";
    private final String T_STORAGE = "存储状态";
    private HouseBean mHouse;

    @Override
    protected FragmentBaseListBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentBaseListBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        Bundle bundle = getArguments();

        mAdapter = new BaseDetailAdapter();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            BaseDetailBean item = mAdapter.getData().get(position);
            String title = item.getTitle();
            switch (title) {
                case T_FOOD_TYPE:
                    getFootType(title);
                    break;
                case T_HOUSE_STATUS:
                    getHouseStatus(title);
                    break;
                case T_STORAGE:
                    getStorage(title);
                    break;
            }
        });
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, false);
    }

    @Override
    protected void onFirstShow() {
//        mAdapter.addData(new BaseDetailBean(BaseDetail.TITLE_CONTENT, T_FOOD_TYPE, "小麦").setDownIcon(true));
//        mAdapter.addData(new BaseDetailBean(BaseDetail.TITLE_CONTENT, T_HOUSE_STATUS, "满仓").setDownIcon(true));
//        mAdapter.addData(new BaseDetailBean(BaseDetail.TITLE_CONTENT, T_STORAGE, "通风").setDownIcon(true));

        onRefresh();

    }

    @Override
    public void onRefreshData() {
        getData();
    }

    private void getData() {
        String houseId = HouseModel.get().getSelectHouse().houseCode;
        HouseModel.get().getHouseDetail(houseId, new DataCallback<HouseBean>() {
            @Override
            public void onSuccess(HouseBean data) {
                if (!isActive()) return;
                if (data != null) {
                    mHouse = data;
                    List<BaseDetailBean> list = new ArrayList<>();
                    list.add(new BaseDetailBean(BaseDetail.TITLE_CONTENT, T_FOOD_TYPE,
                            BasicModel.get().getEnumName(data.getGrainVarieties())).setDownIcon(true));

                    list.add(new BaseDetailBean(BaseDetail.TITLE_CONTENT, T_HOUSE_STATUS,
                            BasicModel.get().getEnumName(data.getStorageStatus())).setDownIcon(true));

                    list.add(new BaseDetailBean(BaseDetail.TITLE_CONTENT, T_STORAGE,
                            BasicModel.get().getEnumName(data.getWarehouseStatus())).setDownIcon(true));
                    mAdapter.setList(list);
                }
                loadComplete();
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                if (!isActive()) return;
                loadError();
            }
        });
    }


    /**
     * 获取粮食品种信息 弹出下拉选项
     */
    private void getFootType(String title) {
        BasicModel.get().getFoodTypeList(new DataCallback<List<EnumBean>>() {
            @Override
            public void onSuccess(List<EnumBean> data) {
                List<MultipleBean> list = new ArrayList<>();
                for (int i = 0; i < data.size(); i++) {
                    EnumBean item = data.get(i);
                    list.add(new MultipleBean(item.getEnumName(), item.getEnumId()));
                }
                showBottomDialog(title, mFoodTypeIndex, list);
            }
        });
    }

    /**
     * 获取仓房状态 弹出下拉选项
     */
    private void getHouseStatus(String title) {
        BasicModel.get().getHouseStatusList(new DataCallback<List<EnumBean>>() {
            @Override
            public void onSuccess(List<EnumBean> data) {
                if (!isActive()) return;

                List<MultipleBean> list = new ArrayList<>();
                for (int i = 0; i < data.size(); i++) {
                    EnumBean item = data.get(i);
                    list.add(new MultipleBean(item.getEnumName(), item.getEnumId()));
                }
                showBottomDialog(title, mHouseStatusIndex, list);
            }
        });
    }

    /**
     * 获取储存状态 弹出下拉选项
     */
    private void getStorage(String title) {

        BasicModel.get().getStorageList(new DataCallback<List<EnumBean>>() {
            @Override
            public void onSuccess(List<EnumBean> data) {
                if (!isActive()) return;
                String content = mAdapter.getData().get(0).getContent();
                List<MultipleBean> list = new ArrayList<>();
                for (int i = 0; i < data.size(); i++) {
                    EnumBean item = data.get(i);
                    list.add(new MultipleBean(item.getEnumName(), item.getEnumId()));
                    if (content != null && content.equals(item.getEnumName()))
                        mStorageIndex = i;
                }
                showBottomDialog(title, mStorageIndex, list);
            }
        });
    }

    public void showBottomDialog(String title, int select, List<MultipleBean> list) {
        if (mMultipleListDialog == null) {
            mMultipleListDialog = new BottomMultipleListDialog(getContext())
                    .hideTwoList()
                    .hideThreeList()
                    .setEnterCallback((v, b1, b2, b3) -> {
                        if (b1 == null) {
                            ToastTool.showLong("请先选择");
                            return false;
                        }
                        int index = Integer.parseInt(b1.getExtras());
                        String name = b1.getName();
                        String id = b1.getExtras();
                        switch (mMultipleListDialog.getTitle()) {
                            case T_FOOD_TYPE:
                                mAdapter.getData().get(0).setContent(name);
                                mAdapter.notifyItemChanged(0);
                                mFoodTypeIndex = index;
                                mHouse.setGrainVarieties(id);
                                update();
                                break;
                            case T_HOUSE_STATUS:
                                mAdapter.getData().get(1).setContent(name);
                                mAdapter.notifyItemChanged(1);
                                mHouseStatusIndex = index;
                                mHouse.setStorageStatus(id);
                                update();
                                break;
                            case T_STORAGE:
                                mAdapter.getData().get(2).setContent(name);
                                mAdapter.notifyItemChanged(2);
                                mStorageIndex = index;
                                mHouse.setWarehouseStatus(id);
                                update();
                                break;
                        }
                        return true;
                    });
        }
        mMultipleListDialog
                .setListOne(list)
                .setSelectOne(select)
                .setTitle(title)
                .show();
    }


    /**
     * 修改仓房信息
     */
    public void update() {
        showProgress("正在修改...", false);
        HouseModel.get().update(mHouse, new DataCallback<String>() {
            @Override
            public void onSuccess(String data) {
                if (!isActive()) return;
                hideProgress();
                ToastTool.showLong("修改成功");
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                if (!isActive()) return;
                hideProgress();
            }
        });
    }

}
