package com.swiftcleaner.chovey.view.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.animation.ValueAnimator;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.business.admob.AdmobManager;
import com.swiftcleaner.chovey.business.admob.NativeView;
import com.swiftcleaner.chovey.databinding.ActivityCollectBinding;
import com.swiftcleaner.chovey.databinding.ActivityMainBinding;
import com.swiftcleaner.chovey.view.MainActivity;

public class CollectActivity extends AppCompatActivity {
    private ActivityCollectBinding binding;
    private ValueAnimator animator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCollectBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        initBar();
        startProgressAnimation();
        initShow();
    }

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

    private void initShow() {
        binding.scanButton.setOnClickListener(v->{
            startActivity(new Intent(CollectActivity.this,CleanJunkActivity.class));
            finish();
        });
        binding.scanSkip.setOnClickListener(v->{
            startActivity(new Intent(CollectActivity.this, MainActivity.class));
            finish();
        });
    }

    private void startProgressAnimation() {
        animator = ValueAnimator.ofInt(0, 100);
        animator.setDuration(5000); // 5秒
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int progress = (int) animation.getAnimatedValue();
                binding.scanningProgressBar.setProgress(progress);
                if (progress == 100) {
                    binding.scanCl1.setVisibility(View.GONE);
                    binding.scanCl2.setVisibility(View.VISIBLE);
                }
            }
        });
        animator.start();
    }

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


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