package com.casic.dcms.ui.fragment;

import android.content.Context;
import android.widget.ExpandableListView;

import com.casic.dcms.R;
import com.casic.dcms.adapter.OnlinePersonAdapter;
import com.casic.dcms.base.BaseFragment;
import com.casic.dcms.bean.OnlinePersonBean;
import com.casic.dcms.bean.PersonBean;
import com.casic.dcms.mvp.presenter.PersonOnlinePresenterImpl;
import com.casic.dcms.mvp.view.IPersonOnlineView;

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

import butterknife.BindView;

public class PersonOnLineFragment extends BaseFragment implements IPersonOnlineView {

    @BindView(R.id.deptTreeView)
    ExpandableListView deptTreeView;

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

    @Override
    public int initLayoutView() {
        return R.layout.fragment_person_list;
    }

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

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

    @Override
    public void initEvent() {

    }

    @Override
    public void onlineResult(OnlinePersonBean resultBean) {
        if (resultBean.isSuccess()) {
            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);
                deptTreeView.setAdapter(onlinePersonAdapter);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (onlinePresenter != null) {
            onlinePresenter.disposeRetrofitRequest();
        }
    }
}