// tech-hud-engine.vala - Core Engine, Themes, Designs & Drawing Routines for Tech HUD Starter
using Gtk;
using Cairo;
using Pango;
using GLib;

public struct ColorTheme {
    public string name;
    public double bg_r1; public double bg_g1; public double bg_b1;
    public double bg_r2; public double bg_g2; public double bg_b2;
    public double panel_r; public double panel_g; public double panel_b;
    public double border_r; public double border_g; public double border_b;
    public double accent_r; public double accent_g; public double accent_b;
    public double text_r; public double text_g; public double text_b;
}

public class ThemeManager {
    private static ColorTheme[] _themes = null;
    public static ColorTheme[] themes {
        get {
            if (_themes == null) {
                _themes = new ColorTheme[20];
                _themes[0] = ColorTheme() { name = _("Cyber Cyan"), bg_r1 = 0.05, bg_g1 = 0.09, bg_b1 = 0.07, bg_r2 = 0.02, bg_g2 = 0.04, bg_b2 = 0.03, panel_r = 0.10, panel_g = 0.18, panel_b = 0.15, border_r = 0.0, border_g = 0.8, border_b = 0.9, accent_r = 0.2, accent_g = 1.0, accent_b = 0.9, text_r = 0.8, text_g = 1.0, text_b = 1.0 };
                _themes[1] = ColorTheme() { name = _("Matrix Emerald"), bg_r1 = 0.04, bg_g1 = 0.08, bg_b1 = 0.05, bg_r2 = 0.01, bg_g2 = 0.03, bg_b2 = 0.02, panel_r = 0.08, panel_g = 0.16, panel_b = 0.10, border_r = 0.1, border_g = 0.9, border_b = 0.3, accent_r = 0.3, accent_g = 1.0, accent_b = 0.4, text_r = 0.8, text_g = 1.0, text_b = 0.7 };
                _themes[2] = ColorTheme() { name = _("Amber Terminal"), bg_r1 = 0.08, bg_g1 = 0.06, bg_b1 = 0.03, bg_r2 = 0.03, bg_g2 = 0.02, bg_b2 = 0.01, panel_r = 0.16, panel_g = 0.12, panel_b = 0.06, border_r = 0.9, border_g = 0.6, border_b = 0.1, accent_r = 1.0, accent_g = 0.8, accent_b = 0.2, text_r = 1.0, text_g = 0.9, text_b = 0.7 };
                _themes[3] = ColorTheme() { name = _("Crimson Alert"), bg_r1 = 0.09, bg_g1 = 0.04, bg_b1 = 0.04, bg_r2 = 0.03, bg_g2 = 0.01, bg_b2 = 0.01, panel_r = 0.18, panel_g = 0.08, panel_b = 0.08, border_r = 0.9, border_g = 0.2, border_b = 0.2, accent_r = 1.0, accent_g = 0.4, accent_b = 0.4, text_r = 1.0, text_g = 0.8, text_b = 0.8 };
                _themes[4] = ColorTheme() { name = _("Deep Space Violet"), bg_r1 = 0.07, bg_g1 = 0.05, bg_b1 = 0.09, bg_r2 = 0.02, bg_g2 = 0.02, bg_b2 = 0.04, panel_r = 0.14, panel_g = 0.10, panel_b = 0.18, border_r = 0.6, border_g = 0.3, border_b = 0.9, accent_r = 0.8, accent_g = 0.5, accent_b = 1.0, text_r = 0.9, text_g = 0.8, text_b = 1.0 };
                _themes[5] = ColorTheme() { name = _("Electric Indigo"), bg_r1 = 0.04, bg_g1 = 0.06, bg_b1 = 0.10, bg_r2 = 0.01, bg_g2 = 0.02, bg_b2 = 0.04, panel_r = 0.08, panel_g = 0.12, panel_b = 0.20, border_r = 0.3, border_g = 0.4, border_b = 1.0, accent_r = 0.5, accent_g = 0.7, accent_b = 1.0, text_r = 0.8, text_g = 0.9, text_b = 1.0 };
                _themes[6] = ColorTheme() { name = _("Solar Gold"), bg_r1 = 0.09, bg_g1 = 0.08, bg_b1 = 0.04, bg_r2 = 0.03, bg_g2 = 0.03, bg_b2 = 0.01, panel_r = 0.18, panel_g = 0.16, panel_b = 0.08, border_r = 0.9, border_g = 0.8, border_b = 0.2, accent_r = 1.0, accent_g = 0.9, accent_b = 0.4, text_r = 1.0, text_g = 1.0, text_b = 0.8 };
                _themes[7] = ColorTheme() { name = _("Toxic Lime"), bg_r1 = 0.06, bg_g1 = 0.09, bg_b1 = 0.03, bg_r2 = 0.02, bg_g2 = 0.03, bg_b2 = 0.01, panel_r = 0.12, panel_g = 0.18, panel_b = 0.06, border_r = 0.6, border_g = 0.9, border_b = 0.2, accent_r = 0.8, accent_g = 1.0, accent_b = 0.3, text_r = 0.9, text_g = 1.0, text_b = 0.7 };
                _themes[8] = ColorTheme() { name = _("Ice Frost"), bg_r1 = 0.06, bg_g1 = 0.08, bg_b1 = 0.10, bg_r2 = 0.02, bg_g2 = 0.03, bg_b2 = 0.04, panel_r = 0.12, panel_g = 0.16, panel_b = 0.20, border_r = 0.4, border_g = 0.8, border_b = 1.0, accent_r = 0.6, accent_g = 0.9, accent_b = 1.0, text_r = 0.9, text_g = 1.0, text_b = 1.0 };
                _themes[9] = ColorTheme() { name = _("Monochrome Steel"), bg_r1 = 0.06, bg_g1 = 0.06, bg_b1 = 0.06, bg_r2 = 0.02, bg_g2 = 0.02, bg_b2 = 0.02, panel_r = 0.12, panel_g = 0.12, panel_b = 0.12, border_r = 0.7, border_g = 0.7, border_b = 0.7, accent_r = 0.9, accent_g = 0.9, accent_b = 0.9, text_r = 1.0, text_g = 1.0, text_b = 1.0 };
                _themes[10] = ColorTheme() { name = _("Neon Sunset"), bg_r1 = 0.09, bg_g1 = 0.03, bg_b1 = 0.07, bg_r2 = 0.03, bg_g2 = 0.01, bg_b2 = 0.02, panel_r = 0.18, panel_g = 0.06, panel_b = 0.14, border_r = 1.0, border_g = 0.3, border_b = 0.7, accent_r = 1.0, accent_g = 0.5, accent_b = 0.8, text_r = 1.0, text_g = 0.8, text_b = 0.9 };
                _themes[11] = ColorTheme() { name = _("Radioactive Orange"), bg_r1 = 0.10, bg_g1 = 0.05, bg_b1 = 0.02, bg_r2 = 0.03, bg_g2 = 0.02, bg_b2 = 0.00, panel_r = 0.20, panel_g = 0.10, panel_b = 0.04, border_r = 1.0, border_g = 0.5, border_b = 0.0, accent_r = 1.0, accent_g = 0.6, accent_b = 0.1, text_r = 1.0, text_g = 0.9, text_b = 0.7 };
                _themes[12] = ColorTheme() { name = _("Plasma Pink"), bg_r1 = 0.10, bg_g1 = 0.02, bg_b1 = 0.08, bg_r2 = 0.03, bg_g2 = 0.00, bg_b2 = 0.03, panel_r = 0.20, panel_g = 0.04, panel_b = 0.16, border_r = 1.0, border_g = 0.0, border_b = 0.6, accent_r = 1.0, accent_g = 0.2, accent_b = 0.8, text_r = 1.0, text_g = 0.8, text_b = 1.0 };
                _themes[13] = ColorTheme() { name = _("Quantum Teal"), bg_r1 = 0.02, bg_g1 = 0.08, bg_b1 = 0.08, bg_r2 = 0.01, bg_g2 = 0.03, bg_b2 = 0.03, panel_r = 0.04, panel_g = 0.16, panel_b = 0.16, border_r = 0.0, border_g = 0.9, border_b = 0.8, accent_r = 0.2, accent_g = 1.0, accent_b = 0.9, text_r = 0.8, text_g = 1.0, text_b = 1.0 };
                _themes[14] = ColorTheme() { name = _("Copper Oxide"), bg_r1 = 0.07, bg_g1 = 0.06, bg_b1 = 0.05, bg_r2 = 0.02, bg_g2 = 0.02, bg_b2 = 0.01, panel_r = 0.12, panel_g = 0.27, panel_b = 0.25, border_r = 0.8, border_g = 0.5, border_b = 0.3, accent_r = 0.9, accent_g = 0.6, accent_b = 0.4, text_r = 1.0, text_g = 0.9, text_b = 0.8 };
                _themes[15] = ColorTheme() { name = _("Hacker Green"), bg_r1 = 0.01, bg_g1 = 0.05, bg_b1 = 0.01, bg_r2 = 0.00, bg_g2 = 0.02, bg_b2 = 0.00, panel_r = 0.02, panel_g = 0.10, panel_b = 0.02, border_r = 0.0, border_g = 1.0, border_b = 0.0, accent_r = 0.2, accent_g = 1.0, accent_b = 0.2, text_r = 0.6, text_g = 1.0, text_b = 0.6 };
                _themes[16] = ColorTheme() { name = _("Supernova Red"), bg_r1 = 0.10, bg_g1 = 0.02, bg_b1 = 0.02, bg_r2 = 0.03, bg_g2 = 0.00, bg_b2 = 0.00, panel_r = 0.20, panel_g = 0.04, panel_b = 0.04, border_r = 1.0, border_g = 0.1, border_b = 0.1, accent_r = 1.0, accent_g = 0.3, accent_b = 0.3, text_r = 1.0, text_g = 0.8, text_b = 0.8 };
                _themes[17] = ColorTheme() { name = _("Arctic Blue"), bg_r1 = 0.03, bg_g1 = 0.06, bg_b1 = 0.09, bg_r2 = 0.01, bg_g2 = 0.02, bg_b2 = 0.03, panel_r = 0.06, panel_g = 0.12, panel_b = 0.18, border_r = 0.3, border_g = 0.7, border_b = 1.0, accent_r = 0.5, accent_g = 0.8, accent_b = 1.0, text_r = 0.8, text_g = 0.9, text_b = 1.0 };
                _themes[18] = ColorTheme() { name = _("Amethyst Twilight"), bg_r1 = 0.06, bg_g1 = 0.03, bg_b1 = 0.08, bg_r2 = 0.02, bg_g2 = 0.01, bg_b2 = 0.03, panel_r = 0.12, panel_g = 0.06, panel_b = 0.16, border_r = 0.8, border_g = 0.3, border_b = 1.0, accent_r = 0.8, accent_g = 0.5, accent_b = 1.0, text_r = 0.9, text_g = 0.8, text_b = 1.0 };
                _themes[19] = ColorTheme() { name = _("Bioluminescent"), bg_r1 = 0.02, bg_g1 = 0.09, bg_b1 = 0.06, bg_r2 = 0.01, bg_g2 = 0.03, bg_b2 = 0.02, panel_r = 0.04, panel_g = 0.18, panel_b = 0.12, border_r = 0.2, border_g = 1.0, border_b = 0.6, accent_r = 0.4, accent_g = 1.0, accent_b = 0.8, text_r = 0.8, text_g = 1.0, text_b = 0.9 };
            }
            return _themes;
        }
    }
    public static int get_theme_count() { return themes.length; }
    public static ColorTheme get_theme(int index) {
        var t = themes;
        if (index < 0 || index >= t.length) return t[2];
        return t[index];
    }
}

public enum DesignCategory { STARSHIP_NEON = 0, GLASS = 1, MARBLE = 2 }
public enum GlassVariant { CLEAR = 0, FROSTED = 1, BLURRY = 2, ULTRA_CLEAR = 3, DIAMOND = 4 }

public class HudDesignSystem {
    private static Gdk.Pixbuf? marble_pixbuf = null;
    private static Cairo.Pattern? marble_pattern = null;
    private static bool marble_search_done = false;

    public static Gdk.Pixbuf? get_marble_pixbuf() {
        if (!marble_search_done) {
            marble_search_done = true;
            string exe_dir = Environment.get_current_dir();
            string[] search_paths = {
                "./marble-tile.jpg", "marble-tile.jpg",
                "../marble-tile.jpg", "../../marble-tile.jpg",
                GLib.Path.build_filename(exe_dir, "marble-tile.jpg"),
                GLib.Path.build_filename(GLib.Path.get_dirname(exe_dir), "marble-tile.jpg"),
                "/usr/local/share/tech-hud-starter/marble-tile.jpg",
                "/usr/share/tech-hud-starter/marble-tile.jpg",
                GLib.Path.build_filename(Environment.get_user_data_dir(), "tech-hud-starter", "marble-tile.jpg")
            };
            foreach (var path in search_paths) {
                if (FileUtils.test(path, FileTest.EXISTS)) {
                    try { marble_pixbuf = new Gdk.Pixbuf.from_file(path); break; }
                    catch (Error e) { stderr.printf("Failed marble %s: %s\n", path, e.message); }
                }
            }
        }
        return marble_pixbuf;
    }

    public static Cairo.Pattern? get_marble_pattern() {
        if (marble_pattern == null) {
            var pixbuf = get_marble_pixbuf();
            if (pixbuf != null) {
                var surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, 0, null);
                marble_pattern = new Cairo.Pattern.for_surface(surface);
                marble_pattern.set_extend(Cairo.Extend.REPEAT);
                marble_pattern.set_filter(Cairo.Filter.BEST);
            }
        }
        return marble_pattern;
    }

    public static void apply_panel_fill(Cairo.Context cr, double w, double h, ColorTheme t, int design_category, int glass_variant, double bg_opacity) {
        if (design_category == (int)DesignCategory.MARBLE) {
            cr.save(); cr.clip_preserve();
            var pattern = get_marble_pattern();
            if (pattern != null) { cr.set_source(pattern); cr.paint_with_alpha(bg_opacity); }
            else {
                var fallback = new Cairo.Pattern.linear(0,0,0,h);
                fallback.add_color_stop_rgba(0, 0.95, 0.95, 0.95, bg_opacity);
                fallback.add_color_stop_rgba(1, 0.80, 0.80, 0.80, bg_opacity);
                cr.set_source(fallback); cr.paint_with_alpha(bg_opacity);
            }
            cr.restore();
        } else if (design_category == (int)DesignCategory.GLASS) {
            double base_alpha = bg_opacity * 0.65;
            switch (glass_variant) {
                case (int)GlassVariant.FROSTED: cr.set_source_rgba(t.bg_r1, t.bg_g1, t.bg_b1, base_alpha*0.85); cr.fill_preserve(); cr.set_source_rgba(1.0,1.0,1.0, bg_opacity*0.12); cr.fill_preserve(); break;
                case (int)GlassVariant.BLURRY: cr.set_source_rgba(t.bg_r1, t.bg_g1, t.bg_b1, base_alpha*0.55); cr.fill_preserve(); cr.set_source_rgba(1.0,1.0,1.0, bg_opacity*0.06); cr.fill_preserve(); break;
                case (int)GlassVariant.ULTRA_CLEAR: cr.set_source_rgba(t.bg_r1, t.bg_g1, t.bg_b1, base_alpha*0.35); cr.fill_preserve(); cr.set_source_rgba(1.0,1.0,1.0, bg_opacity*0.18); cr.fill_preserve(); break;
                case (int)GlassVariant.DIAMOND:
                    cr.set_source_rgba(t.bg_r1, t.bg_g1, t.bg_b1, base_alpha*0.75); cr.fill_preserve();
                    var shimmer = new Cairo.Pattern.linear(0,0,w,h);
                    shimmer.add_color_stop_rgba(0, t.bg_r1, t.bg_g1, t.bg_b1, bg_opacity*0.15);
                    shimmer.add_color_stop_rgba(0.5, 1.0,1.0,1.0, bg_opacity*0.28);
                    shimmer.add_color_stop_rgba(1, t.bg_r1, t.bg_g1, t.bg_b1, bg_opacity*0.08);
                    cr.set_source(shimmer); cr.fill_preserve(); break;
                default: cr.set_source_rgba(t.bg_r1, t.bg_g1, t.bg_b1, base_alpha); cr.fill_preserve(); cr.set_source_rgba(1.0,1.0,1.0, bg_opacity*0.10); cr.fill_preserve(); break;
            }
        } else {
            var bg_pat = new Cairo.Pattern.linear(0,0,0,h);
            bg_pat.add_color_stop_rgba(0.0, t.bg_r1, t.bg_g1, t.bg_b1, 0.95*bg_opacity);
            bg_pat.add_color_stop_rgba(1.0, t.bg_r2, t.bg_g2, t.bg_b2, 0.95*bg_opacity);
            cr.set_source(bg_pat); cr.fill_preserve();
        }
    }

    public static void build_path_for_style(Cairo.Context cr, double x, double y, double w, double h, int style, double ui_scale) {
        switch (style) {
            case 1:
                double c = 12.0 * ui_scale;
                cr.move_to (x, y);
                cr.line_to (x + w - c, y);
                cr.line_to (x + w, y + c);
                cr.line_to (x + w, y + h);
                cr.line_to (x + c, y + h);
                cr.line_to (x, y + h - c);
                cr.close_path();
                break;
            case 0:
                double rc = 8.0 * ui_scale;
                cr.move_to (x + rc, y);
                cr.line_to (x + w - rc, y);
                cr.line_to (x + w, y + rc);
                cr.line_to (x + w, y + h - rc);
                cr.line_to (x + w - rc, y + h);
                cr.line_to (x + rc, y + h);
                cr.line_to (x, y + h - rc);
                cr.line_to (x, y + rc);
                cr.close_path();
                break;
            case 2:
                double sc = 10.0 * ui_scale;
                cr.move_to (x, y);
                cr.line_to (x + w - sc, y);
                cr.line_to (x + w, y + sc);
                cr.line_to (x + w, y + h);
                cr.line_to (x + sc, y + h);
                cr.line_to (x, y + h - sc);
                cr.close_path();
                break;
            case 3:
                cr.rectangle (x + (6 * ui_scale), y, w - (12 * ui_scale), h);
                break;
            case 4:
                double nc = 8.0 * ui_scale;
                cr.move_to (x + nc, y);
                cr.line_to (x + w - nc, y);
                cr.line_to (x + w, y + nc);
                cr.line_to (x + w, y + h - nc);
                cr.line_to (x + w - nc, y + h);
                cr.line_to (x + nc, y + h);
                cr.line_to (x, y + h - nc);
                cr.line_to (x, y + nc);
                cr.close_path();
                break;
            case 5:
                double hc = 12.0 * ui_scale;
                cr.move_to (x + hc, y);
                cr.line_to (x + w - hc, y);
                cr.line_to (x + w, y + h / 2.0);
                cr.line_to (x + w - hc, y + h);
                cr.line_to (x + hc, y + h);
                cr.line_to (x, y + h / 2.0);
                cr.close_path();
                break;
            default:
                cr.rectangle (x, y, w, h);
                break;
        }
    }

    public static void draw_text_with_effects(Cairo.Context cr, string text, double x, double y, double font_size, double r, double g, double b, double alpha, int style_3d, bool use_text_shadow, int shadow_style) {
        cr.save();
        cr.select_font_face("Sans", Cairo.FontSlant.NORMAL, Cairo.FontWeight.BOLD);
        cr.set_font_size(font_size);

        if (use_text_shadow) {
            switch (shadow_style) {
                case 1: // Hard Shadow
                    cr.set_source_rgba(0.0, 0.0, 0.0, alpha * 0.7);
                    cr.move_to(x + 2.0, y + 2.0);
                    cr.show_text(text);
                    break;
                case 2: // Glow + Shadow
                    cr.set_source_rgba(r, g, b, alpha * 0.35);
                    cr.move_to(x - 1.5, y); cr.show_text(text);
                    cr.move_to(x + 1.5, y); cr.show_text(text);
                    cr.move_to(x, y - 1.5); cr.show_text(text);
                    cr.move_to(x, y + 1.5); cr.show_text(text);
                    cr.set_source_rgba(0.0, 0.0, 0.0, alpha * 0.6);
                    cr.move_to(x + 2.0, y + 2.0); cr.show_text(text);
                    break;
                default: // Soft Double Shadow
                    cr.set_source_rgba(0.0, 0.0, 0.0, alpha * 0.4);
                    cr.move_to(x + 1.5, y + 1.5); cr.show_text(text);
                    cr.set_source_rgba(0.0, 0.0, 0.0, alpha * 0.25);
                    cr.move_to(x + 3.0, y + 3.0); cr.show_text(text);
                    break;
            }
        }

        if (style_3d == 1) { // Debossed / Carved
            cr.set_source_rgba(0.0, 0.0, 0.0, alpha * 0.7);
            cr.move_to(x - 1.0, y - 1.0); cr.show_text(text);
            cr.set_source_rgba(1.0, 1.0, 1.0, alpha * 0.3);
            cr.move_to(x + 1.0, y + 1.0); cr.show_text(text);
        } else if (style_3d == 2) { // Embossed / Raised
            cr.set_source_rgba(1.0, 1.0, 1.0, alpha * 0.45);
            cr.move_to(x - 1.0, y - 1.0); cr.show_text(text);
            cr.set_source_rgba(0.0, 0.0, 0.0, alpha * 0.65);
            cr.move_to(x + 1.0, y + 1.0); cr.show_text(text);
        }

        cr.set_source_rgba(r, g, b, alpha);
        cr.move_to(x, y);
        cr.show_text(text);
        cr.restore();
    }
}
