package com.sensetime.demo.util;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.Theme;
import com.blankj.utilcode.util.LogUtils;

import java.util.List;

import kr.co.icontrols.ilp300_700.R;


/**
 * Author:Created by LaoTie on 2019/12/12.
 * Enterprise:SenseTime
 **/
public class MaterialDialogUtils {

    public void showThemed(Context context, String
            title, String content) {
        new MaterialDialog.Builder(context)
                .title(title)
                .content(content)
                .positiveText("agree")
                .negativeText("disagree")
                .positiveColorRes(R.color.white)
                .negativeColorRes(R.color.white)
                .titleGravity(GravityEnum.CENTER)
                .titleColorRes(R.color.white)
                .contentColorRes(android.R.color.white)
                .backgroundColorRes(R.color.material_blue_grey_800)
                .dividerColorRes(R.color.white)
                .btnSelector(R.drawable.md_selector, DialogAction.POSITIVE)
                .positiveColor(Color.WHITE)
                .negativeColorAttr(android.R.attr.textColorSecondaryInverse)
                .theme(Theme.DARK)
                .autoDismiss(true)  //点击是否关闭对话框
                .showListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        //dialog 出现
                    }
                })
                .cancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        //dialog 消失（返回键）
                    }
                })
                .dismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        //dialog 消失
                    }
                })
                .show();
    }

    /***
     * 获取一个耗时等待对话框
     *
     * @param horizontal
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showProgressDialog(Context context, String content, boolean horizontal, int max) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(content)
                .progress(false, max)
                .progressIndeterminateStyle(horizontal)
                .cancelable(false)
                .canceledOnTouchOutside(false)
                .backgroundColorRes(R.color.white)
                .negativeText(R.string.dialog_btn_cancel)
                .keyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (event.getAction() == KeyEvent.ACTION_DOWN) {//如果是按下，则响应，否则，一次按下会响应两次
                            if (keyCode == KeyEvent.KEYCODE_BACK) {
                                //activity.onBackPressed();

                            }
                        }
                        return false;//false允许按返回键取消对话框，true除了调用取消，其他情况下不会取消
                    }
                });
        return builder;
    }

    /***
     * 获取一个耗时等待对话框
     * @param horizontal
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showIndeterminateProgressDialog(Context context, String content, boolean horizontal) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(content)
                .progress(true, 0)
                .progressIndeterminateStyle(horizontal)
                .cancelable(true)
                .canceledOnTouchOutside(false)
                .backgroundColorRes(R.color.white)
                .keyListener((dialog, keyCode, event) -> {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {//如果是按下，则响应，否则，一次按下会响应两次
                        if (keyCode == KeyEvent.KEYCODE_BACK) {
                            //activity.onBackPressed();

                        }
                    }
                    return false;//false允许按返回键取消对话框，true除了调用取消，其他情况下不会取消
                });
        return builder;
    }



    /***
     * 显示一个基础的对话框  只有内容没有标题
     * @param
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showBasicDialogNoTitle(final Context context, String content) {

        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .content(content)
                .cancelable(true)
                .canceledOnTouchOutside(false)
                .positiveText(R.string.dialog_btn_ok)
                .negativeText(R.string.dialog_btn_cancel);

        return builder;
    }


    /***
     * 显示一个基础的对话框  带标题 带内容
     * 没有取消按钮
     * @param
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showBasicDialogNoCancel(final Context context, String            title, String content) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(title)
                .content(content)
                .cancelable(true)
                .canceledOnTouchOutside(false)
                .positiveText(R.string.dialog_btn_ok);

        return builder;
    }

    /***
     * 显示一个基础的对话框  带标题 带内容
     * @param
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showBasicDialog(final Context context, String title, String content) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(title)
                .content(content)
                .cancelable(true)
                .canceledOnTouchOutside(false)
                .positiveText(context.getString(R.string.dialog_btn_ok))
                .negativeText(context.getString(R.string.dialog_btn_cancel));

        return builder;
    }

    /***
     * 显示一个基础的对话框  带标题 带内容
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showBasicDialogPositive(final Context context, String
            title, String content) {

        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(title)
                .content(content)
                .cancelable(true)
                .canceledOnTouchOutside(false)
                .positiveText(R.string.dialog_btn_ok)
                .negativeText(R.string.dialog_btn_cancel);

        return builder;
    }

    /***
     * 选择图片等Item的对话框  带标题
     * @param
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder getSelectDialog(Context context, String title, String[] arrays) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .items(arrays)
                .cancelable(true)
                .canceledOnTouchOutside(false)
                .itemsColor(0XFF456ea6)
                .negativeText(R.string.dialog_btn_cancel);
        if (!TextUtils.isEmpty(title)) {
            builder.title(title);
        }
        return builder;
    }

    /***
     * 获取LIST对话框
     *
     * @param
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showBasicListDialog(final Context context, String title, List
            content) {

        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(title)
                .items(content)
                .itemsCallback(new MaterialDialog.ListCallback() {
                    @Override
                    public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {

                    }
                })
                .negativeText(R.string.dialog_btn_cancel)
//                .checkBoxPromptRes(R.string.app_name, false, null)
                ;

        return builder;
    }

    /***
     * 获取单选LIST对话框
     *
     * @param
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showSingleListDialog(final Context context, String title, List content, int selectedIndex) {

        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(title)
                .items(content)
                .cancelable(true)
                .canceledOnTouchOutside(false)
                .itemsCallbackSingleChoice(selectedIndex, new MaterialDialog.ListCallbackSingleChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog dialog, View itemView, int which,
                                               CharSequence text) {
                        LogUtils.d("text:" + text + ", which:" + which + ",  itemView's text:" + itemView.getTag());
                        return true; // allow selection
                    }
                })
                .positiveText(context.getString(R.string.dialog_btn_ok));

        return builder;
    }


    /***
     * 获取多选LIST对话框
     *
     * @param
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showMultiListDialog(final Context context, String title, List
            content) {

        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(title)
                .items(content)
                .itemsCallbackMultiChoice(new Integer[]{1, 3}, new MaterialDialog
                        .ListCallbackMultiChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {


                        return true; // allow selection
                    }
                })
                .onNeutral(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.clearSelectedIndices();
                    }
                })
                .alwaysCallMultiChoiceCallback()
                .positiveText(R.string.md_choose_label)
                .autoDismiss(false)
                .neutralText("clear")
                .itemsDisabledIndices(0, 1);

        return builder;
    }

    /***
     * 显示一个自定义对话框  带标题 带内容
     * 没有取消按钮
     * @param
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showCustomDialogNoCancel(final Context context, String title, int content) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(title)
                .cancelable(true)
                .canceledOnTouchOutside(false)
                .customView(content, true)
                .positiveText(context.getString(R.string.dialog_btn_ok));

        return builder;
    }



    /***
     * 获取自定义对话框
     *
     * @param
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showCustomDialog(final Context context, String title, int content) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(title)
                .cancelable(true)
                .canceledOnTouchOutside(false)
                .customView(content, true)
                .positiveText(context.getString(R.string.dialog_btn_ok))
                .neutralText(R.string.dialog_btn_cancel);

        return builder;
    }

    public static MaterialDialog.Builder showCustomDialog2(final Context context, String title, int content) {
        return new MaterialDialog.Builder(context)
                .title(title)
                .cancelable(true)
                .canceledOnTouchOutside(true)
                .customView(content, true);
    }


    /***
     * 获取输入对话框
     * @param
     * @return MaterialDialog.Builder
     */
    public static MaterialDialog.Builder showInputDialog(final Context context, String title, String content) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .title(title)
                .cancelable(true)
                .canceledOnTouchOutside(false)
                .content(content)
                .inputType(InputType.TYPE_CLASS_TEXT |
                        InputType.TYPE_TEXT_VARIATION_PERSON_NAME |
                        InputType.TYPE_TEXT_FLAG_CAP_WORDS)
                .positiveText(context.getString(R.string.dialog_btn_ok))
                .negativeText(context.getString(R.string.dialog_btn_cancel));
        return builder;
    }

}
