package com.paint.sweet.drawing.sweetactivity;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

import com.ad.toponlibrary.AdManager;
import com.paint.sweet.drawing.databinding.ActivitySplashScreenBinding;
import com.paint.sweet.drawing.sweetmanager.AppUtils;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;

public class SplashScreenActivity extends AppCompatActivity {

    private ActivitySplashScreenBinding binding;
    // 核心修改1：缩短总加载时长
    private long time = 2000;
    private CountDownTimer countDownTimer;
    private static final long SPLASH_TIME_OUT = 15000; // 总时长15秒


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ========== 新增：状态栏透明+白色文字适配 ==========
        // 1. 隐藏系统标题栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        // 2. Android 5.0+ 状态栏透明 + 白色文字/图标适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            // 清除全屏Flag，保留状态栏显示
            window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

            // 分版本适配
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // 6.0+ 直接设置布局延伸+移除深色文字标志
                window.getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // 布局延伸到状态栏
                );
            } else {
                // 5.0-5.1 兼容配置
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }

            // 强制设置状态栏为全透明
            window.setStatusBarColor(getResources().getColor(android.R.color.transparent));
        }

        // 3. 设置状态栏文字为白色（false代表非深色）
        AppUtils.setStatusBarTextColor(this, false);

        // ========== 原有业务逻辑（完全保留） ==========
        binding = ActivitySplashScreenBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 初始化进度条
        binding.progressBar.setProgress(0);
        binding.tvProgress.setText("0%");

        // 开始倒计时和进度更新
        startCountDown();
    }

    private void startCountDown() {
        countDownTimer =  AdManager.showWelcomeAd(this, SPLASH_TIME_OUT, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                int progressPercentage = (int) ((100 * aLong) / SPLASH_TIME_OUT);
                int Percentage = 100 - progressPercentage;
                binding.progressBar.setProgress(Percentage);
                binding.tvProgress.setText(Percentage + "%");
                return null;
            }

        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                Intent intent = new Intent(SplashScreenActivity.this, HomeMainActivity.class);
                startActivity(intent);
                finish();
                return null;
            }
        });
        countDownTimer.start();
//        // 核心修改2：缩短进度更新间隔
//        countDownTimer = new CountDownTimer(time, 30) {
//            @Override
//            public void onFinish() {
//                // 确保显示100%
//                updateProgress(100);
//                // 核心修改3：缩短跳转延迟
//                binding.getRoot().postDelayed(() -> enterMain(), 100);
//            }

//            @Override
//            public void onTick(long millisUntilFinished) {
//                // 计算当前进度百分比
//                long elapsedTime = time - millisUntilFinished;
//                int progress = (int) (elapsedTime * 100 / time);
//
//                // 更新进度
//                updateProgress(progress);
//            }
//        };
//
//        countDownTimer.start();
    }

//    private void updateProgress(int progress) {
//        // 确保进度在0-100范围内
//        int safeProgress = Math.max(0, Math.min(100, progress));
//
//        // 优化：添加平滑动画，让进度条加载更流畅
//        ValueAnimator progressAnimator = ValueAnimator.ofInt(binding.progressBar.getProgress(), safeProgress);
//        progressAnimator.setDuration(20); // 超短动画，无卡顿感
//        progressAnimator.addUpdateListener(animation -> {
//            binding.progressBar.setProgress((int) animation.getAnimatedValue());
//            // 更新文字显示（仅数字+%）
//            binding.tvProgress.setText(animation.getAnimatedValue() + "%");
//        });
//        progressAnimator.start();
//    }

    private void enterMain() {
        Intent intent = new Intent(SplashScreenActivity.this, HomeMainActivity.class);
        startActivity(intent);
        // 添加过渡动画
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        finish();
    }

    // ========== 新增：页面恢复时重置状态栏状态 ==========
    @Override
    protected void onResume() {
        super.onResume();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            );
            // 重新确认状态栏文字为白色
            AppUtils.setStatusBarTextColor(this, false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}