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 androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.clean.file.b.business.newAdmob.AdmobHelps;
import com.gyf.immersionbar.ImmersionBar;
import com.swiftcleaner.chovey.CleanApplication;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.business.EventUtils;
import com.swiftcleaner.chovey.business.admob.AdmobManager;
import com.swiftcleaner.chovey.databinding.ActivityGuideBinding;
import com.swiftcleaner.chovey.view.MainActivity;
import com.swiftcleaner.chovey.view.adapter.ViewPagerAdapter;
import com.swiftcleaner.chovey.view.dialog.LoadingDialog;

public class GuideActivity extends AppCompatActivity {
    private ViewPagerAdapter adapter;
    ActivityGuideBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityGuideBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        initBar();
        EventUtils.INSTANCE.event("page_GuideActivity", "", null);
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        binding.viewPager.setAdapter(adapter);
        binding.viewPager.setPagingEnabled(false);
        binding.viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // 页面切换时处理逻辑
            }
        });
    }

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

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void nextPage() {
        if (adapter == null || adapter.getCount() == 0) {
            Toast.makeText(this, "No pages available", Toast.LENGTH_SHORT).show();
            return;
        }

        int currentPosition = binding.viewPager.getCurrentItem();
        if (currentPosition < adapter.getCount() - 1) {
            binding.viewPager.setCurrentItem(currentPosition + 1, true);
        } else {
            if (AdmobHelps.INSTANCE.isShowAdInter()) {
                AdmobHelps.INSTANCE.showInterstitialAd(
                        GuideActivity.this,
                        () -> {
                            startIntent();
                            // 当广告被关闭时的逻辑
                            Log.d("AdmobHelps", "Interstitial Ad was dismissed.");
                            return null;
                        },
                        () -> {
                            // 当广告展示完成时的逻辑
                            Log.d("AdmobHelps", "Interstitial Ad was shown.");
                            return null;
                        },
                        () -> {
                            startIntent();
                            // 当广告加载失败时的逻辑
                            Log.d("AdmobHelps", "Interstitial Ad failed to load.");
                            return null;
                        },
                        false
                );
            }else {
                startIntent();
            }
        }
    }
    private void startIntent() {
        startActivity(new Intent(GuideActivity.this, CollectActivity.class));
        finish();  // 关闭当前的 GuideActivity
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (binding.viewPager != null) {
            binding.viewPager.clearOnPageChangeListeners();
        }
    }
}