package com.draw.painting.line.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;

import com.draw.painting.line.databinding.ActivitySplashBinding;
import com.draw.painting.line.manager.AppHelper;

public class SplashActivity extends AppCompatActivity {

    private ActivitySplashBinding binding;
    private Handler handler = new Handler();
    private boolean isAnimationComplete = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        AppHelper.enableEdgeToEdge(this);
        binding = ActivitySplashBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        AppHelper.setupWindowInsetsListener(binding.getRoot());
        
        // 等待布局完成后再启动动画
        binding.getRoot().post(new Runnable() {
            @Override
            public void run() {
                startLaunchAnimation();
            }
        });
    }

    private void startLaunchAnimation() {
        // 启动鹿的绘制动画（使用XML Vector Drawable路径）
        binding.deerView.startAnimation();
        
        // 同步加载条：0-80% 与鹿的动画同步
        // 新版可爱小鹿动画总时长 2.2s，加载条在鹿画到约80%时到达80%
        animateProgressBar(0f, 0.8f, 2200, new Runnable() {
            @Override
            public void run() {
                // 等待真实加载完成（这里可以添加实际的加载逻辑）
                waitForLoading();
            }
        });
    }

    private void animateProgressBar(float from, float to, long duration, Runnable onComplete) {
        ViewGroup.LayoutParams params = binding.progressBar.getLayoutParams();
        int maxWidth = binding.progressContainer.getWidth();
        
        ValueAnimator animator = ValueAnimator.ofFloat(from, to);
        animator.setDuration(duration);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float progress = (float) animation.getAnimatedValue();
                params.width = (int) (maxWidth * progress);
                binding.progressBar.setLayoutParams(params);
            }
        });
        
        if (onComplete != null) {
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    onComplete.run();
                }
            });
        }
        
        animator.start();
    }

    private void waitForLoading() {
        // 模拟等待真实加载（实际项目中这里应该是真实的加载逻辑）
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                completeLoading();
            }
        }, 500); // 等待500ms
    }

    private void completeLoading() {
        if (isAnimationComplete) return;
        isAnimationComplete = true;
        
        // 快速完成加载条到100%
        ViewGroup.LayoutParams params = binding.progressBar.getLayoutParams();
        int maxWidth = binding.progressContainer.getWidth();
        
        ValueAnimator animator = ValueAnimator.ofFloat(0.8f, 1.0f);
        animator.setDuration(200);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float progress = (float) animation.getAnimatedValue();
                params.width = (int) (maxWidth * progress);
                binding.progressBar.setLayoutParams(params);
            }
        });
        
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // 0.2s 淡出
                fadeOutAndEnter();
            }
        });
        
        animator.start();
    }

    private void fadeOutAndEnter() {
        ObjectAnimator fadeOut = ObjectAnimator.ofFloat(binding.getRoot(), "alpha", 1f, 0f);
        fadeOut.setDuration(200);
        fadeOut.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                enterMain();
            }
        });
        fadeOut.start();
    }

    private void enterMain() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}

