package com.unis.sxzcl.ui.basic;

import android.os.Bundle;
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.UrlPath;
import com.unis.model.UserModel;
import com.unis.model.bean.MultiDepotBean;
import com.unis.model.bean.person.PersonTreeBean;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentBaseListBinding;
import com.unis.sxzcl.widget.multi_list.BaseMultiBean;
import com.unis.sxzcl.widget.multi_list.MultiListAdapter;
import com.xyxsbj.mylibrary.tool.ToastTool;

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

/**
 * 包名: com.unis.sxzcl.ui.basic
 * 类名: PersonFrameFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/9 20:23
 * 更新人:
 * 更新时间: 2020/12/9 20:23
 * 更新说明:
 * 版本号: 1.0
 */
public class PersonFrameFragment extends BaseListFragment<FragmentBaseListBinding> implements MultiListAdapter.Callback {

    private MultiListAdapter mAdapter;
    private MultiDepotBean mDepot;


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

    @Override
    protected void init() {

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

    @Override
    protected void onFirstShow() {
        mDepot = DepotModel.get().getSelectDepot();
        if (mDepot == null) {
            ToastTool.showLong("请先选择库");
            onBack();
            return;
        }
        onRefresh();
    }

    @Override
    public void onItemClickListener(BaseNode data) {
        BaseMultiBean item = ((BaseMultiBean) data);
        PersonTreeBean bean = getGson().fromJson(item.extra, PersonTreeBean.class);
        if (bean.isDepartmentStaff.equals("1")) {
            Bundle bundle = new Bundle();
            bundle.putString("id", bean.personnelId);
            navigation(R.id.personDetailFragment, bundle);
        }
    }

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

    public void getData() {

        UserModel.get().getPersonTree(mDepot.depotCode, new DataCallback<List<PersonTreeBean>>() {
            @Override
            public void onSuccess(List<PersonTreeBean> data) {
                if (!isActive()) return;

                List<BaseNode> list = new ArrayList<>();
                int index = 0;
                initListData(index, list, data);
                mAdapter.setList(list);

                loadComplete();
            }

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

    private void initListData(int level, List<BaseNode> list, List<PersonTreeBean> data) {
        level++;
        for (PersonTreeBean bean : data) {
            BaseMultiBean item = new BaseMultiBean(bean.departmentStaffName);
            if (bean.isDepartmentStaff.equals("1")) {
                item.icon = UrlPath.image_host + bean.pheadPortrait;
                item.content = bean.ppost;
            } else {
                // 部门下没有人员 不加入列表中
                if (bean.children == null || bean.children.size() == 0)
                    continue;
            }
            item.level = level;
            // 将数据源加入实体中
            PersonTreeBean personTreeBean = new PersonTreeBean();
            personTreeBean.departmentStaffId = bean.departmentStaffId;
            personTreeBean.departmentStaffName = bean.departmentStaffName;
            personTreeBean.isDepartmentStaff = bean.isDepartmentStaff;
            personTreeBean.personnelId = bean.personnelId;
            item.extra = getGson().toJson(personTreeBean);

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

            list.add(item);
        }

    }

}
