package com.lemenzo.multisms;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

final class Ui {
    private Ui() {}

    static int dp(Activity a, int v) {
        return Math.round(v * a.getResources().getDisplayMetrics().density);
    }

    static TextView text(Activity a, String s, int sp, boolean bold) {
        TextView v = new TextView(a);
        v.setText(s);
        v.setTextSize(sp);
        v.setTextColor(a.getColor(R.color.lemenzo_text_primary));
        v.setGravity(Gravity.CENTER_VERTICAL);
        v.setIncludeFontPadding(false);
        if (bold) v.setTypeface(Typeface.create("sans", Typeface.BOLD));
        else v.setTypeface(Typeface.create("sans", Typeface.NORMAL));
        return v;
    }

    static LinearLayout brandHeader(Activity a) {
        LinearLayout row = new LinearLayout(a);
        row.setOrientation(LinearLayout.HORIZONTAL);
        row.setGravity(Gravity.CENTER_VERTICAL);
        row.setPadding(0, dp(a, 2), 0, dp(a, 4));

        ImageView logo = new ImageView(a);
        logo.setImageResource(R.drawable.ic_launcher);
        logo.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        LinearLayout.LayoutParams logoLp =
                new LinearLayout.LayoutParams(dp(a, 38), dp(a, 38));
        row.addView(logo, logoLp);

        TextView title = text(a, a.getString(R.string.app_name), 22, true);
        LinearLayout.LayoutParams titleLp =
                new LinearLayout.LayoutParams(0, dp(a, 42), 1f);
        titleLp.setMargins(dp(a, 10), 0, 0, 0);
        row.addView(title, titleLp);
        return row;
    }

    static LinearLayout softCard(Activity a) {
        LinearLayout l = new LinearLayout(a);
        l.setOrientation(LinearLayout.VERTICAL);
        l.setBackgroundResource(R.drawable.card_soft);
        l.setPadding(dp(a, 16), dp(a, 15), dp(a, 16), dp(a, 15));
        return l;
    }

    static LinearLayout root(Activity a) {
        LinearLayout l = new LinearLayout(a);
        l.setOrientation(LinearLayout.VERTICAL);
        l.setPadding(dp(a, 20), dp(a, 18), dp(a, 20), dp(a, 18));
        l.setBackgroundColor(a.getColor(R.color.lemenzo_background));
        applySafeInsets(l);
        return l;
    }

    static Button button(Activity a, String s) {
        Button b = new Button(a);
        b.setText(s);
        b.setTextColor(Color.WHITE);
        b.setTextSize(15);
        b.setAllCaps(false);
        b.setTypeface(Typeface.create("sans", Typeface.BOLD));
        b.setBackgroundResource(R.drawable.button);
        b.setMinHeight(dp(a, 52));
        return b;
    }

    static Button secondaryButton(Activity a, String s) {
        Button b = new Button(a);
        b.setText(s);
        b.setTextColor(a.getColor(R.color.lemenzo_text_secondary));
        b.setTextSize(15);
        b.setAllCaps(false);
        b.setTypeface(Typeface.create("sans", Typeface.BOLD));
        b.setBackgroundResource(R.drawable.button_secondary);
        b.setMinHeight(dp(a, 50));
        return b;
    }

    static Button dangerButton(Activity a, String s) {
        Button b = new Button(a);
        b.setText(s);
        b.setTextColor(Color.WHITE);
        b.setTextSize(15);
        b.setAllCaps(false);
        b.setTypeface(Typeface.create("sans", Typeface.BOLD));
        b.setBackgroundResource(R.drawable.button_danger);
        b.setMinHeight(dp(a, 50));
        return b;
    }

    static EditText input(Activity a, String hint) {
        EditText e = new EditText(a);
        e.setHint(hint);
        e.setHintTextColor(a.getColor(R.color.lemenzo_text_muted));
        e.setTextColor(a.getColor(R.color.lemenzo_text_primary));
        e.setTextSize(16);
        e.setBackgroundResource(R.drawable.input);
        e.setSingleLine(true);
        return e;
    }

    static ImageButton iconButton(Activity a, int drawable, String description) {
        ImageButton b = new ImageButton(a);
        b.setImageResource(drawable);
        b.setBackgroundColor(Color.TRANSPARENT);
        b.setColorFilter(a.getColor(R.color.lemenzo_text_primary));
        b.setPadding(dp(a, 12), dp(a, 12), dp(a, 12), dp(a, 12));
        b.setContentDescription(description);
        return b;
    }

    static void gap(Activity a, LinearLayout l, int h) {
        Space s = new Space(a);
        l.addView(s, new LinearLayout.LayoutParams(1, dp(a, h)));
    }

    static LinearLayout card(Activity a) {
        LinearLayout l = new LinearLayout(a);
        l.setOrientation(LinearLayout.VERTICAL);
        l.setBackgroundResource(R.drawable.card);
        l.setPadding(dp(a, 16), dp(a, 14), dp(a, 16), dp(a, 14));
        return l;
    }

    static void applySafeInsets(View root) {
        final int l0 = root.getPaddingLeft();
        final int t0 = root.getPaddingTop();
        final int r0 = root.getPaddingRight();
        final int b0 = root.getPaddingBottom();
        root.setOnApplyWindowInsetsListener((v, insets) -> {
            android.graphics.Insets top = insets.getInsets(
                    WindowInsets.Type.statusBars() | WindowInsets.Type.displayCutout());
            android.graphics.Insets nav = insets.getInsets(WindowInsets.Type.navigationBars());
            v.setPadding(l0 + top.left, t0 + top.top, r0 + top.right, b0 + nav.bottom);
            return insets;
        });
        root.requestApplyInsets();
    }

    static final class DialogParts {
        final Activity activity;
        final Dialog dialog;
        final Button negative;
        final Button positive;

        DialogParts(Activity a, Dialog d, Button n, Button p) {
            activity = a;
            dialog = d;
            negative = n;
            positive = p;
        }
    }

    static DialogParts showDialog(
            Activity a,
            String title,
            String message,
            View customContent,
            String negativeText,
            String positiveText,
            boolean danger) {

        Dialog d = new Dialog(a);
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);

        LinearLayout shell = new LinearLayout(a);
        shell.setOrientation(LinearLayout.VERTICAL);
        shell.setPadding(dp(a, 20), dp(a, 20), dp(a, 20), dp(a, 18));
        shell.setBackgroundResource(R.drawable.dialog_background);

        TextView t = text(a, title, 20, true);
        shell.addView(t, new LinearLayout.LayoutParams(-1, -2));

        if (message != null && !message.isEmpty()) {
            gap(a, shell, 8);
            TextView m = text(a, message, 14, false);
            m.setTextColor(a.getColor(R.color.lemenzo_text_secondary));
            m.setLineSpacing(0f, 1.08f);
            shell.addView(m, new LinearLayout.LayoutParams(-1, -2));
        }

        if (customContent != null) {
            gap(a, shell, 16);
            shell.addView(customContent, new LinearLayout.LayoutParams(-1, -2));
        }

        gap(a, shell, 20);

        LinearLayout actions = new LinearLayout(a);
        actions.setOrientation(LinearLayout.HORIZONTAL);
        actions.setGravity(Gravity.END);

        Button negative = secondaryButton(a, negativeText);
        Button positive = danger ? dangerButton(a, positiveText) : button(a, positiveText);

        LinearLayout.LayoutParams half = new LinearLayout.LayoutParams(0, dp(a, 50), 1f);
        actions.addView(negative, half);

        Space between = new Space(a);
        actions.addView(between, new LinearLayout.LayoutParams(dp(a, 10), 1));

        actions.addView(positive, new LinearLayout.LayoutParams(0, dp(a, 50), 1f));
        shell.addView(actions, new LinearLayout.LayoutParams(-1, -2));

        d.setContentView(shell);
        d.setCanceledOnTouchOutside(true);

        Window w = d.getWindow();
        if (w != null) {
            w.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        d.show();

        w = d.getWindow();
        if (w != null) {
            int screen = a.getResources().getDisplayMetrics().widthPixels;
            w.setLayout(Math.min(screen - dp(a, 32), dp(a, 460)), -2);
            w.setDimAmount(0.72f);
            w.addFlags(android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }

        negative.setOnClickListener(v -> d.dismiss());
        return new DialogParts(a, d, negative, positive);
    }
}
