<?php
/**
 * Kumibox Pro functions and definitions
 * License: GPLv2 or later
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

function kumibox_setup() {
    add_theme_support('post-thumbnails');
    add_theme_support('title-tag');
    add_theme_support('custom-background', apply_filters('kumibox_custom_background_args', array(
        'default-color' => 'f4f6f8',
        'default-image' => '',
    )));
    add_theme_support('custom-logo', array(
        'height'      => 90,
        'width'       => 300,
        'flex-height' => true,
        'flex-width'  => true,
    ));

    register_nav_menus(array(
        'primary' => __('Główne Menu', 'kumibox-pro'),
        'topbar'  => __('Górne Menu (Top Bar)', 'kumibox-pro'),
    ));
}
add_action('after_setup_theme', 'kumibox_setup');

function kumibox_widgets_init() {
    register_sidebar(array(
        'name'          => 'Główny Panel Boczny (Sidebar)',
        'id'            => 'main-sidebar',
        'before_widget' => '<div id="%1$s" class="bg-[var(--bg-card)] border border-[var(--border-custom)] p-5 shadow-sm mb-8 widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<div class="border-b-2 border-black dark:border-white mb-4 flex justify-between items-end pb-2"><h3 class="text-sm font-bold uppercase bg-[var(--color-primary)] text-white px-3 py-1 -mb-[2px]">',
        'after_title'   => '</h3></div>',
    ));

    register_sidebar(array(
        'name'          => 'Reklama w Nagłówku (max 728x90)',
        'id'            => 'header-ad',
        'before_widget' => '<div class="header-ad-widget w-full flex justify-end">',
        'after_widget'  => '</div>',
        'before_title'  => '<span class="hidden">',
        'after_title'   => '</span>',
    ));

    for ($i = 1; $i <= 3; $i++) {
        register_sidebar(array(
            'name'          => 'Stopka - Kolumna ' . $i,
            'id'            => 'footer-' . $i,
            'before_widget' => '<div id="%1$s" class="mb-8 widget %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h4 class="text-white text-sm font-bold uppercase mb-4 border-b border-gray-800 pb-2">',
            'after_title'   => '</h4>',
        ));
    }
}
add_action('widgets_init', 'kumibox_widgets_init');

add_filter('widget_text', 'do_shortcode');
add_filter('widget_custom_html', 'do_shortcode');

function kumibox_customize_register($wp_customize) {
    // Kolory Motywu i Przycisków
    $wp_customize->add_section('kumibox_appearance', array('title' => 'Kolory i Wygląd', 'priority' => 30));
    
    $wp_customize->add_setting('primary_color', array('default' => '#e62d37', 'transport' => 'refresh', 'sanitize_callback' => 'sanitize_hex_color'));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'primary_color', array('label' => 'Kolor Główny', 'section' => 'kumibox_appearance')));

    $wp_customize->add_setting('button_color', array('default' => '#e62d37', 'transport' => 'refresh', 'sanitize_callback' => 'sanitize_hex_color'));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'button_color', array('label' => 'Kolor Tła Przycisków', 'section' => 'kumibox_appearance')));

    $wp_customize->add_setting('button_text_color', array('default' => '#ffffff', 'transport' => 'refresh', 'sanitize_callback' => 'sanitize_hex_color'));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'button_text_color', array('label' => 'Kolor Tekstu Przycisków', 'section' => 'kumibox_appearance')));

    $wp_customize->add_setting('input_radius', array('default' => '8', 'transport' => 'refresh', 'sanitize_callback' => 'absint'));
    $wp_customize->add_control('input_radius', array('label' => 'Zaokrąglenie Pól i Przycisków (px)', 'section' => 'kumibox_appearance', 'type' => 'number'));

    // Ustawienia Nagłówka
    $wp_customize->add_section('kumibox_header', array('title' => 'Nagłówek (Header)', 'priority' => 31));

    $wp_customize->add_setting('header_logo_alignment', array('default' => 'left', 'sanitize_callback' => 'kumibox_sanitize_select'));
    $wp_customize->add_control('header_logo_alignment', array(
        'label' => 'Pozycja Logo/Nazwy',
        'section' => 'kumibox_header',
        'type' => 'select',
        'choices' => array(
            'left' => 'Do lewej',
            'center' => 'Na środku',
            'right' => 'Do prawej'
        )
    ));

    $wp_customize->add_setting('header_datetime_display', array('default' => 'date', 'sanitize_callback' => 'kumibox_sanitize_select'));
    $wp_customize->add_control('header_datetime_display', array(
        'label' => 'Wyświetlanie daty/godziny',
        'section' => 'kumibox_header',
        'type' => 'select',
        'choices' => array(
            'date' => 'Tylko data',
            'date_time' => 'Data i godzina',
            'none' => 'Ukryj'
        )
    ));

    // Gradient Nagłówka
    $wp_customize->add_section('kumibox_header_gradient', array('title' => 'Gradient Nagłówka', 'priority' => 32));
    
    $wp_customize->add_setting('header_grad_start', array('default' => '#ffffff', 'transport' => 'refresh', 'sanitize_callback' => 'sanitize_hex_color'));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'header_grad_start', array('label' => 'Kolor Gradientu (Początek)', 'section' => 'kumibox_header_gradient')));
    
    $wp_customize->add_setting('header_grad_end', array('default' => '#ffffff', 'transport' => 'refresh', 'sanitize_callback' => 'sanitize_hex_color'));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'header_grad_end', array('label' => 'Kolor Gradientu (Koniec)', 'section' => 'kumibox_header_gradient')));
    
    $wp_customize->add_setting('header_grad_angle', array('default' => '135', 'transport' => 'refresh', 'sanitize_callback' => 'absint'));
    $wp_customize->add_control('header_grad_angle', array('label' => 'Kąt Gradientu (stopnie)', 'section' => 'kumibox_header_gradient', 'type' => 'number'));

    // Ustawienia Meta-danych
    $wp_customize->add_section('kumibox_post_meta', array('title' => 'Widoczność danych wpisu', 'priority' => 33));
    
    $wp_customize->add_setting('show_meta_author', array('default' => true, 'transport' => 'refresh', 'sanitize_callback' => 'kumibox_sanitize_checkbox'));
    $wp_customize->add_control('show_meta_author', array('label' => 'Pokaż Autora', 'section' => 'kumibox_post_meta', 'type' => 'checkbox'));
    
    $wp_customize->add_setting('show_meta_date', array('default' => true, 'transport' => 'refresh', 'sanitize_callback' => 'kumibox_sanitize_checkbox'));
    $wp_customize->add_control('show_meta_date', array('label' => 'Pokaż Datę', 'section' => 'kumibox_post_meta', 'type' => 'checkbox'));
    
    $wp_customize->add_setting('show_meta_comments', array('default' => true, 'transport' => 'refresh', 'sanitize_callback' => 'kumibox_sanitize_checkbox'));
    $wp_customize->add_control('show_meta_comments', array('label' => 'Pokaż Liczbę Komentarzy', 'section' => 'kumibox_post_meta', 'type' => 'checkbox'));

    // Social Media
    $wp_customize->add_section('kumibox_socials', array('title' => 'Linki Społecznościowe', 'priority' => 35));
    $socials = array('facebook' => 'Facebook URL', 'twitter' => 'Twitter (X) URL', 'youtube' => 'YouTube URL');
    foreach($socials as $id => $label) {
        $wp_customize->add_setting('social_'.$id, array('default' => '', 'sanitize_callback' => 'esc_url_raw'));
        $wp_customize->add_control('social_'.$id, array('label' => $label, 'section' => 'kumibox_socials', 'type' => 'url'));
    }

    // Strona Główna
    $wp_customize->add_section('kumibox_homepage', array('title' => 'Zarządzanie Stroną Główną', 'priority' => 40));

    $wp_customize->add_setting('sidebar_position', array('default' => 'right', 'sanitize_callback' => 'kumibox_sanitize_sidebar_pos'));
    $wp_customize->add_control('sidebar_position', array('label' => 'Pozycja Panelu Bocznego', 'section' => 'kumibox_homepage', 'type' => 'select', 'choices' => array('left' => 'Po lewej', 'right' => 'Po prawej')));

    $wp_customize->add_setting('ticker_title', array('default' => 'PILNE', 'sanitize_callback' => 'sanitize_text_field'));
    $wp_customize->add_control('ticker_title', array('label' => 'Tytuł paska Ticker', 'section' => 'kumibox_homepage', 'type' => 'text'));

    $wp_customize->add_setting('ticker_cat', array('default' => 0, 'sanitize_callback' => 'absint'));
    $wp_customize->add_control('ticker_cat', array('label' => 'ID Kategorii paska Ticker (0 to wszystkie)', 'section' => 'kumibox_homepage', 'type' => 'number'));

    $wp_customize->add_setting('section1_title', array('default' => 'Wiadomości', 'sanitize_callback' => 'sanitize_text_field'));
    $wp_customize->add_control('section1_title', array('label' => 'Tytuł Głównej Sekcji', 'section' => 'kumibox_homepage', 'type' => 'text'));
    
    $wp_customize->add_setting('section2_title', array('default' => 'Na Czasie', 'sanitize_callback' => 'sanitize_text_field'));
    $wp_customize->add_control('section2_title', array('label' => 'Tytuł Sekcji Dolnej', 'section' => 'kumibox_homepage', 'type' => 'text'));
    
    $wp_customize->add_setting('sidebar_popular_title', array('default' => 'Popularne', 'sanitize_callback' => 'sanitize_text_field'));
    $wp_customize->add_control('sidebar_popular_title', array('label' => 'Tytuł bloku w Sidebarze', 'section' => 'kumibox_homepage', 'type' => 'text'));

    // Stopka
    $wp_customize->add_section('kumibox_footer', array('title' => 'Stopka (Footer)', 'priority' => 45));
    $wp_customize->add_setting('footer_copyright', array('default' => '&copy; 2026 Wszelkie prawa zastrzeżone.', 'sanitize_callback' => 'wp_kses_post'));
    $wp_customize->add_control('footer_copyright', array('label' => 'Tekst Praw Autorskich', 'section' => 'kumibox_footer', 'type' => 'text'));
}
add_action('customize_register', 'kumibox_customize_register');

/**
 * Sanitization functions
 */
function kumibox_sanitize_checkbox($input) {
    return (isset($input) && true === $input) ? true : false;
}

function kumibox_sanitize_sidebar_pos($input) {
    $valid = array('left' => 'Po lewej', 'right' => 'Po prawej');
    return (array_key_exists($input, $valid)) ? $input : 'right';
}

function kumibox_sanitize_select($input, $setting) {
    $input = sanitize_key($input);
    $choices = $setting->manager->get_control($setting->id)->choices;
    return (array_key_exists($input, $choices) ? $input : $setting->default);
}

function kumibox_customizer_css() {
    $primary_color = get_theme_mod('primary_color', '#e62d37');
    $btn_color = get_theme_mod('button_color', '#e62d37');
    $btn_text = get_theme_mod('button_text_color', '#ffffff');
    $radius = get_theme_mod('input_radius', '8');
    
    $g_start = get_theme_mod('header_grad_start', '#ffffff');
    $g_end = get_theme_mod('header_grad_end', '#ffffff');
    $g_angle = get_theme_mod('header_grad_angle', '135');
    
    echo '<style type="text/css">
        :root {
            --color-primary: ' . esc_attr($primary_color) . ';
            --color-btn: ' . esc_attr($btn_color) . ';
            --color-btn-text: ' . esc_attr($btn_text) . ';
            --radius-custom: ' . esc_attr($radius) . 'px;
            --header-gradient: linear-gradient(' . esc_attr($g_angle) . 'deg, ' . esc_attr($g_start) . ', ' . esc_attr($g_end) . ');
        }
        .pagination-links a, .pagination-links span { padding: 0.5rem 1rem; border: 1px solid var(--border-custom); background: var(--bg-card); transition: all 0.2s; border-radius: var(--radius-custom); }
        .pagination-links span.current, .pagination-links a:hover { background: var(--color-primary); color: white; border-color: var(--color-primary); }
        
        input[type="text"], input[type="email"], input[type="url"], input[type="password"], input[type="search"], input[type="tel"], textarea, .kumibox-input {
            width: 100%; padding: 0.75rem 1rem; border: 1px solid var(--border-custom); background: var(--bg-card); color: var(--text-main); border-radius: var(--radius-custom); outline: none; transition: border-color 0.3s;
        }
        input[type="text"]:focus, input[type="email"]:focus, input[type="url"]:focus, input[type="password"]:focus, input[type="search"]:focus, input[type="tel"]:focus, textarea:focus, .kumibox-input:focus {
            border-color: var(--color-primary); box-shadow: 0 0 0 1px var(--color-primary);
        }
        button, input[type="submit"], input[type="button"], .kumibox-btn {
            background-color: var(--color-btn); color: var(--color-btn-text); padding: 0.75rem 2rem; border: none; border-radius: var(--radius-custom); font-weight: bold; cursor: pointer; transition: opacity 0.3s; text-transform: uppercase; letter-spacing: 0.05em; display: inline-block;
        }
        button:hover, input[type="submit"]:hover, input[type="button"]:hover, .kumibox-btn:hover { opacity: 0.85; }
        
        .cf-turnstile-container iframe, .cf-turnstile { margin-bottom: 15px !important; }
        
        .comment-list .comment { border-bottom: 1px solid var(--border-custom); padding-bottom: 1.5rem; margin-bottom: 1.5rem; }
        .comment-author img { border-radius: 50%; float: left; margin-right: 15px; }
        .comment-meta { font-size: 0.75rem; color: #888; margin-bottom: 10px; }
        .reply a { font-size: 0.75rem; font-weight: bold; color: var(--color-primary); text-transform: uppercase; }

        .kumibox-header-gradient { background: var(--header-gradient); }
    </style>';
}
add_action('wp_head', 'kumibox_customizer_css');

function kumibox_sidebar_class() {
    return get_theme_mod('sidebar_position', 'right') === 'left' ? 'lg:flex-row-reverse' : 'lg:flex-row';
}

function kumibox_scripts() {
    wp_enqueue_script('kumibox-main', get_template_directory_uri() . '/js/main.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'kumibox_scripts');

function kumibox_fix_home_ads_query($query) {
    if (!is_admin() && $query->is_main_query() && (is_home() || is_front_page())) {
        $sec1_cat = get_theme_mod('section1_cat', 0);
        $sec1_exclude = get_theme_mod('section1_exclude', '');
        if ($sec1_cat) $query->set('cat', (int)$sec1_cat);
        if (!empty($sec1_exclude)) $query->set('category__not_in', array_map('intval', explode(',', $sec1_exclude)));
    }
}
add_action('pre_get_posts', 'kumibox_fix_home_ads_query');
add_filter( 'wp_is_application_passwords_available', '__return_true' );
