package com.swiftcleaner.chovey.view.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Debug;
import android.view.View;

import com.gyf.immersionbar.ImmersionBar;
import com.swiftcleaner.chovey.databinding.ActivityBatteryInfoBinding;
import com.swiftcleaner.chovey.model.bean.AppInfo;
import com.swiftcleaner.chovey.model.bean.AppInfoBean;
import com.swiftcleaner.chovey.model.tool.AppUtils;
import com.swiftcleaner.chovey.view.adapter.AppInfoAdapter;
import com.swiftcleaner.chovey.view.dialog.CustomScoreDialog;
import com.zxdemo.admob.AdmobHelper;
import com.zxdemo.http.ZxHttp;

import java.util.ArrayList;
import java.util.List;

public class BatteryInfoActivity extends AppCompatActivity {
    ActivityBatteryInfoBinding binding;
    private AppInfoAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBatteryInfoBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        ZxHttp.INSTANCE.getHttpReportInterface("page_BatteryInfoActivity", "", null);
        initBar();
        initShow();
    }

    private void initShow() {

        PackageManager packageManager = getPackageManager();
        List<ApplicationInfo> installedApps = packageManager.getInstalledApplications(0);

        // 过滤掉系统应用
        List<ApplicationInfo> nonSystemApps = new ArrayList<>();
        for (ApplicationInfo appInfo : installedApps) {
            if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                nonSystemApps.add(appInfo);
            }
        }

        // 过滤掉当前应用
        List<ApplicationInfo> nonSystemAppsFiltered = new ArrayList<>();
        String currentPackageName = getPackageName();
        for (ApplicationInfo appInfo : nonSystemApps) {
            if (!currentPackageName.equals(appInfo.packageName)) {
                nonSystemAppsFiltered.add(appInfo);
            }
        }

        // 将ApplicationInfo对象转换为AppInfoBean对象
        List<AppInfoBean> apps = new ArrayList<>();
        for (ApplicationInfo packageInfo : nonSystemAppsFiltered) {
            String appName = packageManager.getApplicationLabel(packageInfo).toString();
            Drawable appIcon = packageInfo.loadIcon(packageManager);
            AppInfoBean appInfoBean = new AppInfoBean(appName, appIcon, packageInfo.packageName);
            apps.add(appInfoBean);
        }

        binding.tvBatteryInfoNum.setText("(" + apps.size() + " in total)");
        binding.batteryRecy.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AppInfoAdapter(apps, this);
        binding.batteryRecy.setAdapter(adapter);
        binding.ivReturn.setOnClickListener(v -> {
            initCustDialog();
        });
    }

    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() {
                AdmobHelper.INSTANCE.showInterstitialAd(BatteryInfoActivity.this, () -> {
                            finish();
                            return null;
                        },
                        () -> {
                            return null;
                        },
                        () -> {
                            finish();
                            return null;
                        });
            }
        });
        customScoreDialog.show();
    }
}