package com.videogo.ui.login.helper;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.core.content.ContextCompat;

import ezviz.ezopensdk.R;

/**
 * MainActivity 通用 UI 工具类
 * 包含字符串清洗、输入框清除按钮、加载动画等与页面逻辑无关的通用方法
 */
public class MainActivityHelper {

    private final Activity mActivity;
    private ViewGroup mLoginAnimVg = null;
    private boolean isShowLoginAnim = false;

    public MainActivityHelper(Activity activity) {
        mActivity = activity;
    }

    /**
     * 清洗字符串：去除空字符、换行符、不间断空格等无效字符
     */
    public String getValidText(String origin) {
        if (origin == null) {
            return null;
        }
        StringBuilder target = new StringBuilder();
        for (char ch : origin.toCharArray()) {
            if (ch == '\0' || ch == '\n' || ch == 160) {
                break;
            } else {
                target.append(ch);
            }
        }
        return target.toString();
    }

    /**
     * 显示/隐藏登录加载动画
     */
    public void showLoginAnim(final boolean show) {
        if (mLoginAnimVg == null) {
            mLoginAnimVg = mActivity.findViewById(R.id.vg_login_anim);
        }
        if (mLoginAnimVg == null) {
            return;
        }
        mActivity.runOnUiThread(() -> {
            isShowLoginAnim = show;
            mLoginAnimVg.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
        });
    }

    /**
     * 是否正在显示加载动画
     */
    public boolean isShowLoginAnim() {
        return isShowLoginAnim;
    }

    /**
     * 为输入框设置清除按钮：有内容时显示，点击清除内容
     */
    @SuppressLint("ClickableViewAccessibility")
    public void setupClearButton(final EditText editText) {
        if (editText == null) return;
        final Drawable clearIcon = ContextCompat.getDrawable(mActivity, R.drawable.login_ic_clear);
        if (clearIcon == null) return;
        clearIcon.setBounds(0, 0, clearIcon.getIntrinsicWidth(), clearIcon.getIntrinsicHeight());

        // 根据当前内容更新清除图标的显隐
        updateClearIcon(editText, clearIcon);

        editText.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) {
                updateClearIcon(editText, clearIcon);
            }
        });

        editText.setOnFocusChangeListener((v, hasFocus) -> updateClearIcon(editText, clearIcon));

        editText.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                Drawable endDrawable = editText.getCompoundDrawables()[2];
                if (endDrawable != null) {
                    int iconLeft = editText.getWidth() - editText.getPaddingRight() - endDrawable.getIntrinsicWidth();
                    if (event.getX() >= iconLeft) {
                        editText.setText("");
                        return true;
                    }
                }
            }
            return false;
        });
    }

    /**
     * 更新清除图标的显隐状态
     */
    private void updateClearIcon(EditText editText, Drawable clearIcon) {
        boolean show = editText.hasFocus() && editText.getText().length() > 0;
        editText.setCompoundDrawables(null, null, show ? clearIcon : null, null);
    }
}
