package com.unis.sxzcl.ui.house;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.unis.model.bean.DictBean;
import com.unis.model.bean.HouseBean;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentHouseListBinding;
import com.unis.sxzcl.widget.menu.MenuListView;
import com.unis.sxzcl.widget.menu.MenuMultipleListView;
import com.unis.sxzcl.widget.menu.SimpleDownModel;
import com.xyxsbj.mylibrary.tool.LogTool;

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

/**
 * 包名: com.unis.sxzcl.ui.house
 * 类名: HouseListFragment
 * 作用描述: 仓房列表页面
 * 创建人: mQ
 * 创建时间: 2020/11/26 9:39
 * 更新人:
 * 更新时间: 2020/11/26 9:39
 * 更新说明:
 * 版本号: 1.0
 */
public class HouseListFragment extends BaseListFragment<FragmentHouseListBinding> implements MenuListView.MenuListCallBack, MenuMultipleListView.Callback, OnItemClickListener {

    private HouseListAdapter mAdapter;
    private MenuMultipleListView mHouseMenu;
    private MenuListView mHouseStatusMenu;
    private MenuListView mStorageMenu;
    private MenuListView mFoodTypeMenu;

    private String[] mLabels = new String[]{"全部仓房", "仓房状态", "储存状态", "全部品种"};
    private List<HouseBean> mHouseList;
    private List<DictBean> mHouseStatusList;
    private List<DictBean> mStorageList;
    private List<DictBean> mFoodTypeList;
    private boolean mShowTitle;

    public static HouseListFragment newFragmentNoTitle() {
        Bundle bundle = new Bundle();
        bundle.putBoolean("showTitle", false);
        HouseListFragment fragment = new HouseListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

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

    @Override
    public void onMoreData() {
        super.onMoreData();
        getData(false);
    }

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

    @Override
    protected void init() {
        Bundle bundle = getArguments();
        mShowTitle = bundle.getBoolean("showTitle", true);
        LogTool.i("showTitle:" + mShowTitle);
        if (mShowTitle) {
            initTitle();
        } else {
            mBinding.bar.setVisibility(View.GONE);
            mBinding.simpleMenu.setVisibility(View.GONE);
        }

        mAdapter = new HouseListAdapter();
        mAdapter.setOnItemClickListener(this);
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, true);
    }

    /**
     * 初始化标题 和 筛选控件
     */
    private void initTitle() {

        mBinding.bar.setTitle("仓房信息")
                .setLeftClickListener(v -> onBack());

        //初始化筛选菜单
        List<SimpleDownModel> modelList = new ArrayList<>();
        for (String label : mLabels) modelList.add(new SimpleDownModel(label));
        //初始化筛选弹出框
        mHouseMenu = new MenuMultipleListView(getContext()).setCallback(this);
        mHouseStatusMenu = new MenuListView(getContext()).setOnItemClickListener(this);
        mStorageMenu = new MenuListView(getContext()).setOnItemClickListener(this);
        mFoodTypeMenu = new MenuListView(getContext()).setOnItemClickListener(this);
        //添加筛选控件
        mBinding.simpleMenu.initMenu(modelList,
                mHouseMenu.getView(),
                mHouseStatusMenu.getView(),
                mStorageMenu.getView(),
                mFoodTypeMenu.getView());
    }

    @Override
    protected void onFirstShow() {
        if (mShowTitle)
            getMenuData();

        onRefresh();
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        HouseBean item = mAdapter.getData().get(position);
        String json = getGson().toJson(item);
        Bundle bundle = new Bundle();
        bundle.putString("data", json);
        navigation(R.id.houseDetailFragment, bundle);
    }

    private void getMenuData() {
        //仓房列表
//        BasicModel.get().getHouseList(new DataCallback<List<HouseBean>>() {
//            @Override
//            public void onSuccess(List<HouseBean> data) {
//                if (!isActive())return;
//
//                mHouseList = data;
//                for (HouseBean item : data) {
//                    mHouseMenu.addData(item.houseName);
//                }
//            }
//        });

        //仓房状态
//        BasicModel.get().getHouseStatusList(new DataCallback<List<DictBean>>() {
//            @Override
//            public void onSuccess(List<DictBean> data) {
//                if (!isActive())return;
//
//                mHouseStatusList = data;
//                mHouseStatusMenu.addData("全部");
//                for (DictBean item : data) {
//                    mHouseStatusMenu.addData(item.name);
//                }
//            }
//        });
//
//        //储存状态
//        BasicModel.get().getStorageList(new DataCallback<List<DictBean>>() {
//            @Override
//            public void onSuccess(List<DictBean> data) {
//                if (!isActive())return;
//
//                mStorageList = data;
//                mStorageMenu.addData("全部");
//                for (DictBean item : data) {
//                    mStorageMenu.addData(item.name);
//                }
//            }
//        });
//
//        //粮食品种
//        BasicModel.get().getFoodTypeList(new DataCallback<List<DictBean>>() {
//            @Override
//            public void onSuccess(List<DictBean> data) {
//                if (!isActive())return;
//
//                mFoodTypeList = data;
//                mFoodTypeMenu.addData("全部");
//                for (DictBean item : data) {
//                    mFoodTypeMenu.addData(item.name);
//                }
//            }
//        });
    }


    private void getData(boolean refresh) {
//        new Handler().postDelayed(() -> {
//            if (refresh) {
//                mAdapter.getData().clear();
//                mAdapter.notifyDataSetChanged();
//            }
//            BasicModel.get().getHouseList(new DataCallback<List<HouseBean>>() {
//                @Override
//                public void onSuccess(List<HouseBean> data) {
//                    if (!isActive())return;
//
//                    mAdapter.addData(data);
                    loadEnd();
//                }
//
//                @Override
//                public void onError(String error) {
//                    super.onError(error);
//                    if (!isActive())return;
//                    loadError();
//                }
//            });
//        }, 500);
    }

    @Override
    public void onMenuItemClickListener(BaseQuickAdapter adapter, View view, int position, RecyclerView recyclerView) {
//        mSimpleMemu.closeMenu(position == 0 ? headers[0] : barns.get(position - 1).getStorehouseName());
//        houseId = position == 0 ? "" : barns.get(position - 1).getStorehouseId() + "";
//        onRefreshDataTask();

        if (recyclerView == mHouseStatusMenu.getView()) {
            mBinding.simpleMenu.closeMenu(position == 0 ? mLabels[1] : mHouseStatusList.get(position - 1).name);
        } else if (recyclerView == mStorageMenu.getView()) {
            mBinding.simpleMenu.closeMenu(position == 0 ? mLabels[2] : mStorageList.get(position - 1).name);
        } else if (recyclerView == mFoodTypeMenu.getView()) {
            mBinding.simpleMenu.closeMenu(position == 0 ? mLabels[3] : mFoodTypeList.get(position - 1).name);
        }
        onRefresh();
    }

    @Override
    public void onMenuMultipleListener(View root, Set<Integer> positions) {
        if (positions.size() == 0) {
            mBinding.simpleMenu.closeMenu(mLabels[0]);
        } else {
            StringBuilder sb = new StringBuilder();
            for (Integer index : positions) {
//                sb.append(mHouseList.get(index).houseName).append(",");
            }
            mBinding.simpleMenu.closeMenu(sb.toString());
        }
        onRefresh();
    }

}
