package com.casic.dcms.ui.fragment;

import android.content.Context;

import com.casic.dcms.adapter.OnlinePersonAdapter;
import com.casic.dcms.base.AndroidxBaseFragment;
import com.casic.dcms.bean.OnlinePersonBean;
import com.casic.dcms.bean.PersonBean;
import com.casic.dcms.databinding.FragmentPersonListBinding;
import com.casic.dcms.mvp.presenter.impl.PersonOnlinePresenterImpl;
import com.casic.dcms.mvp.view.IPersonOnlineView;

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

public class PersonOnLineFragment extends AndroidxBaseFragment<FragmentPersonListBinding> implements IPersonOnlineView {

    private final Set<String> deptSet = new HashSet<>();
    private final List<List<PersonBean>> deptAndPersons = new ArrayList<>(); //个人信息集合的集合
    private Context context;
    private PersonOnlinePresenterImpl onlinePresenter;
    private OnlinePersonAdapter onlinePersonAdapter;
    private List<String> deptList = new ArrayList<>(); //部门名集合

    @Override
    protected void setupTopBarLayout() {
        context = getContext();
    }

    @Override
    protected void initData() {
        onlinePresenter = new PersonOnlinePresenterImpl(this);
        onlinePresenter.onReadyRetrofitRequest("", "", "");
        onlinePersonAdapter = new OnlinePersonAdapter(context);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void onlineResult(OnlinePersonBean resultBean) {
        if (resultBean.getCode() == 200) {
            List<OnlinePersonBean.DataBean> dataBeans = resultBean.getData();
            deptList.clear();
            for (OnlinePersonBean.DataBean bean : dataBeans) {
                deptSet.add(bean.getDeptName());
            }
            //hashSet去重然后转List
            deptList = new ArrayList<>(deptSet);
            if (deptList.size() > 0) {
                deptAndPersons.clear();
                for (String title : deptList) {
                    List<PersonBean> beanList = new ArrayList<>();
                    for (OnlinePersonBean.DataBean dataBean : dataBeans) {
                        if (title.equals(dataBean.getDeptName())) {
                            PersonBean personBean = new PersonBean();
                            personBean.setPersonDept(dataBean.getDeptName());
                            personBean.setPersonName(dataBean.getName());
                            personBean.setPersonPhone(dataBean.getPhone());
                            personBean.setLastTimeFmt(dataBean.getLastTimeFmt());
                            personBean.setOnLine(dataBean.isOnLine());
                            beanList.add(personBean);
                        }
                    }
                    deptAndPersons.add(beanList);
                }
                onlinePersonAdapter.setData(deptList, deptAndPersons);
                viewBinding.deptTreeView.setAdapter(onlinePersonAdapter);
            }
        }
    }

    @Override
    public void onDestroyView() {
        onlinePresenter.disposeRetrofitRequest();
        super.onDestroyView();
    }
}