package com.file.clean.q.view.activity;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;

import com.file.clean.q.business.EventUtils;
import com.file.clean.q.business.admob.AdmobManager;

import com.file.clean.q.view.BaseActivity;
import com.file.clean.q.view.dialog.CustomScoreDialog;
import com.file.clean.q.view.dialog.LoadingDialog;
import com.gyf.immersionbar.ImmersionBar;
import com.file.clean.q.databinding.ActivityEndCleanJunkBinding;


public class EndCleanJunkActivity extends BaseActivity<ActivityEndCleanJunkBinding> {
    private CountDownTimer countDownTimer;
    private ValueAnimator animator;
    @Override
    protected ActivityEndCleanJunkBinding getViewBinding() {
        return ActivityEndCleanJunkBinding.inflate(getLayoutInflater());
    }

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

    @Override
    protected void initData() {
        EventUtils.INSTANCE.event("page_EndCleanJunkActivity", "", null);
        initPage();
        initShow();
    }
    private void initPage() {
        binding.clRadarview.setVisibility(View.VISIBLE);
        binding.radarview.setSearching(true);
        initAnimotorPer();
        countDownTimer = new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                binding.radarview.setSearching(false);
                countDownTimer.cancel();
                animator.cancel();
                binding.clRadarview.setVisibility(View.GONE);
                LoadingDialog loadingDialog = new LoadingDialog(EndCleanJunkActivity.this);
                loadingDialog.show();
                AdmobManager.INSTANCE.showInterstitialAd(EndCleanJunkActivity.this, () -> {
                            return null;
                        },
                        () -> {
                            loadingDialog.dismiss();
                            return null;
                        },
                        () -> {
                            loadingDialog.dismiss();
                            return null;
                        }, false);
            }
        }.start();
    }
    private void initAnimotorPer(){
        // 创建ValueAnimator，从1到100
        animator = ValueAnimator.ofInt(1, 100);
        animator.setDuration(3000); // 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 initShow() {
        Intent intent = getIntent();
        String stringExtra = intent.getStringExtra("cleanstr");
        binding.endCleanJunkFileNum.setText("Cleaned "+ stringExtra +" junk files");
        binding.ivReturn.setOnClickListener(v->{
            initCustDialog();
        });
        binding.buttonBatteryInfoCheckNo.setOnClickListener(v->{
            startActivity(new Intent(EndCleanJunkActivity.this, InfoActivity.class));
            finish();
        });
        binding.buttonAppManagerCheckNo.setOnClickListener(v->{
            startActivity(new Intent(EndCleanJunkActivity.this, BatteryInfoActivity.class));
            finish();
        });
        binding.buttonWhatsappCheckNo.setOnClickListener(v->{
            startActivity(new Intent(EndCleanJunkActivity.this, WhatsAppActivity.class));
            finish();
        });
        binding.buttonAppProcessCheckNo.setOnClickListener(v->{
            startActivity(new Intent(EndCleanJunkActivity.this, AppProcessActivity.class));
            finish();
        });
        binding.buttonCleanJunkCleanNo.setOnClickListener(v->{
            startActivity(new Intent(EndCleanJunkActivity.this, CleanJunkActivity.class));
            finish();
        });
        binding.buttonLargefilecleanerCleanUp.setOnClickListener(v->{
            startActivity(new Intent(EndCleanJunkActivity.this, LargeFileActivity.class));
            finish();
        });
        binding.buttonSimilarPhotos.setOnClickListener(v->{
            startActivity(new Intent(EndCleanJunkActivity.this, SimilarPhotosActivity.class));
            finish();
        });
        binding.buttonScreenshot.setOnClickListener(v->{
            startActivity(new Intent(EndCleanJunkActivity.this, ScreenShotActivity.class));
            finish();
        });
    }

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

    @Override
    public void onBackPressed() {
        initCustDialog();
    }

    private void initCustDialog(){
        CustomScoreDialog customScoreDialog = new CustomScoreDialog(this);
        customScoreDialog.setOnDialogClickListener(new CustomScoreDialog.OnDialogClickListener() {
            @Override
            public void onCancel() {
                customScoreDialog.dismiss();
            }

            @Override
            public void onSubmit() {
                if (AdmobManager.INSTANCE.isShowAdBackInter()){
                    AdmobManager.INSTANCE.showInterstitialAd(EndCleanJunkActivity.this, () -> {
                                finish();
                                return null;
                            },
                            () -> {
                                return null;
                            },
                            () -> {
                                finish();
                                return null;
                            },false);
                }else {
                    finish();
                }
            }
        });
        customScoreDialog.show();
    }

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