package com.swiftcleaner.chovey.view;

import static com.mbridge.msdk.foundation.same.e.a.id;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.animation.ValueAnimator;
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.CountDownTimer;
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.CleanApplication;
import com.swiftcleaner.chovey.GlobalConfig;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.business.admob.AdmobManager;
import com.swiftcleaner.chovey.business.push.NotificationUtils;
import com.swiftcleaner.chovey.databinding.ActivityMainBinding;
import com.swiftcleaner.chovey.model.tool.MemoryUtils;
import com.swiftcleaner.chovey.model.util.PermissionUtils;
import com.swiftcleaner.chovey.view.activity.AppProcessActivity;
import com.swiftcleaner.chovey.view.activity.BatteryInfoActivity;
import com.swiftcleaner.chovey.view.activity.CleanJunkActivity;
import com.swiftcleaner.chovey.view.activity.InfoActivity;
import com.swiftcleaner.chovey.view.activity.LargeFileActivity;
import com.swiftcleaner.chovey.view.activity.ScreenShotActivity;
import com.swiftcleaner.chovey.view.activity.SettingActivity;
import com.swiftcleaner.chovey.view.activity.SimilarPhotosActivity;
import com.swiftcleaner.chovey.view.activity.StartActivity;
import com.swiftcleaner.chovey.view.activity.WhatsAppActivity;
import com.swiftcleaner.chovey.view.dialog.AppExitDialog;
import com.swiftcleaner.chovey.view.dialog.CustomBottomSheetDialog;
import com.swiftcleaner.chovey.view.dialog.LoadingDialog;
import com.swiftcleaner.chovey.view.dialog.NoticeDialog;

import java.util.concurrent.atomic.AtomicBoolean;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;
    private ActivityResultLauncher<String[]> multiplePermissionsLauncher;
    private ActivityResultLauncher<Intent> notificationSettingsLauncher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        initBar();
        initPermission();
        initShow();
    }

    private void initShow() {
        //判断是否有权限
//        NotificationUtils.INSTANCE.sendNotification(CleanApplication.context, GlobalConfig.ID_CLEAN_JUNK, "app_receiver");
        binding.lottieAnimattionView.setAnimation(R.raw.cleaner_junk_found_animation);
        binding.lottieAnimattionView.playAnimation();
        // 获取总内存
        String totalMemory = MemoryUtils.getUsedDiskMemory(this);

        // 获取当前使用内存
        String usedMemory = MemoryUtils.getTotalDiskMemory(this);
        //内存赋值
        binding.phoneMemory.setText(totalMemory + "/" + usedMemory);

        binding.cleanUp.setOnClickListener(v -> {
            intTempJump();
        });
        binding.ivSetting.setOnClickListener(v -> {
//            AdmobManager.INSTANCE.showInterstitialAd(this, () -> {
//                        initSetting();
//                        return null;
//                    },
//                    () -> {
//                        return null;
//                    },
//                    () -> {
//                        initSetting();
//                        return null;
//                    });
            initSetting();

        });
        binding.cl3CleanJunk.setOnClickListener(v -> {
            intTempJump();
        });
        binding.cl3AppManager.setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, BatteryInfoActivity.class));
        });
        binding.cl3BatteryInfo.setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, InfoActivity.class));
        });
        binding.appProcess.setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, AppProcessActivity.class));
        });
        binding.largeFileCleaner.setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, LargeFileActivity.class));
        });
        binding.screenshotCleaner.setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, ScreenShotActivity.class));
        });
        binding.whatsApp.setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, WhatsAppActivity.class));
        });
        binding.similarPhotos.setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, SimilarPhotosActivity.class));
        });
    }
    private void initPermission() {
        multiplePermissionsLauncher = registerForActivityResult(
                new ActivityResultContracts.RequestMultiplePermissions(),
                result -> {
                    if (result.get(Manifest.permission.POST_NOTIFICATIONS) != null && result.get(Manifest.permission.POST_NOTIFICATIONS)) {

                    }
                }
        );


        notificationSettingsLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    // 处理结果
                }
        );
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
                NoticeDialog noticeDialog = new NoticeDialog(this);
                noticeDialog.setOnDialogClickListener(new NoticeDialog.OnDialogClickListener() {
                    @Override
                    public void onCancel() {
                        noticeDialog.cancel();
                    }

                    @Override
                    public void onSubmit() {
                        // 请求通知权限
                        PermissionUtils.requestNotificationPermission(MainActivity.this, notificationSettingsLauncher, multiplePermissionsLauncher);
                        noticeDialog.cancel();
                    }
                });
                noticeDialog.show();
            }

        }



    }
    private void intTempJump() {
        Intent intent = new Intent(this, CleanJunkActivity.class);
        startActivity(intent);
    }

    private void initSetting() {
        Intent intent = new Intent(this, SettingActivity.class);
        startActivity(intent);
    }

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

    @Override
    public void onBackPressed() {
        AppExitDialog appExitDialog = new AppExitDialog(this);
        appExitDialog.setOnDialogClickListener(new AppExitDialog.OnDialogClickListener() {
            @Override
            public void onCancel() {
                appExitDialog.dismiss();
            }

            @Override
            public void onSubmit() {
                finish();
                System.exit(0); // 完全退出应用
            }
        });
        appExitDialog.show();
    }

    private AtomicBoolean bannerShowed = new AtomicBoolean(false);

    @Override
    protected void onResume() {
        super.onResume();
        if (binding.lottieAnimattionView != null) {
            binding.lottieAnimattionView.resumeAnimation(); // 恢复动画
        }

        if (bannerShowed.get()) return;
        bannerShowed.set(true);
        AdmobManager.INSTANCE.showBannerAd(this, binding.flBanner, "bottom", null, null);
    }

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (binding.lottieAnimattionView != null) {
            binding.lottieAnimattionView.cancelAnimation(); // 停止动画
            binding.lottieAnimattionView.clearAnimation(); // 清除动画
            binding.lottieAnimattionView.setImageDrawable(null); // 清除图像
            binding.lottieAnimattionView.destroyDrawingCache(); // 销毁缓存
        }
        if (binding.radarview != null) {
            binding.radarview.setSearching(false);
        }
    }
}