package com.example.launcher;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    // ADD YOUR HARDCODED PACKAGE NAMES HERE
    private final List<String> WHITELIST = Arrays.asList(
            "com.android.settings",
            "com.example.scoutingapp"
    );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        loadApps();
    }

    private void loadApps() {
        PackageManager pm = getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        // 1. Get all apps
        List<ResolveInfo> availableActivities = pm.queryIntentActivities(intent, 0);

        // 2. Sort them alphabetically
        Collections.sort(availableActivities, (a, b) ->
                a.loadLabel(pm).toString().compareToIgnoreCase(b.loadLabel(pm).toString()));

        GridLayout layout = findViewById(R.id.container);
        layout.removeAllViews();

        // 3. Set standard icon size (DP to Pixels)
        int iconSize = (int) (52 * getResources().getDisplayMetrics().density);

        for (ResolveInfo ri : availableActivities) {
            String packageName = ri.activityInfo.packageName;

            // 4. CHECK THE WHITELIST
            if (WHITELIST.contains(packageName)) {
                String appLabel = ri.loadLabel(pm).toString();
                Drawable appIcon = ri.loadIcon(pm);

                // Create Item Container
                LinearLayout appItem = new LinearLayout(this);
                appItem.setOrientation(LinearLayout.VERTICAL);
                appItem.setGravity(Gravity.CENTER);





                // Grid Layout Params for perfect columns
                GridLayout.LayoutParams gridParams = new GridLayout.LayoutParams(
                        GridLayout.spec(GridLayout.UNDEFINED, 1f),
                        GridLayout.spec(GridLayout.UNDEFINED, 1f)
                );
                gridParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                gridParams.setMargins(0, 30, 0, 30);
                appItem.setLayoutParams(gridParams);

                // Create the Icon
                ImageView imageView = new ImageView(this);
                LinearLayout.LayoutParams iconParams = new LinearLayout.LayoutParams(iconSize, iconSize);
                imageView.setLayoutParams(iconParams);
                imageView.setImageDrawable(appIcon);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

                // Create the Label
                TextView textView = new TextView(this);
                textView.setText(appLabel);
                textView.setGravity(Gravity.CENTER);
                textView.setTextColor(Color.WHITE);
                textView.setTextSize(11);
                textView.setMaxLines(1);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                textView.setShadowLayer(8, 0, 0, Color.BLACK); // For wallpaper readability
                textView.setPadding(0, 10, 0, 0);

                // Assemble
                appItem.addView(imageView);
                appItem.addView(textView);

                // Launch Intent
                appItem.setOnClickListener(v -> {
                    Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
                    if (launchIntent != null) startActivity(launchIntent);
                });

                layout.addView(appItem);
            }
        }
    }
}