package com.swiftcleaner.chovey.view.activity;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.CountDownTimer;
import android.util.Log;

import com.clean.file.b.business.newAdmob.AdmobHelps;
import com.gyf.immersionbar.ImmersionBar;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.databinding.ActivityAnimatorBinding;
import com.swiftcleaner.chovey.view.BaseActivity;

public class AnimatorActivity extends BaseActivity<ActivityAnimatorBinding> {


    private boolean isAnimatorCancelled = false;
    private boolean isTimerCancelled = false;
    private ValueAnimator animator;
    private CountDownTimer countDownTimer;
    private Intent startIntent;
    @Override
    protected ActivityAnimatorBinding getViewBinding() {
        return ActivityAnimatorBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        initBar();
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        int animatorIntent = intent.getIntExtra("animator", 0);
        switch (animatorIntent) {
            case 1:
                binding.tupian.setBackgroundResource(R.mipmap.clean_img);
                startIntent = new Intent(this, CleanJunkActivity.class);
                break;
            case 2:
                binding.tupian.setBackgroundResource(R.mipmap.dianchi_img);
                startIntent = new Intent(this, InfoActivity.class);
                break;
            case 3:
                binding.tupian.setBackgroundResource(R.mipmap.large_file_img);
                startIntent = new Intent(this, LargeFileActivity.class);
                break;
            case 4:
                binding.tupian.setBackgroundResource(R.mipmap.screenshot_img);
                startIntent = new Intent(this, ScreenShotActivity.class);
                break;
            case 5:
                String stringExtra = intent.getStringExtra("cleanstr");
                String title = intent.getStringExtra("title");
                binding.tupian.setBackgroundResource(R.mipmap.clean_img);
                startIntent = new Intent(this, EndCleanJunkActivity.class);
                startIntent.putExtra("cleanstr", stringExtra);
                startIntent.putExtra("title",title);
                break;
            case 6:
                binding.tupian.setBackgroundResource(R.mipmap.appprocess_img);
                startIntent = new Intent(this, AppProcessActivity.class);
                break;
            case 7:
                binding.tupian.setBackgroundResource(R.mipmap.app_manager_img);
                startIntent = new Intent(this, BatteryInfoActivity.class);
                break;
            case 8:
                binding.tupian.setBackgroundResource(R.mipmap.whatsapp_img);
                startIntent = new Intent(this, WhatsAppActivity.class);
                break;
            default:
                Log.e("AnimatorActivity", "Invalid animator intent value: " + animatorIntent);
        }
        initPage();
    }

    private void initPage() {
        AdmobHelps.INSTANCE.showNativeAd(this,binding.flAd,R.layout.layout_ad_native_small,AdmobHelps.INSTANCE.isShowAdNativeAnimator(),false);
        binding.radarview.setSearching(true);
        initAnimotor();
        countDownTimer = new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                binding.radarview.setSearching(false);
                countDownTimer.cancel();
                animator.cancel();
                if (AdmobHelps.INSTANCE.isShowAdInter()) {
                    AdmobHelps.INSTANCE.showInterstitialAd(
                            AnimatorActivity.this,
                            () -> {
                                startInt();
                                // 当广告被关闭时的逻辑
                                Log.d("AdmobHelps", "Interstitial Ad was dismissed.");
                                return null;
                            },
                            () -> {
                                // 当广告展示完成时的逻辑
                                Log.d("AdmobHelps", "Interstitial Ad was shown.");
                                return null;
                            },
                            () -> {
                                startInt();
                                // 当广告加载失败时的逻辑
                                Log.d("AdmobHelps", "Interstitial Ad failed to load.");
                                return null;
                            },
                            false
                    );
                }else {
                    startInt();
                }
            }
        }.start();
    }

    private void startInt() {
        if (startIntent != null) {
            startActivity(startIntent);
            finish();
        } else {
            Log.e("AnimatorActivity", "Intent for next activity is null. Cannot start activity.");
            finish(); // 如果 intent1 为 null，直接结束当前 Activity
        }
    }


    private void initAnimotor() {
        // 创建ValueAnimator，从1到100
        animator = ValueAnimator.ofInt(1, 100);
        animator.setDuration(5000); // 3秒

        // 设置更新监听器，每帧更新进度文本
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int currentValue = (int) animation.getAnimatedValue();
                binding.parText.setText(currentValue + "%");
            }
        });
        // 开始动画
        animator.start();
    }

    private void initBar() {
        ImmersionBar.with(this).transparentStatusBar()  // 透明状态栏
                .statusBarDarkFont(false)  // 状态栏字体颜色为浅色
                .navigationBarDarkIcon(true)  // 导航栏图标颜色为浅色
                .init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (binding.radarview != null) {
            binding.radarview.setSearching(false);
        }

        if (countDownTimer != null && !isTimerCancelled) {
            countDownTimer.cancel();
            isTimerCancelled = true;
        }

        if (animator != null && !isAnimatorCancelled) {
            animator.cancel();
            isAnimatorCancelled = true;
        }
    }
}