package com.mytatvarnsdk.myView.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.mytatvarnsdk.R;

/**
 * 警告弹出框定义
 *
 * @author minyuchun
 */
public class WarnDialog extends Dialog {
    /**
     * 头部标题的文字
     */
    private TextView titleMessage;

    /**
     * 显示的dialog图片
     */
    private ImageView dialogImg;
    /**
     * 显示的dialog文字
     */
    private TextView message;
    /**
     * 单只确认按钮
     */
    private Button positiveButton;
    /**
     * 选择按钮区域
     */
    private ConstraintLayout mLayout;
    /**
     * 取消按钮，确认按钮
     */
    private TextView cancelButton, confirmButton;
    /**
     * 标题以及内容写显示
     */
    private CharSequence title, content, leftText, rightText;
    /**
     * 显示警示图标状态
     */
    private boolean isShowImg = true;

    /**
     * 点击监听按钮(只有确认按钮的监听)
     */
    private OnClickListener confirmButtonClickListener;
    /***选择按钮监听(取消、确认)*/
    private OnSelectListener mSelectListener;
    /***三选一按钮监听(取消、跳过、确认)*/
    private OnChooseListener mChooseListener;

    private boolean showBottomImg = false;
    private ImageView bottomImg;

    private View line;

    private boolean isTitleBold = false;

    private int successImg = -1;


    /***
     * 初始化
     * @param context 上下文
     * @param theme 主题
     */
    public WarnDialog(Context context, int theme) {
        super(context, theme);
    }

    public WarnDialog(Context context) {
        super(context, R.style.WarnDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_warn);
        initView();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        titleMessage = findViewById(R.id.dialog_top);
        message = findViewById(R.id.message);
        positiveButton = findViewById(R.id.positiveButton);
        mLayout = findViewById(R.id.selectButton);
        line = findViewById(R.id.dialog_line);
        cancelButton = findViewById(R.id.cancel);
        confirmButton = findViewById(R.id.confirm);
        dialogImg = findViewById(R.id.dialog_image);
        bottomImg = findViewById(R.id.dialog_image_bottom);
    }


    /**
     * 刷新界面
     */
    private void refreshView() {
        if (TextUtils.isEmpty(title)) {
            titleMessage.setVisibility(View.GONE);
        } else {
            titleMessage.setVisibility(View.VISIBLE);
            titleMessage.setText(title);
        }
        if (TextUtils.isEmpty(content)) {
            message.setVisibility(View.GONE);
        } else {
            message.setVisibility(View.VISIBLE);
            message.setText(content);
        }
        if (!TextUtils.isEmpty(rightText)) {
            confirmButton.setText(rightText);
            positiveButton.setText(rightText);
        }

        if (!TextUtils.isEmpty(leftText)) {
            confirmButton.setText(leftText);
        }
        dialogImg.setVisibility(isShowImg ? View.VISIBLE : View.GONE);
        if (successImg > -1) {
            dialogImg.setImageResource(successImg);
        }
        bottomImg.setVisibility(showBottomImg ? View.VISIBLE : View.GONE);
        setCanceledOnTouchOutside(true);
        positiveButton.setVisibility(View.GONE);
        mLayout.setVisibility(View.GONE);
        if (message != null) {
            message.setTypeface(message.getTypeface(), isTitleBold ? Typeface.BOLD : Typeface.NORMAL);
        }
        if (confirmButtonClickListener != null) {
            setCanceledOnTouchOutside(false);
            positiveButton.setVisibility(View.VISIBLE);
            positiveButton.setOnClickListener(v -> {
                confirmButtonClickListener.onClick(this, DialogInterface.BUTTON_POSITIVE);
            });
        } else if (mSelectListener != null) {
            setCanceledOnTouchOutside(false);
            mLayout.setVisibility(View.VISIBLE);
            cancelButton.setVisibility(TextUtils.isEmpty(rightText) != TextUtils.isEmpty(leftText) ? View.GONE : View.VISIBLE);
            line.setVisibility(TextUtils.isEmpty(rightText) != TextUtils.isEmpty(leftText) ? View.GONE : View.VISIBLE);

            confirmButton.setOnClickListener(v -> {
                mSelectListener.confirm(this);
            });
            cancelButton.setOnClickListener(v -> {
                mSelectListener.cancel(this);
            });
        } else if (mChooseListener != null) {
            setCanceledOnTouchOutside(false);
            mLayout.setVisibility(View.VISIBLE);
            cancelButton.setVisibility(TextUtils.isEmpty(rightText) != TextUtils.isEmpty(leftText) ? View.GONE : View.VISIBLE);
            line.setVisibility(TextUtils.isEmpty(rightText) != TextUtils.isEmpty(leftText) ? View.GONE : View.VISIBLE);
            confirmButton.setOnClickListener(v -> {
                mChooseListener.confirm(this);
            });
            cancelButton.setOnClickListener(v -> {
                mChooseListener.cancel(this);
            });
        }
    }

    public void setTitleBold(boolean isTitlebold) {
        this.isTitleBold = isTitlebold;
    }

    /**
     * 设置标题
     *
     * @param title 标题文字
     * @return 返回本身
     */
    public WarnDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    /**
     * 设置警示图标
     *
     * @param isShow 是否显示图片
     * @return
     */
    public WarnDialog setImageShow(boolean isShow) {
        isShowImg = isShow;
        return this;
    }

    public WarnDialog setSuccessImg() {
        successImg = R.drawable.toast_success;
        return this;
    }

    public WarnDialog setShowBottomImg(boolean showBottomImg) {
        this.showBottomImg = showBottomImg;
        return this;
    }

    public WarnDialog setBottomText(String rightText, String leftText) {
        this.rightText = rightText;
        this.leftText = leftText;
        return this;
    }

    /**
     * 设置文字显示内容
     *
     * @param content 文字显示内容
     * @return 返回本身
     */
    public WarnDialog setContent(CharSequence content) {
        this.content = content;
        return this;
    }

    /**
     * 设置确认按钮监听
     *
     * @param confirmButtonClickListener 监听器
     * @return 返回本身
     */
    public WarnDialog setConfirmButtonClickListener(OnClickListener confirmButtonClickListener) {
        this.confirmButtonClickListener = confirmButtonClickListener;
        return this;
    }

    /**
     * 设置选择按钮监听
     *
     * @param selectListener 监听器
     * @return 返回本身
     */
    public WarnDialog setSelectListener(OnSelectListener selectListener) {
        this.mSelectListener = selectListener;
        return this;
    }

    /**
     * 设置三选一按钮监听
     *
     * @param chooseListener 监听器
     * @return 返回本身
     */
    public WarnDialog setChooseListener(OnChooseListener chooseListener) {
        this.mChooseListener = chooseListener;
        return this;
    }

    public WarnDialog setOutTouchable(boolean touchable) {
        setCanceledOnTouchOutside(touchable);
        return this;
    }


    @Override
    public void show() {
        super.show();
        refreshView();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        isTitleBold = false;
    }

    /**
     * 选择按钮
     */
    public interface OnSelectListener {
        /**
         * 确认
         *
         * @param dialog
         */
        void confirm(DialogInterface dialog);

        /**
         * 取消
         *
         * @param dialog
         */
        void cancel(DialogInterface dialog);
    }

    /**
     * 三选一选择按钮
     */
    public interface OnChooseListener {
        /**
         * 确认
         *
         * @param dialog
         */
        void confirm(DialogInterface dialog);

        /**
         * 跳过
         *
         * @param dialog
         */
        void skip(DialogInterface dialog);

        /**
         * 取消
         *
         * @param dialog
         */
        void cancel(DialogInterface dialog);
    }
}
