<?php

namespace auctionforge\inc\controllers;

class StyleCssOutput
{
    public function init()
    {
        add_action('wp_head', [$this, 'output_css'], 999);
    }

    public function output_css()
    {
        $s = StyleSettings::getOption();
        $d = StyleSettings::getDefaults();

        $hasCustom = false;
        foreach ($s as $key => $val) {
            if (isset($d[$key]) && $val !== $d[$key]) {
                $hasCustom = true;
                break;
            }
        }
        if (!$hasCustom) return;

        $css = $this->buildCss($s, $d);
        if (empty(trim($css))) return;

        echo "\n<style id=\"bidspirit-custom-styles\">\n";
        echo $css;
        echo "</style>\n";
    }

    private function changed($s, $d, $key)
    {
        return isset($s[$key]) && isset($d[$key]) && $s[$key] !== $d[$key];
    }

    private function v($s, $key)
    {
        return isset($s[$key]) ? $s[$key] : '';
    }

    private function shadow($s, $presetKey, $colorKey)
    {
        $preset = isset($s[$presetKey]) ? $s[$presetKey] : 'medium';
        $color  = isset($s[$colorKey]) ? $s[$colorKey] : 'rgba(0,0,0,0.16)';
        return StyleSettings::buildShadowValue($preset, $color);
    }

    private function buildCss($s, $d)
    {
        $css = '';

        // === 1. GLOBAL / BASE ===
        if ($this->changed($s, $d, 'global_primary_font')) {
            $css .= ".bp-container, .bp-container-fluid { font-family: {$this->v($s,'global_primary_font')} !important; }\n";
        }
        if ($this->changed($s, $d, 'global_secondary_font')) {
            $css .= ".bp-countdown, .bp-single-lot .bp-single-lot__head, .bp-lots .bp-lot.bp-lot_auction .bp-lot__title .bp-title, .bp-lots .bp-lot.bp-lot_countdown .bp-lot__title .bp-title { font-family: {$this->v($s,'global_secondary_font')} !important; }\n";
        }
        if ($this->changed($s, $d, 'global_accent_color')) {
            $c = $this->v($s, 'global_accent_color');
            $css .= ".bp-lots .bp-lot .bp-lot__title .bp-title { color: {$c} !important; }\n";
            $css .= ".bp-single-lot .bp-single-lot__head { color: {$c} !important; }\n";
            $css .= ".bp-single-lot .bp-control__notice .bp-bid { color: {$c} !important; }\n";
            $css .= ".user_nav .menu a:hover span { color: {$c} !important; }\n";
        }
        if ($this->changed($s, $d, 'global_body_text_color')) {
            $c = $this->v($s, 'global_body_text_color');
            $css .= ".bp-container, .bp-container-fluid, .bp-single-lot .bp-single-lot__body .bp-body__main .bp-body__main_content { color: {$c} !important; }\n";
        }
        if ($this->changed($s, $d, 'global_muted_text_color')) {
            $c = $this->v($s, 'global_muted_text_color');
            $css .= ".bp-lots .bp-lot .bp-lot__text, .bp-lots .bp-lot .bp-lot__title, .bp-lots .bp-lot .bp-lot__title p, .bp-lots .bp-lot .bp-lot__title a { color: {$c} !important; }\n";
        }
        if ($this->changed($s, $d, 'global_loader_color')) {
            $c = $this->v($s, 'global_loader_color');
            $css .= ":root { --main-bg-color: {$c} !important; }\n";
        }

        // === 2. BUTTONS - PRIMARY ===
        $bp = false;
        $bpCss = ".bp-btn.bp-btn-default {\n";
        if ($this->changed($s, $d, 'btn_primary_bg'))       { $bpCss .= "  background-color: {$this->v($s,'btn_primary_bg')} !important;\n"; $bp = true; }
        if ($this->changed($s, $d, 'btn_primary_text'))     { $bpCss .= "  color: {$this->v($s,'btn_primary_text')} !important;\n"; $bp = true; }
        if ($this->changed($s, $d, 'btn_primary_border'))   { $bpCss .= "  border-color: {$this->v($s,'btn_primary_border')} !important;\n"; $bp = true; }
        if ($this->changed($s, $d, 'btn_primary_font_size')){ $bpCss .= "  font-size: {$this->v($s,'btn_primary_font_size')}px !important;\n"; $bp = true; }
        if ($this->changed($s, $d, 'btn_primary_text_transform')){ $bpCss .= "  text-transform: {$this->v($s,'btn_primary_text_transform')} !important;\n"; $bp = true; }
        $bpCss .= "}\n";
        if ($bp) $css .= $bpCss;

        $bph = false;
        $bphCss = ".bp-btn.bp-btn-default:hover {\n";
        if ($this->changed($s, $d, 'btn_primary_hover_bg'))    { $bphCss .= "  background-color: {$this->v($s,'btn_primary_hover_bg')} !important;\n"; $bph = true; }
        if ($this->changed($s, $d, 'btn_primary_hover_text'))  { $bphCss .= "  color: {$this->v($s,'btn_primary_hover_text')} !important;\n"; $bph = true; }
        if ($this->changed($s, $d, 'btn_primary_hover_border')){ $bphCss .= "  border-color: {$this->v($s,'btn_primary_hover_border')} !important;\n"; $bph = true; }
        $bphCss .= "}\n";
        if ($bph) $css .= $bphCss;

        // === 3. BUTTONS - OUTLINE ===
        $bo = false;
        $boCss = ".bp-btn.bp-btn-outline-default {\n";
        if ($this->changed($s, $d, 'btn_outline_bg'))     { $boCss .= "  background-color: {$this->v($s,'btn_outline_bg')} !important;\n"; $bo = true; }
        if ($this->changed($s, $d, 'btn_outline_text'))   { $boCss .= "  color: {$this->v($s,'btn_outline_text')} !important;\n"; $bo = true; }
        if ($this->changed($s, $d, 'btn_outline_border')) { $boCss .= "  border-color: {$this->v($s,'btn_outline_border')} !important;\n"; $bo = true; }
        $boCss .= "}\n";
        if ($bo) $css .= $boCss;

        $boh = false;
        $bohCss = ".bp-btn.bp-btn-outline-default:hover {\n";
        if ($this->changed($s, $d, 'btn_outline_hover_bg'))   { $bohCss .= "  background-color: {$this->v($s,'btn_outline_hover_bg')} !important;\n"; $boh = true; }
        if ($this->changed($s, $d, 'btn_outline_hover_text')) { $bohCss .= "  color: {$this->v($s,'btn_outline_hover_text')} !important;\n"; $boh = true; }
        $bohCss .= "}\n";
        if ($boh) $css .= $bohCss;

        // === 4. BUTTONS - LOGIN & REGISTER ===
        $bl = false;
        $blCss = ".bp-btn.bp-btn-login {\n";
        if ($this->changed($s, $d, 'btn_login_bg'))   { $blCss .= "  background: {$this->v($s,'btn_login_bg')} !important; border-color: {$this->v($s,'btn_login_bg')} !important;\n"; $bl = true; }
        if ($this->changed($s, $d, 'btn_login_text')) { $blCss .= "  color: {$this->v($s,'btn_login_text')} !important;\n"; $bl = true; }
        $blCss .= "}\n";
        if ($bl) $css .= $blCss;

        if ($this->changed($s, $d, 'btn_login_hover_bg')) {
            $css .= ".bp-btn.bp-btn-login:hover { background: {$this->v($s,'btn_login_hover_bg')} !important; }\n";
        }

        $br = false;
        $brCss = ".bp-btn.bp-btn-register {\n";
        if ($this->changed($s, $d, 'btn_register_bg'))   { $brCss .= "  background-color: {$this->v($s,'btn_register_bg')} !important; border-color: {$this->v($s,'btn_register_bg')} !important;\n"; $br = true; }
        if ($this->changed($s, $d, 'btn_register_text')) { $brCss .= "  color: {$this->v($s,'btn_register_text')} !important;\n"; $br = true; }
        $brCss .= "}\n";
        if ($br) $css .= $brCss;

        if ($this->changed($s, $d, 'btn_register_hover_bg')) {
            $css .= ".bp-btn.bp-btn-register:hover { background-color: {$this->v($s,'btn_register_hover_bg')} !important; }\n";
        }

        // === 5. BUTTONS - BID ===
        $bb = false;
        $bbCss = ".bp-btn.bp-btn-black {\n";
        if ($this->changed($s, $d, 'btn_bid_bg'))        { $bbCss .= "  background-color: {$this->v($s,'btn_bid_bg')} !important;\n"; $bb = true; }
        if ($this->changed($s, $d, 'btn_bid_text'))      { $bbCss .= "  color: {$this->v($s,'btn_bid_text')} !important;\n"; $bb = true; }
        if ($this->changed($s, $d, 'btn_bid_font_size')) { $bbCss .= "  font-size: {$this->v($s,'btn_bid_font_size')}px !important;\n"; $bb = true; }
        $bbCss .= "}\n";
        if ($bb) $css .= $bbCss;

        // === 6. AUCTION CARDS ===
        $acChanged = $this->changed($s, $d, 'auction_card_shadow') || $this->changed($s, $d, 'auction_card_shadow_color') || $this->changed($s, $d, 'auction_card_bg');
        if ($acChanged) {
            $shadow = $this->shadow($s, 'auction_card_shadow', 'auction_card_shadow_color');
            $css .= ".bp-lots .bp-lot.bp-lot_auction {\n";
            $css .= "  box-shadow: {$shadow} !important;\n";
            if ($this->changed($s, $d, 'auction_card_bg')) {
                $css .= "  background-color: {$this->v($s,'auction_card_bg')} !important;\n";
            }
            $css .= "}\n";
        }

        if ($this->changed($s, $d, 'auction_card_hover_shadow') || $this->changed($s, $d, 'auction_card_hover_shadow_color')) {
            $shadow = $this->shadow($s, 'auction_card_hover_shadow', 'auction_card_hover_shadow_color');
            $css .= ".bp-lots .bp-lot.bp-lot_auction:hover { box-shadow: {$shadow} !important; }\n";
        }

        if ($this->changed($s, $d, 'auction_card_title_color')) {
            $css .= ".bp-lots .bp-lot.bp-lot_auction .bp-lot__title .bp-title { color: {$this->v($s,'auction_card_title_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'auction_card_title_size')) {
            $css .= ".bp-lots .bp-lot.bp-lot_auction .bp-lot__title .bp-title { font-size: {$this->v($s,'auction_card_title_size')}px !important; }\n";
        }
        if ($this->changed($s, $d, 'auction_card_date_color')) {
            $css .= ".bp-lots .bp-lot.bp-lot_auction .bp-lot__date { color: {$this->v($s,'auction_card_date_color')} !important; }\n";
        }

        // === 7. CATALOG / LOT CARDS ===
        if ($this->changed($s, $d, 'lot_card_hover_shadow') || $this->changed($s, $d, 'lot_card_hover_shadow_color')) {
            $shadow = $this->shadow($s, 'lot_card_hover_shadow', 'lot_card_hover_shadow_color');
            $css .= ".bp-lots .bp-lot:hover { box-shadow: {$shadow} !important; }\n";
        }

        if ($this->changed($s, $d, 'lot_title_color')) {
            $css .= ".bp-lots .bp-lot .bp-lot__title .bp-title { color: {$this->v($s,'lot_title_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'lot_title_font_size')) {
            $css .= ".bp-lots .bp-lot .bp-lot__title .bp-title { font-size: {$this->v($s,'lot_title_font_size')}px !important; }\n";
        }
        if ($this->changed($s, $d, 'lot_desc_color')) {
            $css .= ".bp-lots .bp-lot .bp-lot__title, .bp-lots .bp-lot .bp-lot__title p, .bp-lots .bp-lot .bp-lot__title a { color: {$this->v($s,'lot_desc_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'lot_desc_border_color')) {
            $css .= ".bp-lots .bp-lot .bp-lot__title { border-bottom-color: {$this->v($s,'lot_desc_border_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'lot_tool_text_color')) {
            $css .= ".bp-lots .bp-lot .bp-lot__tool { color: {$this->v($s,'lot_tool_text_color')} !important; }\n";
            $css .= ".bp-lots .bp-lot .bp-lot__tool .bp-subtitle { color: {$this->v($s,'lot_tool_text_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'lot_price_color')) {
            $css .= ".bp-lots .bp-lot .bp-lot__tool .bp-price { color: {$this->v($s,'lot_price_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'lot_lead_bg')) {
            $css .= ".bp-lots .bp-lot .bp-lot__tool.bp-lead { background-color: {$this->v($s,'lot_lead_bg')} !important; }\n";
        }
        if ($this->changed($s, $d, 'lot_lead_text')) {
            $css .= ".bp-lots .bp-lot .bp-lot__tool.bp-lead .bp-subtitle, .bp-lots .bp-lot .bp-lot__tool.bp-lead .bp-price { color: {$this->v($s,'lot_lead_text')} !important; }\n";
        }

        // === 8. COUNTDOWN TIMER ===
        if ($this->changed($s, $d, 'countdown_bg')) {
            $css .= ".bp-countdown { background: {$this->v($s,'countdown_bg')} !important; }\n";
        }
        if ($this->changed($s, $d, 'countdown_text_color')) {
            $c = $this->v($s, 'countdown_text_color');
            $css .= ".bp-countdown, .bp-countdown .bp-countdown-text, .bp-countdown .bp-countdown-time { color: {$c} !important; }\n";
        }
        if ($this->changed($s, $d, 'countdown_number_size')) {
            $css .= ".bp-countdown .bp-countdown-time { font-size: {$this->v($s,'countdown_number_size')}px !important; }\n";
        }
        if ($this->changed($s, $d, 'countdown_label_size')) {
            $css .= ".bp-countdown .bp-countdown-text { font-size: {$this->v($s,'countdown_label_size')}px !important; }\n";
        }

        // === 9. SINGLE LOT PAGE ===
        if ($this->changed($s, $d, 'single_heading_color')) {
            $css .= ".bp-single-lot .bp-single-lot__head { color: {$this->v($s,'single_heading_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'single_heading_size')) {
            $css .= ".bp-single-lot .bp-single-lot__head { font-size: {$this->v($s,'single_heading_size')}px !important; }\n";
        }

        $spChanged = $this->changed($s, $d, 'single_panel_shadow') || $this->changed($s, $d, 'single_panel_shadow_color') || $this->changed($s, $d, 'single_panel_bg');
        if ($spChanged) {
            $shadow = $this->shadow($s, 'single_panel_shadow', 'single_panel_shadow_color');
            $css .= ".bp-single-lot .bp-control {\n";
            $css .= "  box-shadow: {$shadow} !important;\n";
            if ($this->changed($s, $d, 'single_panel_bg')) {
                $css .= "  background-color: {$this->v($s,'single_panel_bg')} !important;\n";
            }
            $css .= "}\n";
        }

        if ($this->changed($s, $d, 'single_commission_color')) {
            $css .= ".bp-single-lot .bp-control .bp-control__commission, .bp-single-lot .bp-control .bp-control__desc { color: {$this->v($s,'single_commission_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'single_notice_bg')) {
            $css .= ".bp-single-lot .bp-control__notice { background-color: {$this->v($s,'single_notice_bg')} !important; }\n";
        }
        if ($this->changed($s, $d, 'single_notice_text')) {
            $css .= ".bp-single-lot .bp-control__notice .bp-title { color: {$this->v($s,'single_notice_text')} !important; }\n";
        }
        if ($this->changed($s, $d, 'single_bid_highlight')) {
            $css .= ".bp-single-lot .bp-control__notice .bp-bid { color: {$this->v($s,'single_bid_highlight')} !important; }\n";
        }

        // === 10. MODALS ===
        if ($this->changed($s, $d, 'modal_overlay_bg')) {
            $css .= ".bp-modal .modal__overlay { background-color: {$this->v($s,'modal_overlay_bg')} !important; }\n";
        }

        $mcChanged = $this->changed($s, $d, 'modal_container_bg') || $this->changed($s, $d, 'modal_shadow') || $this->changed($s, $d, 'modal_shadow_color');
        if ($mcChanged) {
            $shadow = $this->shadow($s, 'modal_shadow', 'modal_shadow_color');
            $css .= ".bp-modal .modal__container {\n";
            if ($this->changed($s, $d, 'modal_container_bg')) {
                $css .= "  background-color: {$this->v($s,'modal_container_bg')} !important;\n";
            }
            $css .= "  box-shadow: {$shadow} !important;\n";
            $css .= "}\n";
        }

        if ($this->changed($s, $d, 'modal_title_color')) {
            $css .= ".bp-modal .modal__header .modal__title { color: {$this->v($s,'modal_title_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'modal_title_size')) {
            $css .= ".bp-modal .modal__header .modal__title { font-size: {$this->v($s,'modal_title_size')}px !important; }\n";
        }
        if ($this->changed($s, $d, 'modal_link_color')) {
            $css .= ".bp-modal .modal__footer .modal_links__item { color: {$this->v($s,'modal_link_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'modal_btn_bg')) {
            $css .= ".bp-modal .modal__btn-primary { background-color: {$this->v($s,'modal_btn_bg')} !important; }\n";
        }

        // === 11. WISHLIST HEART ===
        if ($this->changed($s, $d, 'heart_outline_color')) {
            $outlineHex = ltrim($this->v($s, 'heart_outline_color'), '#');
            $css .= ".bp__wishlist { background-image: url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='19.556' height='17.736' viewBox='0 0 19.556 17.736'%3E%3Cdefs%3E%3Cstyle%3E.a%7Bfill:none;stroke:%23{$outlineHex};stroke-width:1;%7D%3C/style%3E%3C/defs%3E%3Cpath class='a' d='M638.281,548.136a4.488,4.488,0,0,0-2.282-2.6,5.483,5.483,0,0,0-2.326-.517,4.863,4.863,0,0,0-4.213,2.188l-.115.2-.1-.17a4.861,4.861,0,0,0-4.214-2.192,5.479,5.479,0,0,0-2.324.516,4.488,4.488,0,0,0-2.283,2.6,5.72,5.72,0,0,0,.205,4.29c1.434,3.03,8.223,8.993,8.512,9.245a.3.3,0,0,0,.2.065l.043,0a.336.336,0,0,0,.19-.094c.4-.346,7.08-6.235,8.5-9.24A5.711,5.711,0,0,0,638.281,548.136Z' transform='translate(-619.575 -544.521)'/%3E%3C/svg%3E\") !important; }\n";
        }
        if ($this->changed($s, $d, 'heart_active_color')) {
            $activeHex = ltrim($this->v($s, 'heart_active_color'), '#');
            $css .= ".bp__wishlist.bp__wishlist-active, .bp__wishlist:hover { background-image: url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18.5' height='16.736' viewBox='0 0 18.5 16.736'%3E%3Cpath fill='%23{$activeHex}' d='M638.281,548.136a4.488,4.488,0,0,0-2.282-2.6,5.483,5.483,0,0,0-2.326-.517,4.863,4.863,0,0,0-4.213,2.188l-.115.2-.1-.17a4.861,4.861,0,0,0-4.214-2.192,5.479,5.479,0,0,0-2.324.516,4.488,4.488,0,0,0-2.283,2.6,5.72,5.72,0,0,0,.205,4.29c1.434,3.03,8.223,8.993,8.512,9.245a.3.3,0,0,0,.2.065l.043,0a.336.336,0,0,0,.19-.094c.4-.346,7.08-6.235,8.5-9.24A5.711,5.711,0,0,0,638.281,548.136Z' transform='translate(-620.102 -545.021)'/%3E%3C/svg%3E\") !important; }\n";
        }
        if ($this->changed($s, $d, 'heart_bg')) {
            $css .= ".bp__wishlist, .bp__wishlist.bp__wishlist-active, .bp__wishlist:hover { background-color: {$this->v($s,'heart_bg')} !important; }\n";
        }

        // === 12. FORMS ===
        if ($this->changed($s, $d, 'form_input_border')) {
            $css .= ".bp-from .bp-form-group .bp-form-control { border-color: {$this->v($s,'form_input_border')} !important; }\n";
        }
        if ($this->changed($s, $d, 'form_input_bg')) {
            $css .= ".bp-from .bp-form-group .bp-form-control { background-color: {$this->v($s,'form_input_bg')} !important; }\n";
        }
        if ($this->changed($s, $d, 'form_input_text')) {
            $css .= ".bp-from .bp-form-group .bp-form-control { color: {$this->v($s,'form_input_text')} !important; }\n";
        }
        if ($this->changed($s, $d, 'form_input_font_size')) {
            $css .= ".bp-from .bp-form-group .bp-form-control { font-size: {$this->v($s,'form_input_font_size')}px !important; }\n";
        }
        if ($this->changed($s, $d, 'form_label_color')) {
            $css .= ".bp-from .bp-form-group label, .bp-from .bp-form-group .bp-label { color: {$this->v($s,'form_label_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'form_error_color')) {
            $css .= ".bp-from .bp-form-group .bp-text-error, .bp-from .bp-form-group .bp-form-error { color: {$this->v($s,'form_error_color')} !important; }\n";
        }
        if ($this->changed($s, $d, 'form_checkbox_border')) {
            $css .= ".bp-from .bp-form-group input.bp-form-check-input + label::before { border-color: {$this->v($s,'form_checkbox_border')} !important; }\n";
        }

        return $css;
    }
}
