package com.bonait.bnframework.HBL.Dialog;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bonait.bnframework.HBL.Logs.MessageLog;
import com.bonait.bnframework.R;

/**
 * 自定义弹框
 * 需要在 res->values->styles.xml中添加 custom_dialog2样式
 * 需要在layout中添加dialog布局页面
 */
public class DialogManager {

//    private static DialogView dview;

    private static Context mContext;

    private static Activity mActivity;

    public static void Init(Context  context,Activity activity){
        mContext = context;
        mActivity=activity;
    }

    /**
     * 对话框，自定义按钮，非阻塞
     * */
    private static void showDialog(String message, AlertDialogButton btn,DialogType dt, IDialogAction action) {
        if(mContext==null) return;
        try{
            DialogView dview=new DialogView(mContext, R.layout.activity_dialog, R.style.custom_dialog2);
            if (dview != null) {
                if (!dview.isShowing()) {

                    String btnName1="确定";
                    String btnName2="取消";
                    if(btn==AlertDialogButton.YesNoCancel||btn==AlertDialogButton.YesNo){
                        btnName1="是";
                        btnName2="否";
                    }

                    dview.setCanceledOnTouchOutside(false);//禁用触摸其它区域关闭弹框
                    RelativeLayout rlTitle = (RelativeLayout) dview.findViewById(R.id.rl_title);
                    if(dt== DialogType.提示)rlTitle.setBackgroundResource(R.drawable.dialog_info_title_back);
                    if(dt== DialogType.警告)rlTitle.setBackgroundResource(R.drawable.dialog_warn_title_back);
                    if(dt== DialogType.错误)rlTitle.setBackgroundResource(R.drawable.dialog_error_title_back);
                    TextView Tv=(TextView)dview.findViewById(R.id.tv_Title);
                    TextView Info=(TextView)dview.findViewById(R.id.tv_Info);
                    Button ok = (Button)dview.findViewById(R.id.btn_ok);
                    Button cancel = (Button)dview.findViewById(R.id.btn_cancel);

                    Tv.setText(dt.toString());
                    Info.setText(message);
                    ok.setText(btnName1);
                    cancel.setText(btnName2);

                    if(btn==AlertDialogButton.OK)cancel.setVisibility(View.GONE);

                    ok.setOnClickListener(view->{
                        if(action!=null)action.ExitDialog(true);
                        hide(dview);
                    });

                    cancel.setOnClickListener(view->{
                        if(action!=null)action.ExitDialog(false);
                        hide(dview);
                    });
                    dview.show();
                }
            }
        }catch(Exception e){
            MessageLog.ShowError("打开弹框异常:"+e.toString());
        }
    }

    /**
     * 对话框，自定义按钮，非阻塞
     * */
    private static void showDialog(Context ct,String message, AlertDialogButton btn,DialogType dt, IDialogAction action) {
        if(ct==null) return;
        try{
            DialogView dview=new DialogView(ct, R.layout.activity_dialog, R.style.custom_dialog2);
            if (dview != null) {
                if (!dview.isShowing()) {

                    String btnName1="确定";
                    String btnName2="取消";
                    if(btn==AlertDialogButton.YesNoCancel||btn==AlertDialogButton.YesNo){
                        btnName1="是";
                        btnName2="否";
                    }

                    dview.setCanceledOnTouchOutside(false);//禁用触摸其它区域关闭弹框
                    RelativeLayout rlTitle = (RelativeLayout) dview.findViewById(R.id.rl_title);
                    if(dt== DialogType.提示)rlTitle.setBackgroundResource(R.drawable.dialog_info_title_back);
                    if(dt== DialogType.警告)rlTitle.setBackgroundResource(R.drawable.dialog_warn_title_back);
                    if(dt== DialogType.错误)rlTitle.setBackgroundResource(R.drawable.dialog_error_title_back);
                    TextView Tv=(TextView)dview.findViewById(R.id.tv_Title);
                    TextView Info=(TextView)dview.findViewById(R.id.tv_Info);
                    Button ok = (Button)dview.findViewById(R.id.btn_ok);
                    Button cancel = (Button)dview.findViewById(R.id.btn_cancel);

                    Tv.setText(dt.toString());
                    Info.setText(message);
                    ok.setText(btnName1);
                    cancel.setText(btnName2);

                    if(btn==AlertDialogButton.OK)cancel.setVisibility(View.GONE);

                    ok.setOnClickListener(view->{
                        if(action!=null)action.ExitDialog(true);
                        hide(dview);
                    });

                    cancel.setOnClickListener(view->{
                        if(action!=null)action.ExitDialog(false);
                        hide(dview);
                    });
                    dview.show();
                }
            }
        }catch(Exception e){
            MessageLog.ShowError("打开弹框异常:"+e.toString());
        }
    }

    /**
     * 对话框，自定义按钮，非阻塞
     * */
    private static void showDialog(Activity activity,String message, AlertDialogButton btn,DialogType dt, IDialogAction action) {
        if(activity==null) return;
        try{
            DialogView dview1=new DialogView(activity, R.layout.activity_dialog, R.style.custom_dialog2);
            if (dview1 != null) {
                if (!dview1.isShowing()) {

                    String btnName1="确定";
                    String btnName2="取消";
                    if(btn==AlertDialogButton.YesNoCancel||btn==AlertDialogButton.YesNo){
                        btnName1="是";
                        btnName2="否";
                    }

                    dview1.setCanceledOnTouchOutside(false);//禁用触摸其它区域关闭弹框
                    RelativeLayout rlTitle = (RelativeLayout) dview1.findViewById(R.id.rl_title);
                    if(dt== DialogType.提示)rlTitle.setBackgroundResource(R.drawable.dialog_info_title_back);
                    if(dt== DialogType.警告)rlTitle.setBackgroundResource(R.drawable.dialog_warn_title_back);
                    if(dt== DialogType.错误)rlTitle.setBackgroundResource(R.drawable.dialog_error_title_back);
                    TextView Tv=(TextView)dview1.findViewById(R.id.tv_Title);
                    TextView Info=(TextView)dview1.findViewById(R.id.tv_Info);
                    Button ok = (Button)dview1.findViewById(R.id.btn_ok);
                    Button cancel = (Button)dview1.findViewById(R.id.btn_cancel);

                    Tv.setText(dt.toString());
                    Info.setText(message);
                    ok.setText(btnName1);
                    cancel.setText(btnName2);

                    if(btn==AlertDialogButton.OK)cancel.setVisibility(View.GONE);

                    ok.setOnClickListener(view->{
                        if(action!=null)action.ExitDialog(true);
                        hide(dview1);
                    });

                    cancel.setOnClickListener(view->{
                        if(action!=null)action.ExitDialog(false);
                        hide(dview1);
                    });
                    dview1.show();
                }
            }
        }catch(Exception e){
            MessageLog.ShowError("打开弹框异常:"+e.toString());
        }
    }

    /**
     * 隐藏弹框
     * @param view
     */
    private static void hide(DialogView view) {
        if (view != null) {
            if (view.isShowing()) {
                view.dismiss();
            }
        }
    }

    /**
     * 信息提示框
     * @param message 提示信息
     * @param btn 显示按钮
     * @param action 执行回调
     */
    public static void showInfo(String message, AlertDialogButton btn, IDialogAction action) {
        if(mActivity!=null)
            mActivity.runOnUiThread(()->{showDialog(message,btn,DialogType.提示,action);});
        else showDialog(message,btn,DialogType.提示,action);
    }

    /**
     * 警告提示框
     * @param message 警告信息
     * @param btn 显示按钮
     * @param action 执行回调
     */
    public static void showWarn(String message, AlertDialogButton btn, IDialogAction action) {
        if(mActivity!=null)
            mActivity.runOnUiThread(()->{showDialog(message,btn,DialogType.警告,action);});
        else showDialog(message,btn,DialogType.警告,action);
    }

    /**
     * 错误提示框
     * @param message 错误信息
     * @param btn 显示按钮
     * @param action 执行回调
     */
    public static void showError(String message, AlertDialogButton btn, IDialogAction action) {
        if(mActivity!=null)
            mActivity.runOnUiThread(()->{showDialog(message,btn,DialogType.错误,action);});
        else showDialog(message,btn,DialogType.错误,action);
    }



    /**
     * 信息提示框
     * @param message 提示信息
     * @param btn 显示按钮
     * @param action 执行回调
     */
    public static void showInfo( Context ct,String message, AlertDialogButton btn, IDialogAction action) {
        if(mActivity!=null)
            mActivity.runOnUiThread(()->{showDialog(ct,message,btn,DialogType.提示,action);});
        else showDialog(ct,message,btn,DialogType.提示,action);
    }

    /**
     * 警告提示框
     * @param message 警告信息
     * @param btn 显示按钮
     * @param action 执行回调
     */
    public static void showWarn( Context ct,String message, AlertDialogButton btn, IDialogAction action) {
        if(mActivity!=null)
            mActivity.runOnUiThread(()->{showDialog(ct,message,btn,DialogType.警告,action);});
        else showDialog(ct,message,btn,DialogType.警告,action);
    }

    /**
     * 错误提示框
     * @param message 错误信息
     * @param btn 显示按钮
     * @param action 执行回调
     */
    public static void showError( Context ct,String message, AlertDialogButton btn, IDialogAction action) {
        if(mActivity!=null)
            mActivity.runOnUiThread(()->{showDialog(ct,message,btn,DialogType.错误,action);});
        else showDialog(ct,message,btn,DialogType.错误,action);
    }



    /**
     * 信息提示框
     * @param message 提示信息
     * @param btn 显示按钮
     * @param action 执行回调
     */
    public static void showInfo( Activity activity,String message, AlertDialogButton btn, IDialogAction action) {
        if(mActivity!=null)
            mActivity.runOnUiThread(()->{showDialog(activity,message,btn,DialogType.提示,action);});
        else showDialog(activity,message,btn,DialogType.提示,action);
    }

    /**
     * 警告提示框
     * @param message 警告信息
     * @param btn 显示按钮
     * @param action 执行回调
     */
    public static void showWarn(Activity activity,String message, AlertDialogButton btn, IDialogAction action) {
        if(mActivity!=null)
            mActivity.runOnUiThread(()->{showDialog(activity,message,btn,DialogType.警告,action);});
        else showDialog(activity,message,btn,DialogType.警告,action);
    }

    /**
     * 错误提示框
     * @param message 错误信息
     * @param btn 显示按钮
     * @param action 执行回调
     */
    public static void showError( Activity activity,String message, AlertDialogButton btn, IDialogAction action) {
        if(mActivity!=null)
            mActivity.runOnUiThread(()->{showDialog(activity,message,btn,DialogType.错误,action);});
        else showDialog(activity,message,btn,DialogType.错误,action);
    }

}

