package com.videogo.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.videogo.constant.Constant;

import ezviz.ezopensdk.R;

public class VerifyCodeInput {

    public interface VerifyCodeInputListener {

        void onInputVerifyCode(final String verifyCode);
    }

    public interface VerifyCodeErrorListener{

        void verifyCodeError();
    }

    private static Dialog mPasswordDialog = null;

    /**
     * 创建仿 iOS 风格的验证码输入弹框
     * @param context 上下文
     * @param l 验证码输入回调
     * @return Dialog 实例（兼容原有调用方式）
     */
    public static Dialog VerifyCodeInputDialog(final Context context, final VerifyCodeInputListener l) {
        // 创建 iOS 风格弹框
        final Dialog dialog = new Dialog(context, R.style.IOSAlertDialogStyle);
        dialog.setContentView(R.layout.dialog_ios_verify_code_input);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);

        // 设置弹框居中显示
        Window window = dialog.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);
            // 弹出软键盘
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                    | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }

        // 绑定视图
        final TextView titleTv = dialog.findViewById(R.id.tv_ios_dialog_title);
        final TextView messageTv = dialog.findViewById(R.id.tv_ios_dialog_message);
        final EditText inputEt = dialog.findViewById(R.id.et_ios_dialog_input);
        final ImageView clearIv = dialog.findViewById(R.id.iv_ios_dialog_clear);
        final TextView negativeBtn = dialog.findViewById(R.id.btn_ios_dialog_negative);
        final TextView positiveBtn = dialog.findViewById(R.id.btn_ios_dialog_positive);

        // 设置标题和消息
        titleTv.setText(R.string.verify_code_dialog_title);
        messageTv.setText(R.string.verify_code_dialog_message);

        // 设置输入框属性（文本可见）
        inputEt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(Constant.PSW_MAX_LENGTH)});
        inputEt.setInputType(InputType.TYPE_CLASS_TEXT);

        // 设置按钮文本（取消在左，确定在右）
        negativeBtn.setText(R.string.cancel);
        positiveBtn.setText(R.string.confirm);

        // 初始状态：确定按钮不可点击
        positiveBtn.setEnabled(false);
        positiveBtn.setTextColor(0x80007AFF);

        // 监听输入变化
        inputEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                boolean hasInput = s.length() > 0;
                // 输入框有内容时显示清空图标
                clearIv.setVisibility(hasInput ? View.VISIBLE : View.GONE);
                // 输入框有内容时确定按钮可点击
                positiveBtn.setEnabled(hasInput);
                positiveBtn.setTextColor(hasInput ? 0xFF007AFF : 0x80007AFF);
            }
        });

        // 清空按钮点击事件
        clearIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputEt.setText("");
            }
        });

        // 取消按钮点击事件
        negativeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        // 确定按钮点击事件
        positiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String verifyCode = inputEt.getText().toString();
                if (TextUtils.isEmpty(verifyCode)) {
                    return;
                }
                dialog.dismiss();
                l.onInputVerifyCode(verifyCode);
            }
        });

        return dialog;
    }
}
