package com.videogo.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import ezviz.ezopensdk.R;

/**
 * iOS 风格简易弹框组件
 * 模拟 iOS UIAlertController 的视觉效果：
 * 居中圆角卡片 + 消息文本 + 底部两个按钮（取消/确定）
 */
public class IOSAlertDialog {

    private final Dialog mDialog;
    private final TextView mMessageTv;
    private final TextView mNegativeBtn;
    private final TextView mPositiveBtn;

    private IOSAlertDialog(Context context) {
        mDialog = new Dialog(context, R.style.IOSAlertDialogStyle);
        mDialog.setContentView(R.layout.dialog_ios_alert);
        mDialog.setCanceledOnTouchOutside(true);

        mMessageTv = mDialog.findViewById(R.id.tv_ios_dialog_message);
        mNegativeBtn = mDialog.findViewById(R.id.btn_ios_dialog_negative);
        mPositiveBtn = mDialog.findViewById(R.id.btn_ios_dialog_positive);

        // 设置弹框居中显示
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
        }
    }

    public void show() {
        Context context = mDialog.getContext();
        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return;
        }
        mDialog.show();
    }

    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    /**
     * Builder 模式构建弹框
     */
    public static class Builder {
        private final Context mContext;
        private CharSequence mMessage;
        private CharSequence mNegativeText;
        private CharSequence mPositiveText;
        private View.OnClickListener mNegativeListener;
        private View.OnClickListener mPositiveListener;
        private boolean mCancelable = true;

        public Builder(Context context) {
            mContext = context;
        }

        /** 设置弹框消息 */
        public Builder setMessage(CharSequence message) {
            mMessage = message;
            return this;
        }

        /** 通过资源 ID 设置弹框消息 */
        public Builder setMessage(int resId) {
            mMessage = mContext.getString(resId);
            return this;
        }

        /** 设置取消按钮 */
        public Builder setNegativeButton(CharSequence text, View.OnClickListener listener) {
            mNegativeText = text;
            mNegativeListener = listener;
            return this;
        }

        /** 通过资源 ID 设置取消按钮 */
        public Builder setNegativeButton(int resId, View.OnClickListener listener) {
            mNegativeText = mContext.getString(resId);
            mNegativeListener = listener;
            return this;
        }

        /** 设置确定按钮 */
        public Builder setPositiveButton(CharSequence text, View.OnClickListener listener) {
            mPositiveText = text;
            mPositiveListener = listener;
            return this;
        }

        /** 通过资源 ID 设置确定按钮 */
        public Builder setPositiveButton(int resId, View.OnClickListener listener) {
            mPositiveText = mContext.getString(resId);
            mPositiveListener = listener;
            return this;
        }

        /** 设置是否可以点击外部取消 */
        public Builder setCancelable(boolean cancelable) {
            mCancelable = cancelable;
            return this;
        }

        /** 构建并返回弹框实例 */
        public IOSAlertDialog build() {
            IOSAlertDialog dialog = new IOSAlertDialog(mContext);

            if (mMessage != null) {
                dialog.mMessageTv.setText(mMessage);
            }

            if (mNegativeText != null) {
                dialog.mNegativeBtn.setText(mNegativeText);
                dialog.mNegativeBtn.setOnClickListener(v -> {
                    dialog.dismiss();
                    if (mNegativeListener != null) {
                        mNegativeListener.onClick(v);
                    }
                });
            }

            if (mPositiveText != null) {
                dialog.mPositiveBtn.setText(mPositiveText);
                dialog.mPositiveBtn.setOnClickListener(v -> {
                    dialog.dismiss();
                    if (mPositiveListener != null) {
                        mPositiveListener.onClick(v);
                    }
                });
            }

            dialog.mDialog.setCancelable(mCancelable);
            dialog.mDialog.setCanceledOnTouchOutside(mCancelable);

            return dialog;
        }

        /** 构建并直接显示弹框 */
        public IOSAlertDialog show() {
            IOSAlertDialog dialog = build();
            dialog.show();
            return dialog;
        }
    }
}
