package com.unis.sxzcl.ui.company;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.unis.model.DataCallback;
import com.unis.model.DepotModel;
import com.unis.model.bean.MultiDepotBean;
import com.unis.model.event.SelectDepotEvent;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentBaseListTitleBinding;
import com.unis.sxzcl.widget.multi_list.BaseMultiBean;
import com.unis.sxzcl.widget.multi_list.MultiListAdapter;

import org.greenrobot.eventbus.EventBus;

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

/**
 * 包名: com.unis.sxzcl.ui.company
 * 类名: CompanyListFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/11/25 21:02
 * 更新人:
 * 更新时间: 2020/11/25 21:02
 * 更新说明:
 * 版本号: 1.0
 */
public class CompanyListFragment extends BaseListFragment<FragmentBaseListTitleBinding> implements MultiListAdapter.Callback {

    private MultiListAdapter mAdapter;


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

    @Override
    protected void init() {
        mBinding.bar.setTitle("中储粮山西分公司")
                .setLeftClickListener(v -> onBack());

        mAdapter = new MultiListAdapter(getContext());
        mAdapter.setCallback(this);
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, false);
    }

    @Override
    protected void onFirstShow() {
        onRefresh();
    }

    @Override
    public void onRefreshData() {

        DepotModel.get().getMultiList(new DataCallback<List<MultiDepotBean>>() {
            @Override
            public void onSuccess(List<MultiDepotBean> data) {
                if (!isActive()) return;
                List<BaseNode> list = new ArrayList<>();
                int level = 0;
                initListData(level, list, data);

                mAdapter.setList(list);
                loadComplete();
            }

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


    private void getData() {

//        new Handler().postDelayed(() -> {
//            if (refresh) {
//                mAdapter.getData().clear();
//                mAdapter.notifyDataSetChanged();
//            }
//            List<LevelOneBean> list = new ArrayList<>();
//            LevelOneBean oneBean = new LevelOneBean("山西太原直属库有限公司");
//            oneBean.addChildNode(new LevelTwoBean("山西太原直属库有限公司").setIcon("http://"));
//            oneBean.addChildNode(new LevelTwoBean("山西太原直属库有限公司分库").setIcon("http://"));
//            list.add(oneBean);
//
//            LevelOneBean oneBean2 = new LevelOneBean("山西大同直属库有限公司");
//            oneBean2.addChildNode(new LevelTwoBean("山西大同直属库有限公司").setIcon("http://"));
//            oneBean2.addChildNode(new LevelTwoBean("山西大同直属库有限公司分库").setIcon("http://"));
//            list.add(oneBean2);
//
//            mAdapter.setList(list);
//            loadEnd();
//        }, 500);
    }


    public void initListData(int level, List<BaseNode> list, List<MultiDepotBean> data) {
        level++;
        for (MultiDepotBean bean : data) {
            BaseMultiBean item = new BaseMultiBean(bean.depotName);
            item.level = level;
            // 将库内容存到拓展字段中
            MultiDepotBean depotBean = new MultiDepotBean();
            depotBean.depotCode = bean.depotCode;
            depotBean.depotName = bean.depotName;
            depotBean.parentCode = bean.parentCode;
            item.extra = getGson().toJson(depotBean);

            // 如果有子级 递归
            if (bean.children != null) {
                List<BaseNode> childList = new ArrayList<>();
                initListData(level, childList, bean.children);
                item.addChildNode(childList);
            }
            list.add(item);
        }
    }

    @Override
    public void onItemClickListener(BaseNode data) {
        BaseMultiBean item = (BaseMultiBean) data;
        String extra = item.extra;
        MultiDepotBean bean = getGson().fromJson(extra, MultiDepotBean.class);
        if (bean.depotCode.equals("1")) {
            // 分公司不可点击
            return;
        }
        DepotModel.get().saveSelectDepot(bean);
        EventBus.getDefault().post(new SelectDepotEvent());
        onBack();
    }
}
