package com.ifavine.pay.utils;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.ifavine.pay.R;
import com.ifavine.pay.common.AppContext;

import java.util.Calendar;

public class DialogUtil {
    private static Dialog tipDialog;
    private static TextView tipTextView;

    public static final int OK = 0xff888801;
    public static final int CANCEL = 0xff888802;

    public static void dismissTipDialog() {
        if (tipDialog != null) {
            tipDialog.dismiss();
            tipDialog = null;
        }
    }

    public static boolean isShowing() {
        if (tipDialog == null) {
            return false;
        }
        return tipDialog.isShowing();
    }


    /**
     * showOkDialog
     */
    public static Dialog showOkDialog(Context mContext, Integer imageRes, String title, String msg,
                                      String okName, View.OnClickListener okListener) {
        View view = View.inflate(mContext, R.layout.dialog_common_ok, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_style);// 创建自定义样式dialog

        dialog.setCancelable(true);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(false); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        TextView tv_msg = (TextView) view.findViewById(R.id.dialog_msg);
        ImageView iv = (ImageView) view.findViewById(R.id.dialog_iv);
        if (msg != null) {
            tv_msg.setText(msg);
            tv_msg.setVisibility(View.VISIBLE);
        }
        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }
        btn_ok.setText(okName);
        //set image
        if (imageRes != null) {
            iv.setImageResource(imageRes);
        }
        if (okListener != null) {
            btn_ok.setOnClickListener(okListener);
        } else {
            btn_ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        dialog.show();
        return dialog;
    }

    /**
     * showConfirmDialog
     *
     * @param imageRes   imageID
     * @param okName     set button name
     * @param cancelName set button name
     */
    public static Dialog showConfirmDialog(Context mContext, Integer imageRes, String title,
                                           String msg, String okName, String cancelName, View.OnClickListener okListener,
                                           View.OnClickListener cancelListener, boolean cancelable) {
        View view = View.inflate(mContext, R.layout.dialog_common_confirm, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_style);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) view.findViewById(R.id.dialog_cancel);
        TextView tv_msg = (TextView) view.findViewById(R.id.dialog_msg);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        ImageView iv = (ImageView) view.findViewById(R.id.dialog_iv);
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        if (msg != null) {
            tv_msg.setText(msg);
            tv_msg.setVisibility(View.VISIBLE);
        }
        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }
        btn_ok.setText(okName);
        btn_cancel.setText(cancelName);
        //set image
        if (imageRes != null) {
            iv.setImageResource(imageRes);
        } else {
            iv.setVisibility(View.GONE);
        }
        //set cancel listener
        if (cancelListener != null) {
            btn_cancel.setOnClickListener(cancelListener);
        } else {
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        //set confirm listener
        if (okListener != null) {
            btn_ok.setOnClickListener(okListener);
        } else {
            btn_ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }


    public static Dialog showLoadingDialog(Context context, boolean cancelable, String msg) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.view_progressbar, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // main.xml中的ImageView

        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
        tipTextView.setText(msg);// 设置加载信息

        tipDialog = new Dialog(context, R.style.dialog_loading_dialog);// 创建自定义样式dialog

        tipDialog.setCancelable(cancelable);
        tipDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        tipDialog.show();
        return tipDialog;
    }


    /**
     * showConfirmDialog
     *
     * @param imageRes   imageID
     * @param okName     set button name
     * @param cancelName set button name
     */
    public static Dialog showInputDialog(Context mContext, Integer imageRes, String title,
                                         final String msg, String okName, String cancelName, final View.OnClickListener okListener,
                                         View.OnClickListener cancelListener, boolean cancelable) {
        View view = View.inflate(mContext, R.layout.dialog_common_input_confirm, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_style);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) view.findViewById(R.id.dialog_cancel);
        final EditText tv_msg = (EditText) view.findViewById(R.id.dialog_msg);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        ImageView iv = (ImageView) view.findViewById(R.id.dialog_iv);
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        tv_msg.setTransformationMethod(PasswordTransformationMethod.getInstance());
        if (msg != null) {
            tv_msg.setHint(msg);
            tv_msg.setVisibility(View.VISIBLE);
        }
        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }
        btn_ok.setText(okName);
        btn_cancel.setText(cancelName);
        //set image
        if (imageRes != null) {
            iv.setImageResource(imageRes);
        } else {
            iv.setVisibility(View.GONE);
        }
        //set cancel listener
        if (cancelListener != null) {
            btn_cancel.setOnClickListener(cancelListener);
        } else {
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        //set confirm listener
        if (okListener != null) {
            btn_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String pwd = tv_msg.getText().toString().trim();
                    if ("".equals(pwd)) {
                        AppContext.showToast("请输入" + msg);
                        return;
                    }
                    if (MD5Util.MD5(pwd).equals(SPUtil.getInstance().readString("pwd"))) {
                        dialog.dismiss();
                        okListener.onClick(tv_msg);
                    } else {
                        AppContext.showToast("管理员密码不正确");
                    }
                }
            });
        } else {
            btn_ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }

    public static void showTimePickerDialog(Context activity, int themeResId, final TextView tv, Calendar calendar) {
        // Calendar c = Calendar.getInstance();
        // 创建一个TimePickerDialog实例，并把它显示出来
        // 解释一哈，Activity是context的子类
        String[] times = tv.getText().toString().split(":");
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(times[1]));
        new TimePickerDialog(activity, themeResId,
                // 绑定监听器
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        tv.setText((hourOfDay > 9 ? hourOfDay : "0" + hourOfDay) + ":" + (minute > 9 ? minute : "0" + minute));
                    }
                }
                // 设置初始时间
                , calendar.get(Calendar.HOUR_OF_DAY)
                , calendar.get(Calendar.MINUTE)
                // true表示采用24小时制
                , true).show();
    }
}
