package com.casic.dcms.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ExpandableListView;

import com.casic.dcms.adapter.DeptExpandableAdapter;
import com.casic.dcms.base.AndroidxBaseFragment;
import com.casic.dcms.bean.DeptTreeBean;
import com.casic.dcms.bean.PersonBean;
import com.casic.dcms.bean.PhoneBookBean;
import com.casic.dcms.databinding.FragmentPhoneBinding;
import com.casic.dcms.mvp.presenter.impl.DeptTreePresenterImpl;
import com.casic.dcms.mvp.presenter.impl.PhoneBookPresenterImpl;
import com.casic.dcms.mvp.view.IDeptTreeView;
import com.casic.dcms.mvp.view.IPhoneBookView;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.ToastHelper;
import com.casic.dcms.widgets.AlertControlDialog;

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

public class PhonePageFragment extends AndroidxBaseFragment<FragmentPhoneBinding> implements IDeptTreeView, IPhoneBookView {

    private final List<String> deptList = new ArrayList<>(); //部门名集合
    private final List<List<PersonBean>> deptAndPersons = new ArrayList<>(); //个人信息集合的集合
    private Context context;
    private DeptTreePresenterImpl deptTreePresenter;
    private PhoneBookPresenterImpl phoneBookPresenter;
    private DeptExpandableAdapter expandableAdapter;

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

    @Override
    protected void initData() {
        deptTreePresenter = new DeptTreePresenterImpl(this);
        deptTreePresenter.onReadyRetrofitRequest("0");
        phoneBookPresenter = new PhoneBookPresenterImpl(this);
        expandableAdapter = new DeptExpandableAdapter(context);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void obtainDeptTreeResult(DeptTreeBean resultBean) {
        //先获取部门树，在往树结构里面加人
        if (resultBean.getCode() == 200) {
            List<DeptTreeBean.DataBean.ListBean> listBeans = resultBean.getData().getList();
            deptList.clear();
            for (DeptTreeBean.DataBean.ListBean bean : listBeans) {
                deptList.add(bean.getName());
            }
            if (deptList.size() > 0) {
                phoneBookPresenter.onReadyRetrofitRequest(StringHelper.getUserBean().getDeptId());
            }
        }
    }

    @Override
    public void obtainPhoneBookResult(PhoneBookBean resultBean) {
        if (resultBean.getCode() == 200) {
            List<PhoneBookBean.DataBean> dataBeans = resultBean.getData();
            deptAndPersons.clear();
            for (String title : deptList) {
                List<PersonBean> beanList = new ArrayList<>();
                for (PhoneBookBean.DataBean dataBean : dataBeans) {
                    if (title.equals(dataBean.getDeptName())) {
                        PersonBean personBean = new PersonBean();
                        personBean.setPersonDept(dataBean.getDeptName());
                        personBean.setPersonName(dataBean.getName());
                        personBean.setPersonPhone(dataBean.getPhone());
                        beanList.add(personBean);
                    }
                }
                deptAndPersons.add(beanList);
            }
            //展示部门树以及人员
            expandableAdapter.setData(deptList, deptAndPersons);
            viewBinding.deptTreeView.setAdapter(expandableAdapter);
            //默认展开数据
            for (int i = 0; i < expandableAdapter.getGroupCount(); i++) {
                viewBinding.deptTreeView.expandGroup(i);
            }
            viewBinding.deptTreeView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
                    PersonBean personBean = deptAndPersons.get(groupPosition).get(childPosition);
                    callPhone(personBean.getPersonName(), personBean.getPersonPhone());
                    return true;
                }
            });
        }
    }

    /**
     * 直接呼叫电话
     */
    private void callPhone(String personName, String personPhone) {
        if (TextUtils.isEmpty(personPhone)) {
            ToastHelper.showToast("手机号为空，无法拨号", ToastHelper.ERROR);
            return;
        }
        new AlertControlDialog.Builder()
                .setContext(context)
                .setTitle("提示")
                .setMessage("是否确定拨打【" + personName + "】的电话？")
                .setNegativeButton("取消")
                .setPositiveButton("确定")
                .setOnDialogButtonClickListener(new AlertControlDialog.OnDialogButtonClickListener() {
                    @Override
                    public void onConfirmClick() {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:" + personPhone));
                        startActivity(intent);
                    }

                    @Override
                    public void onCancelClick() {

                    }
                }).build().show();
    }

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