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.R;
import com.casic.dcms.adapter.DeptExpandableAdapter;
import com.casic.dcms.base.BaseFragment;
import com.casic.dcms.bean.DeptTreeBean;
import com.casic.dcms.bean.PersonBean;
import com.casic.dcms.bean.PhoneBookBean;
import com.casic.dcms.mvp.presenter.DeptTreePresenterImpl;
import com.casic.dcms.mvp.presenter.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.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

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

import butterknife.BindView;

public class PhonePageFragment extends BaseFragment implements IDeptTreeView, IPhoneBookView {

    private static final String TAG = "PhonePageFragment";

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

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

    @Override
    protected int initLayoutView() {
        return R.layout.fragment_phone;
    }

    @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) {
//        Log.d(TAG, "obtainPhoneBookResult: " + new Gson().toJson(resultBean));
        //先获取部门树，在往树结构里面加人
        if (resultBean.isSuccess()) {
            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.getUserDeptId());
            }
        }
    }

    @Override
    public void obtainPhoneBookResult(PhoneBookBean resultBean) {
//        Log.d(TAG, "obtainPhoneBookResult: " + new Gson().toJson(resultBean));
        if (resultBean.isSuccess()) {
            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);
            deptTreeView.setAdapter(expandableAdapter);
            deptTreeView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                    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 QMUIDialog.MessageDialogBuilder(context)
                .setTitle("提示")
                .setMessage("是否确定拨打[" + personName + "]的电话？")
                .setCanceledOnTouchOutside(false)
                .addAction("取消", (dialog, index) -> dialog.dismiss())
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:" + personPhone));
                        startActivity(intent);
                    }
                })
                .create().show();
    }

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