package com.sjkj.base_lib.utils;

import android.text.TextUtils;
import android.view.View;

import com.kongzue.dialogx.datepicker.DatePickerDialog;
import com.kongzue.dialogx.datepicker.interfaces.OnDateSelected;
import com.kongzue.dialogx.dialogs.BottomDialog;
import com.kongzue.dialogx.dialogs.BottomMenu;
import com.kongzue.dialogx.dialogs.InputDialog;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialogx.interfaces.OnInputDialogButtonClickListener;
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;
import com.kongzue.dialogx.interfaces.OnMenuItemSelectListener;
import com.kongzue.dialogx.util.TextInfo;

import java.util.Calendar;
import java.util.List;

public class DialogUtils {

    public static void showBottomInput(String title, String msg, DialogInputCallBack dialogInputCallBack) {
        new InputDialog(title, msg, "确认", "取消")
                .setInputText("")
                .setOkButton(new OnInputDialogButtonClickListener<InputDialog>() {
                    @Override
                    public boolean onClick(InputDialog baseDialog, View v, String inputStr) {
                        if (dialogInputCallBack != null) {
                            dialogInputCallBack.itemListener(inputStr);
                        }
//                        PopTip.show("输入的内容：" + inputStr);
                        return false;
                    }
                })
                .show();
    }

    public static void showBottomLatLngInput(String title, String msg, String doubleNumber,String txtValue, DialogInputCallBack dialogInputCallBack) {
        new InputDialog(title, msg, "确认", "取消")
                .setInputText(txtValue)
                .setOkButton(new OnInputDialogButtonClickListener<InputDialog>() {
                    @Override
                    public boolean onClick(InputDialog baseDialog, View v, String inputStr) {
                        if (TextUtils.isEmpty(inputStr)) {
                            ToastUtils.show("请输入正确格式经纬度");
                            return true;
                        }
                        double doubleNum = 0;
                        try {
                            doubleNum = Double.parseDouble(inputStr);
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                            ToastUtils.show("请输入正确格式经纬度");
                            return true;
                        }

                        if ("lat".equals(doubleNumber)) {
                            if (doubleNum > -90 && doubleNum < 90) {
                                if (dialogInputCallBack != null) {
                                    dialogInputCallBack.itemListener(inputStr);
                                }
                            }else {
                                ToastUtils.show("请输入正确格式经纬度");
                                return true;
                            }
                        } else if ("lng".equals(doubleNumber)) {
                            if (doubleNum > -180 && doubleNum < 180) {
                                if (dialogInputCallBack != null) {
                                    dialogInputCallBack.itemListener(inputStr);
                                }
                            }else {
                                ToastUtils.show("请输入正确格式经纬度");
                                return true;
                            }
                        }
//                        PopTip.show("输入的内容：" + inputStr);
                        return false;
                    }
                })
                .show();
    }

    public static void showBottomMenu(String title, List<CharSequence> list, BottomMenuCallBack callBack) {
        if (list != null && list.size() >= 14) {
            BottomMenu.show(list)
                    .setBottomDialogMaxHeight(0.6f)
                    .setMessage(title)
                    .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                        @Override
                        public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                            if (callBack != null)
                                callBack.itemListener(text.toString(), index);
                            return false;
                        }
                    });
            return;
        }
        BottomMenu.show(list)
                .setMessage(title)
                .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                    @Override
                    public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                        if (callBack != null)
                            callBack.itemListener(text.toString(), index);
                        return false;
                    }
                });
    }

    public static void showBottomMenuBig(String title, List<CharSequence> list, BottomMenuCallBack callBack) {
        if (list != null && list.size() >= 14) {
            BottomMenu.show(list)
                    .setBottomDialogMaxHeight(0.6f)
                    .setMessage(title)
                    .setMessageTextInfo(new TextInfo().setFontSize(20).setBold(true))
                    .setMenuTextInfo(new TextInfo().setFontSize(20).setBold(true))
                    .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                        @Override
                        public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                            if (callBack != null)
                                callBack.itemListener(text.toString(), index);
                            return false;
                        }
                    });
            return;
        }
        BottomMenu.show(list)
                .setMessage(title)
                .setMessageTextInfo(new TextInfo().setFontSize(20).setBold(true))
                .setMenuTextInfo(new TextInfo().setFontSize(20).setBold(true))
                .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                    @Override
                    public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                        if (callBack != null)
                            callBack.itemListener(text.toString(), index);
                        return false;
                    }
                });
    }

    public static void showBottomMenuForce(String title, List<CharSequence> list, BottomMenuCallBack callBack) {
        if (list != null && list.size() >= 14) {
            BottomMenu.show(list)
                    .setBottomDialogMaxHeight(0.6f)
                    .setMessage(title)
                    .setCancelable(false)
                    .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                        @Override
                        public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                            if (callBack != null)
                                callBack.itemListener(text.toString(), index);
                            return false;
                        }
                    });
            return;
        }
        BottomMenu.show(list)
                .setMessage(title)
                .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                    @Override
                    public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                        if (callBack != null)
                            callBack.itemListener(text.toString(), index);
                        return false;
                    }
                });
    }

    public static void showBottomMenu(String title, String[] menus, BottomMenuCallBack callBack) {
        if (menus != null && menus.length >= 14) {
            BottomMenu.show(menus)
                    .setBottomDialogMaxHeight(0.6f)
                    .setMessage(title)
                    .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                        @Override
                        public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                            if (callBack != null)
                                callBack.itemListener(text.toString(), index);
                            return false;
                        }
                    });
            return;
        }

        BottomMenu.show(menus)
                .setMessage(title)
                .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                    @Override
                    public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                        if (callBack != null)
                            callBack.itemListener(text.toString(), index);
                        return false;
                    }
                });
    }

    private static int[] selectMenuIndexArray;
    private static CharSequence[] selectMenuStringArray;
    private static String multiSelectMenuResultCache;
    private static int[] selectShiIndexArray;//视联网
    private static CharSequence[] selectShiStringArray;
    private static int[] selectWUIndexArray;//物联网
    private static int[] selectYunIndexArray;//云广播
    private static String multiSelectShiResultCache;
    private static String multiSelectShiResultCacheTest;
    private static String multiSelectWuResultCache;
    private static String multiSelectYunResultCache;

    public static void showMultiItemBottomMenu(String title, List<CharSequence> menus, BottomMenuMultiItemCallBack callBack) {
        selectMenuStringArray = null;
        selectMenuIndexArray = null;
        if (menus != null && menus.size() >= 14) {
            BottomMenu.show(menus)
                    .setBottomDialogMaxHeight(0.6f)
                    .setMessage(title)
                    .setOnMenuItemClickListener(new OnMenuItemSelectListener<BottomMenu>() {
                        @Override
                        public void onMultiItemSelect(BottomMenu dialog, CharSequence[] text, int[] indexArray) {
                            super.onMultiItemSelect(dialog, text, indexArray);
                            multiSelectMenuResultCache = "";
                            for (CharSequence c : text) {
                                multiSelectMenuResultCache = multiSelectMenuResultCache + " " + c;
                            }
                            selectMenuStringArray = text;
                            selectMenuIndexArray = indexArray;
                        }
                    })
                    .setCancelButton("确定", new OnDialogButtonClickListener<BottomDialog>() {
                        @Override
                        public boolean onClick(BottomDialog baseDialog, View v) {
                            if (callBack != null)
                                callBack.itemListener(selectMenuStringArray, selectMenuIndexArray);
                            return false;
                        }
                    })
                    .setSelection(selectMenuIndexArray);

            return;
        }
        BottomMenu.show(menus)
                .setMessage(title)
                .setOnMenuItemClickListener(new OnMenuItemSelectListener<BottomMenu>() {
                    @Override
                    public void onMultiItemSelect(BottomMenu dialog, CharSequence[] text, int[] indexArray) {
                        super.onMultiItemSelect(dialog, text, indexArray);
                        multiSelectMenuResultCache = "";
                        for (CharSequence c : text) {
                            multiSelectMenuResultCache = multiSelectMenuResultCache + " " + c;
                        }
                        selectMenuStringArray = text;
                        selectMenuIndexArray = indexArray;
                    }
                })
                .setCancelButton("确定", new OnDialogButtonClickListener<BottomDialog>() {
                    @Override
                    public boolean onClick(BottomDialog baseDialog, View v) {
                        if (callBack != null)
                            callBack.itemListener(selectMenuStringArray, selectMenuIndexArray);
                        return false;
                    }
                })
                .setSelection(selectMenuIndexArray);

    }

    public static void showMultiItemBottomMenuBig(String title, List<CharSequence> menus, BottomMenuMultiItemCallBack callBack) {
        selectMenuStringArray = null;
        selectMenuIndexArray = null;
        if (menus != null && menus.size() >= 14) {
            BottomMenu.show(menus)
                    .setMessageTextInfo(new TextInfo().setFontSize(20).setBold(true))
                    .setMenuTextInfo(new TextInfo().setFontSize(20).setBold(true))
                    .setBottomDialogMaxHeight(0.6f)
                    .setMessage(title)
                    .setOnMenuItemClickListener(new OnMenuItemSelectListener<BottomMenu>() {
                        @Override
                        public void onMultiItemSelect(BottomMenu dialog, CharSequence[] text, int[] indexArray) {
                            super.onMultiItemSelect(dialog, text, indexArray);
                            multiSelectMenuResultCache = "";
                            for (CharSequence c : text) {
                                multiSelectMenuResultCache = multiSelectMenuResultCache + " " + c;
                            }
                            selectMenuStringArray = text;
                            selectMenuIndexArray = indexArray;
                        }
                    })
                    .setCancelButton("确定", new OnDialogButtonClickListener<BottomDialog>() {
                        @Override
                        public boolean onClick(BottomDialog baseDialog, View v) {
                            if (callBack != null)
                                callBack.itemListener(selectMenuStringArray, selectMenuIndexArray);
                            return false;
                        }
                    })
                    .setSelection(selectMenuIndexArray);

            return;
        }
        BottomMenu.show(menus)
                .setMessageTextInfo(new TextInfo().setFontSize(20).setBold(true))
                .setMenuTextInfo(new TextInfo().setFontSize(20).setBold(true))
                .setMessage(title)
                .setOnMenuItemClickListener(new OnMenuItemSelectListener<BottomMenu>() {
                    @Override
                    public void onMultiItemSelect(BottomMenu dialog, CharSequence[] text, int[] indexArray) {
                        super.onMultiItemSelect(dialog, text, indexArray);
                        multiSelectMenuResultCache = "";
                        for (CharSequence c : text) {
                            multiSelectMenuResultCache = multiSelectMenuResultCache + " " + c;
                        }
                        selectMenuStringArray = text;
                        selectMenuIndexArray = indexArray;
                    }
                })
                .setCancelButton("确定", new OnDialogButtonClickListener<BottomDialog>() {
                    @Override
                    public boolean onClick(BottomDialog baseDialog, View v) {
                        if (callBack != null)
                            callBack.itemListener(selectMenuStringArray, selectMenuIndexArray);
                        return false;
                    }
                })
                .setSelection(selectMenuIndexArray);

    }

    public static void showMultiItemBottomMenu(String title, boolean isCache, List<CharSequence> menus, BottomMenuMultiItemCallBack callBack) {
        if (!isCache) {
            selectMenuStringArray = null;
            selectMenuIndexArray = null;
        }
        if (menus != null && menus.size() >= 14) {
            BottomMenu.show(menus)
                    .setBottomDialogMaxHeight(0.6f)
                    .setMessage(title)
                    .setOnMenuItemClickListener(new OnMenuItemSelectListener<BottomMenu>() {
                        @Override
                        public void onMultiItemSelect(BottomMenu dialog, CharSequence[] text, int[] indexArray) {
                            super.onMultiItemSelect(dialog, text, indexArray);
                            multiSelectMenuResultCache = "";
                            for (CharSequence c : text) {
                                multiSelectMenuResultCache = multiSelectMenuResultCache + " " + c;
                            }
                            selectMenuStringArray = text;
                            selectMenuIndexArray = indexArray;
                        }
                    })
                    .setCancelButton("确定", new OnDialogButtonClickListener<BottomDialog>() {
                        @Override
                        public boolean onClick(BottomDialog baseDialog, View v) {
                            if (callBack != null)
                                callBack.itemListener(selectMenuStringArray, selectMenuIndexArray);
                            return false;
                        }
                    })
                    .setSelection(selectMenuIndexArray);

            return;
        }
        BottomMenu.show(menus)
                .setMessage(title)
                .setOnMenuItemClickListener(new OnMenuItemSelectListener<BottomMenu>() {
                    @Override
                    public void onMultiItemSelect(BottomMenu dialog, CharSequence[] text, int[] indexArray) {
                        super.onMultiItemSelect(dialog, text, indexArray);
                        multiSelectMenuResultCache = "";
                        for (CharSequence c : text) {
                            multiSelectMenuResultCache = multiSelectMenuResultCache + " " + c;
                        }
                        selectMenuStringArray = text;
                        selectMenuIndexArray = indexArray;
                    }
                })
                .setCancelButton("确定", new OnDialogButtonClickListener<BottomDialog>() {
                    @Override
                    public boolean onClick(BottomDialog baseDialog, View v) {
                        if (callBack != null)
                            callBack.itemListener(selectMenuStringArray, selectMenuIndexArray);
                        return false;
                    }
                })
                .setSelection(selectMenuIndexArray);

    }

    public static void showMultiItemBottomMenu(String title, String[] menus, BottomMenuMultiItemCallBack callBack) {
        selectMenuStringArray = null;
        selectMenuIndexArray = null;
        if (menus != null && menus.length >= 14) {
            BottomMenu.show(menus)
                    .setBottomDialogMaxHeight(0.6f)
                    .setMessage(title)
                    .setOnMenuItemClickListener(new OnMenuItemSelectListener<BottomMenu>() {
                        @Override
                        public void onMultiItemSelect(BottomMenu dialog, CharSequence[] text, int[] indexArray) {
                            super.onMultiItemSelect(dialog, text, indexArray);
                            multiSelectMenuResultCache = "";
                            for (CharSequence c : text) {
                                multiSelectMenuResultCache = multiSelectMenuResultCache + " " + c;
                            }
                            selectMenuStringArray = text;
                            selectMenuIndexArray = indexArray;
                        }
                    })
                    .setCancelButton("确定", new OnDialogButtonClickListener<BottomDialog>() {
                        @Override
                        public boolean onClick(BottomDialog baseDialog, View v) {
                            if (callBack != null)
                                callBack.itemListener(selectMenuStringArray, selectMenuIndexArray);
                            return false;
                        }
                    })
                    .setSelection(selectMenuIndexArray);

            return;
        }

        BottomMenu.show(menus)
                .setMessage(title)
                .setOnMenuItemClickListener(new OnMenuItemSelectListener<BottomMenu>() {
                    @Override
                    public void onMultiItemSelect(BottomMenu dialog, CharSequence[] text, int[] indexArray) {
                        super.onMultiItemSelect(dialog, text, indexArray);
                        multiSelectMenuResultCache = "";
                        for (CharSequence c : text) {
                            multiSelectMenuResultCache = multiSelectMenuResultCache + " " + c;
                        }
                        selectMenuStringArray = text;
                        selectMenuIndexArray = indexArray;
                    }
                })
                .setCancelButton("确定", new OnDialogButtonClickListener<BottomDialog>() {
                    @Override
                    public boolean onClick(BottomDialog baseDialog, View v) {
                        if (callBack != null)
                            callBack.itemListener(selectMenuStringArray, selectMenuIndexArray);
                        return false;
                    }
                })
                .setSelection(selectMenuIndexArray);

    }

    public static void showDateBottomMenu(BottomDateCallBack callBack) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog.build()
                .setTitle("请选择日期")
                .setMinTime(1940, 1, 1)           //指定最小可选日期 1990年5月20日
                .setMaxTime(2040, 1, 1)            //指定最大可选日期 2030年2月10日
                .setDefaultSelect(year, month + 1, day)    //设置默认选中日期
                .setDoubleDateFormat(true)
                .show(new OnDateSelected() {
                    /**
                     * 此处为回调，
                     * @param text 直接返回默认文本，例如“2021-9-25”
                     * @param year  年
                     * @param month 月
                     * @param day   日
                     */
                    @Override
                    public void onSelect(String text, int year, int month, int day) {
                        if (callBack != null)
                            callBack.itemListener(text, year, month, day);
                    }
                });

    }

    public static void showDateBottomMenu(String title, BottomDateCallBack callBack) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog.build()
                .setTitle(title)
                .setMinTime(1940, 1, 1)              //指定最小可选日期 1990年5月20日
                .setMaxTime(2040, 1, 1)            //指定最大可选日期 2030年2月10日
                .setDefaultSelect(year, month + 1, day)    //设置默认选中日期
                .setDoubleDateFormat(true)
                .show(new OnDateSelected() {
                    /**
                     * 此处为回调，
                     * @param text 直接返回默认文本，例如“2021-9-25”
                     * @param year  年
                     * @param month 月
                     * @param day   日
                     */
                    @Override
                    public void onSelect(String text, int year, int month, int day) {
                        if (callBack != null)
                            callBack.itemListener(text, year, month, day);
                    }
                });

    }

    public interface BottomMenuCallBack {
        void itemListener(String text, int index);
    }

    public interface BottomMenuMultiItemCallBack {
        void itemListener(CharSequence[] text, int[] indexArray);
    }

    public interface BottomDateCallBack {
        void itemListener(String text, int year, int month, int day);
    }

    public interface DialogInputCallBack {
        void itemListener(String text);
    }

}
