package com.casic.dcms.fragment;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.casic.dcms.adapter.PersonListAdapter;
import com.casic.dcms.databinding.FragmentPersonListBinding;
import com.casic.dcms.model.PersonBean;
import com.casic.dcms.model.PersonStatusBean;
import com.casic.dcms.vm.PersonViewModel;
import com.pengxh.androidx.lite.base.AndroidxBaseFragment;

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

public class PersonListFragment extends AndroidxBaseFragment<FragmentPersonListBinding> {

    private final Set<String> deptSet = new HashSet<>();
    private final List<List<PersonBean>> deptAndPersons = new ArrayList<>(); //个人信息集合的集合
    private PersonViewModel statusViewModel;
    private PersonListAdapter onlinePersonAdapter;
    private List<String> deptList = new ArrayList<>(); //部门名集合

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void initData() {
        statusViewModel = new ViewModelProvider(this).get(PersonViewModel.class);
        statusViewModel.obtainPersonByStatus("", "", "");

        onlinePersonAdapter = new PersonListAdapter(requireContext());
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initEvent() {
        statusViewModel.personStatusModel.observe(this, new Observer<PersonStatusBean>() {
            @Override
            public void onChanged(PersonStatusBean resultBean) {
                if (resultBean.getCode() == 200) {
                    List<PersonStatusBean.DataDTO> dataBeans = resultBean.getData();
                    deptList.clear();
                    for (PersonStatusBean.DataDTO 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 (PersonStatusBean.DataDTO 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);
                    }
                }
            }
        });
    }
}