package com.casic.dcms.view.worker;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.casic.dcms.R;
import com.casic.dcms.adapter.WorkerAdapter;
import com.casic.dcms.databinding.ActivityWorkerListBinding;
import com.casic.dcms.model.PersonBean;
import com.casic.dcms.model.WorkerBean;
import com.casic.dcms.utils.ViewGroupKit;
import com.casic.dcms.vm.WorkerViewModel;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.utils.LoadState;
import com.pengxh.androidx.lite.utils.LoadingDialog;
import com.pengxh.androidx.lite.widget.TitleBarView;

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

public class WorkerListActivity extends AndroidxBaseActivity<ActivityWorkerListBinding> {

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

    @Override
    protected void setupTopBarLayout() {
        ViewGroupKit.initImmersionBar(binding.rootView, this, true, R.color.white);
        binding.titleView.setOnClickListener(new TitleBarView.OnClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {

            }
        });
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        onlinePersonAdapter = new WorkerAdapter(this);

        workerViewModel = new ViewModelProvider(this).get(WorkerViewModel.class);
        workerViewModel.workerModel.observe(this, new Observer<WorkerBean>() {
            @Override
            public void onChanged(WorkerBean resultBean) {
                if (resultBean.getCode() == 200) {
                    List<WorkerBean.DataBean> dataBeans = resultBean.getData();
                    deptList.clear();
                    for (WorkerBean.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 (WorkerBean.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);
                        binding.deptTreeView.setAdapter(onlinePersonAdapter);
                    }
                }
            }
        });
        workerViewModel.getWorkers(this, "", "", "");
    }

    @Override
    protected void observeRequestState() {
        workerViewModel.loadState.observe(this, new Observer<LoadState>() {
            @Override
            public void onChanged(LoadState loadState) {
                if (loadState == LoadState.Loading) {
                    LoadingDialog.show(WorkerListActivity.this, "获取数据中...");
                } else {
                    LoadingDialog.dismiss();
                }
            }
        });
    }

    @Override
    protected void initEvent() {

    }
}
