package com.swiftcleaner.chovey.view.activity;

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

import com.gyf.immersionbar.ImmersionBar;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.business.EventUtils;
import com.swiftcleaner.chovey.business.admob.AdmobManager;
import com.swiftcleaner.chovey.databinding.ActivityEndCleanJunkBinding;
import com.swiftcleaner.chovey.view.BaseActivity;
import com.swiftcleaner.chovey.view.BaseActivityKotlin;
import com.swiftcleaner.chovey.view.dialog.CustomScoreDialog;
import com.swiftcleaner.chovey.view.dialog.LoadingDialog;

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();
    }

    @Override
    protected void onResume() {
        super.onResume();
        AdmobManager.INSTANCE.showNativeAd(EndCleanJunkActivity.this, (nativeAd) -> {
            binding.flAdDonghua.removeAllViews();
            binding.flAdDonghua.setNativeAd(nativeAd, R.layout.layout_ad_native);
            return null;
        }, null, null);
    }

    private void initPage() {
        binding.clRadarview.setVisibility(View.VISIBLE);

        binding.radarview.setSearching(true);
        initAnimotorPer();
        countDownTimer = new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                binding.radarview.setSearching(false);
                if (animator!= null) {
                    animator.cancel();
                }
                binding.clRadarview.setVisibility(View.GONE);
                LoadingDialog loadingDialog = new LoadingDialog(EndCleanJunkActivity.this);
                loadingDialog.show();
                if (AdmobManager.INSTANCE.isShowAdInter()) {
                    AdmobManager.INSTANCE.showInterstitialAd(EndCleanJunkActivity.this, () -> {
                                loadingDialog.dismiss();
                                binding.flAdDonghua.removeAllViews();
                                return null;
                            },
                            () -> {
                                loadingDialog.dismiss();
                                binding.flAdDonghua.removeAllViews();
                                return null;
                            },
                            () -> {
                                loadingDialog.dismiss();
                                binding.flAdDonghua.removeAllViews();
                                return null;
                            }, false);
                }else {
                    loadingDialog.dismiss();
                    binding.flAdDonghua.removeAllViews();
                }
            }
        }.start();
        AdmobManager.INSTANCE.showNativeAd(EndCleanJunkActivity.this, (nativeAd) -> {
            binding.flAd.removeAllViews();
            binding.flAd.setNativeAd(nativeAd, R.layout.layout_ad_native);
            return null;
        }, null, null);

    }

    private void initAnimotorPer() {
        animator = ValueAnimator.ofInt(1, 100);
        animator.setDuration(5000);
        animator.addUpdateListener(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();
        }
    }
}