package xn.hxp.weidith;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import androidx.appcompat.widget.AppCompatImageView;

import xn.hxp.R;

public class SwitchImageView extends AppCompatImageView {
    private static final long DURATION = 120_000; // 120秒
    private int defaultSrc;
    private int switchedSrc;
    private final Handler handler = new Handler();
    private Runnable restoreTask;
    private boolean isSwitched = false;

    public SwitchImageView(Context context) {
        super(context);
        init(context, null);
    }

    public SwitchImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SwitchImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        // 读取自定义属性
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SwitchImageView);
        defaultSrc = ta.getResourceId(R.styleable.SwitchImageView_defaultSrc, 0);
        switchedSrc = ta.getResourceId(R.styleable.SwitchImageView_switchedSrc, 0);
        ta.recycle();

        setImageResource(defaultSrc);
        setupClickListener();
    }
    private void setupClickListener() {
        setOnClickListener(v -> switchImageWithAnimation());
    }

    private void switchImageWithAnimation() {
        if (isSwitched) return; // 防止重复点击

        // 淡出动画
        AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setDuration(300);
        fadeOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                setImageResource(switchedSrc);
                // 淡入动画
                AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
                fadeIn.setDuration(300);
                startAnimation(fadeIn);

                scheduleRestore();
                isSwitched = true;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });

        startAnimation(fadeOut);
    }

    private void scheduleRestore() {
        // 取消之前的任务（如果有）
        if (restoreTask != null) {
            handler.removeCallbacks(restoreTask);
        }

        restoreTask = () -> {
            AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
            fadeOut.setDuration(300);
            fadeOut.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {}

                @Override
                public void onAnimationEnd(Animation animation) {
                    setImageResource(defaultSrc);
                    AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
                    fadeIn.setDuration(300);
                    startAnimation(fadeIn);
                    isSwitched = false;
                }

                @Override
                public void onAnimationRepeat(Animation animation) {}
            });
            startAnimation(fadeOut);
        };

        handler.postDelayed(restoreTask, DURATION);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        // 移除回调防止内存泄漏
        if (restoreTask != null) {
            handler.removeCallbacks(restoreTask);
        }
    }

    // 可选：手动恢复方法
    public void restoreImmediately() {
        if (restoreTask != null) {
            handler.removeCallbacks(restoreTask);
        }
        setImageResource(defaultSrc);
        isSwitched = false;
    }
}
