package eu.konggdev.strikemaps.ui.factory;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.ui.element.item.PreviewItem;
import org.maplibre.geojson.Feature;

import java.util.List;
import java.util.function.Consumer;

//FIXME: Move Item functions into specific classes for specific types - e.g. StyleItem
public final class AlertDialogFactory {
    public static AlertDialog pointSelector(AppController app, List<Feature> features, Consumer<Feature> callback) {
        LinearLayout layout = new LinearLayout(app.getActivity());
        layout.setOrientation(LinearLayout.VERTICAL);

        ScrollView scrollView = new ScrollView(app.getActivity());
        scrollView.addView(layout);

        AlertDialog dialog = new AlertDialog.Builder(app.getActivity())
                .setView(scrollView)
                .create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));

        for (Feature feature : features) {
            View itemView = PreviewItem.fromFeature(feature).makeView(app.getUi(), v -> {
                dialog.dismiss();
                new android.os.Handler(android.os.Looper.getMainLooper())
                        .post(() -> callback.accept(feature));
            });
            layout.addView(itemView);
        }

        return dialog;
    }

    public static AlertDialog searchSettings(AppController app) {
        return new AlertDialog.Builder(app.getActivity())
                .setTitle("Configure Search")
                .setPositiveButton("OK", null)
                .create();
    }

    public static AlertDialog restartDialog(AppController app) {
        return new AlertDialog.Builder(app.getActivity())
                .setTitle("Restart required")
                .setMessage("Restart the app to apply changes.")
                .setCancelable(false)
                .setNegativeButton("Cancel", (d, w) -> {
                    Toast.makeText(app.getActivity(),
                            "Changes will be applied on next restart",
                            Toast.LENGTH_SHORT).show();
                    d.dismiss();
                })
                .setPositiveButton("Restart", (d, w) -> {
                    Intent i = app.getActivity().getPackageManager()
                            .getLaunchIntentForPackage(app.getActivity().getPackageName());
                    if (i != null) {
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                        app.getActivity().startActivity(i);
                    }
                    Runtime.getRuntime().exit(0);
                })
                .create();
    }
}
