package com.casic.dcms.fragment;

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

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

import com.casic.dcms.adapter.DeptExpandableAdapter;
import com.casic.dcms.databinding.FragmentPhoneBinding;
import com.casic.dcms.model.DeptTreeBean;
import com.casic.dcms.model.PersonBean;
import com.casic.dcms.model.PhoneBookBean;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.vm.DeptTreeViewModel;
import com.casic.dcms.vm.PhoneBookViewModel;
import com.pengxh.androidx.lite.base.AndroidxBaseFragment;
import com.pengxh.androidx.lite.kit.StringKit;
import com.pengxh.androidx.lite.widget.dialog.AlertControlDialog;

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

public class PhonePageFragment extends AndroidxBaseFragment<FragmentPhoneBinding> {

    private final List<String> deptCollection = new ArrayList<>(); //部门名集合
    private final List<List<PersonBean>> deptAndPersons = new ArrayList<>(); //个人信息集合的集合
    private DeptTreeViewModel deptTreeViewModel;
    private PhoneBookViewModel phoneBookViewModel;
    private DeptExpandableAdapter expandableAdapter;

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        deptTreeViewModel = new ViewModelProvider(this).get(DeptTreeViewModel.class);
        phoneBookViewModel = new ViewModelProvider(this).get(PhoneBookViewModel.class);
        expandableAdapter = new DeptExpandableAdapter(requireContext());

        deptTreeViewModel.getDeptTree(requireContext(), "0");
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initEvent() {
        deptTreeViewModel.deptModel.observe(this, new Observer<DeptTreeBean>() {
            @Override
            public void onChanged(DeptTreeBean resultBean) {
                //先获取部门树，在往树结构里面加人
                if (resultBean.getCode() == 200) {
                    List<DeptTreeBean.DataBean.ListBean> listBeans = resultBean.getData().getList();
                    deptCollection.clear();
                    for (DeptTreeBean.DataBean.ListBean bean : listBeans) {
                        deptCollection.add(bean.getName());
                    }
                    if (deptCollection.size() > 0) {
                        phoneBookViewModel.getPhoneBook(requireContext(), StringHelper.getUserBean().getDeptId());
                    }
                }
            }
        });
        phoneBookViewModel.phoneBookModel.observe(this, new Observer<PhoneBookBean>() {
            @Override
            public void onChanged(PhoneBookBean resultBean) {
                if (resultBean.getCode() == 200) {
                    List<PhoneBookBean.DataBean> dataBeans = resultBean.getData();
                    deptAndPersons.clear();
                    for (String deptName : deptCollection) {
                        List<PersonBean> beanList = new ArrayList<>();
                        for (PhoneBookBean.DataBean dataBean : dataBeans) {
                            if (deptName.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(deptCollection, deptAndPersons);
                    getBinding().deptTreeView.setAdapter(expandableAdapter);
                    //默认展开数据
                    for (int i = 0; i < expandableAdapter.getGroupCount(); i++) {
                        getBinding().deptTreeView.expandGroup(i);
                    }
                    getBinding().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)) {
            StringKit.show(requireContext(), "手机号为空，无法拨号");
            return;
        }
        new AlertControlDialog.Builder()
                .setContext(requireContext())
                .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();
    }
}