<?php

namespace Rtc\PlantShopWC\Admin\Views;

use Rtc\PlantShopWC\Admin\Models\LabelEditorModel;
use Rtc\PlantShopWC\Admin\Views\Templates\SettingsPage;
class GeneralSettingsView {
    private const SETTINGS_FIELD_FORMAT = 'plantshop-wc_settings[%s]';

    public static function render() : void {
        SettingsPage::render_settings_page();
    }

    public static function get_args( $args ) : array {
        $options = get_option( $args['option_key'], array() );
        $id = esc_attr( $args['id'] );
        $value = $options[$id] ?? '';
        $description = esc_html( $args['description'] ?? '' );
        $data = $args['data'] ?? '';
        return compact(
            'id',
            'value',
            'description',
            'data'
        );
    }

    public function render_disabled_cats( $args ) : void {
        $vars = $this->get_args( $args );
        $selected = ( is_array( $vars['value'] ) ? $vars['value'] : array() );
        echo '<p class="description">' . esc_html( $vars['description'] ) . '</p>';
        echo '<div class="max-w-[500px] max-h-[300px] overflow-y-auto p-2 rounded-box border border-base-content/20 bg-base-100">';
        foreach ( $vars['data'] as $cat ) {
            $checked = ( in_array( $cat->term_id, $selected, true ) ? 'checked' : '' );
            printf(
                '<label class="block mb-2">
                    <input type="checkbox" name="plantshop-wc_settings[%1$s][]" value="%2$d" %3$s/>
                    %4$s
                </label>',
                esc_attr( $vars['id'] ),
                esc_attr( (int) $cat->term_id ),
                esc_attr( $checked ),
                esc_html( $cat->name )
            );
        }
        echo '</div>';
    }

    public function render_registration_number( $args ) : void {
        $vars = $this->get_args( $args );
        echo '<p class="description">' . esc_html( $vars['description'] ) . '</p>';
        printf( '<label>
                <input type="text" name="plantshop-wc_settings[%1$s]" value="%2$s"/>
            </label>', esc_attr( $vars['id'] ), esc_attr( $vars['value'] ) );
    }

    public function render_toggle( $args ) : void {
        $vars = $this->get_args( $args );
        $field_name = sprintf( self::SETTINGS_FIELD_FORMAT, $vars['id'] );
        $toggle_field_id = 'plantshop-wc_toggle_' . sanitize_html_class( $vars['id'] );
        $is_checked = (string) ($vars['value'] ?? '') === '1';
        if ( !empty( $vars['description'] ) ) {
            echo '<p class="description">' . esc_html( $vars['description'] ) . '</p>';
        }
        ?>
        <label for="<?php 
        echo esc_attr( $toggle_field_id );
        ?>">
            <input type="hidden" name="<?php 
        echo esc_attr( $field_name );
        ?>" value="0"/>
            <input type="checkbox" id="<?php 
        echo esc_attr( $toggle_field_id );
        ?>" name="<?php 
        echo esc_attr( $field_name );
        ?>" value="1" <?php 
        checked( $is_checked );
        ?> />
            <?php 
        esc_html_e( 'Enable', 'plantshop-wc' );
        ?>
        </label>
        <?php 
    }

    public function render_equivalent_replacement_info_url( $args ) : void {
        $vars = $this->get_args( $args );
        echo '<p class="description">' . esc_html( $vars['description'] ) . '</p>';
        printf( '<label>
                <input type="url" placeholder="https://example.com/info" name="plantshop-wc_settings[%1$s]" value="%2$s"/>
            </label>', esc_attr( $vars['id'] ), esc_attr( $vars['value'] ) );
    }

    public function render_company_logo_id( $args ) : void {
        $vars = $this->get_args( $args );
        $attachment_id = intval( $vars['value'] );
        $image_url = ( $attachment_id ? wp_get_attachment_image_url( $attachment_id, 'medium' ) : '' );
        echo '<p class="description">' . esc_html( $vars['description'] ) . '</p>';
        echo '<div class="plantshop-wc-media-field" data-target="' . esc_attr( $vars['id'] ) . '">';
        // Preview
        echo '<div class="plantshop-wc-media-preview" style="margin-bottom:8px;">';
        if ( $image_url ) {
            echo '<img src="' . esc_url( $image_url ) . '" alt="" style="max-height:80px; max-width:240px; height:auto; width:auto;" />';
        } else {
            echo '<em>' . esc_html__( 'No logo selected.', 'plantshop-wc' ) . '</em>';
        }
        echo '</div>';
        printf( '<input type="hidden" name="plantshop-wc_settings[%1$s]" value="%2$s" class="plantshop-wc-media-input" />', esc_attr( $vars['id'] ), esc_attr( $attachment_id ) );
        echo '<button type="button" class="btn plantshop-wc-media-select">' . esc_html__( 'Select logo', 'plantshop-wc' ) . '</button> ';
        echo '<button type="button" class="btn plantshop-wc-media-remove"' . (( $attachment_id ? '' : ' disabled' )) . '>' . esc_html__( 'Remove', 'plantshop-wc' ) . '</button>';
        echo '</div>';
    }

    public function render_botanical_name_checkbox( $args ) : void {
        $vars = $this->get_args( $args );
        $field_name = sprintf( self::SETTINGS_FIELD_FORMAT, $vars['id'] );
        $botanical_checkbox_field_id = 'plantshop-wc_botanical_' . sanitize_html_class( $vars['id'] );
        $is_checked = (string) ($vars['value'] ?? '') === '1';
        if ( !empty( $vars['description'] ) ) {
            echo '<p class="description">' . esc_html( $vars['description'] ) . '</p>';
        }
        ?>
        <label for="
        <?php 
        echo esc_attr( $botanical_checkbox_field_id );
        ?>
        ">
            <input type="hidden" name="
            <?php 
        echo esc_attr( $field_name );
        ?>
            " value="0"/>
            <input
                    type="checkbox"
                    id="
                    <?php 
        echo esc_attr( $botanical_checkbox_field_id );
        ?>
                    "
                    name="
                    <?php 
        echo esc_attr( $field_name );
        ?>
                    "
                    value="1"
                    <?php 
        checked( $is_checked );
        ?>
            />
            <?php 
        esc_html_e( 'Enable', 'plantshop-wc' );
        ?>
        </label>
        <?php 
    }

    public function render_shipping_periods_toggle( $args ) : void {
        $vars = $this->get_args( $args );
        $field_name = sprintf( self::SETTINGS_FIELD_FORMAT, $vars['id'] );
        $shipping_toggle_field_id = 'plantshop-wc_shipping_toggle_' . sanitize_html_class( $vars['id'] );
        $is_checked = (string) ($vars['value'] ?? '') === '1';
        if ( !empty( $vars['description'] ) ) {
            echo '<p class="description">' . esc_html( $vars['description'] ) . '</p>';
        }
        ?>
        <label for="<?php 
        echo esc_attr( $shipping_toggle_field_id );
        ?>">
            <input type="hidden" name="<?php 
        echo esc_attr( $field_name );
        ?>" value="0"/>
            <input type="checkbox" id="<?php 
        echo esc_attr( $shipping_toggle_field_id );
        ?>" name="<?php 
        echo esc_attr( $field_name );
        ?>" value="1" <?php 
        checked( $is_checked );
        ?> />
            <?php 
        esc_html_e( 'Enable', 'plantshop-wc' );
        ?>
        </label>
        <?php 
    }

    public function render_shipping_week_ranges( $args ) : void {
        $vars = $this->get_args( $args );
        $field_name = sprintf( self::SETTINGS_FIELD_FORMAT, $vars['id'] );
        $field_id = 'plantshop-wc_' . sanitize_html_class( $vars['id'] );
        $value = ( is_string( $vars['value'] ) ? $vars['value'] : '' );
        echo '<p class="description">' . esc_html( $vars['description'] ) . '</p>';
        printf(
            '<textarea id="%1$s" name="%2$s" rows="6" style="width: 100%%; font-family: monospace;" placeholder="W12-W16' . PHP_EOL . 'W20' . PHP_EOL . 'W22-W24">%3$s</textarea>',
            esc_attr( $field_id ),
            esc_attr( $field_name ),
            esc_textarea( $value )
        );
        echo '<p class="description">' . esc_html__( 'Use week format (Wnn). One per line. Ranges use a hyphen, e.g., W10-W15.', 'plantshop-wc' ) . '</p>';
        $helper_id = $field_id . '__helper';
        wp_enqueue_script(
            'plantshop-wc-shipping-weeks',
            PLANTSHOP_WC_PUBLIC_URL . 'admin/js/shipping-weeks.js',
            array(),
            PLANTSHOP_WC_PLUGIN_VERSION,
            true
        );
        wp_localize_script( 'plantshop-wc-shipping-weeks', 'plantshop_wc_shipping_weeks', array(
            'helper_id' => $helper_id,
            'l10n'      => array(
                'invalidLine'         => __( 'Invalid line', 'plantshop-wc' ),
                'invalidWeekNumber'   => __( 'Invalid week number', 'plantshop-wc' ),
                'rangeTooLarge'       => __( 'Range too large', 'plantshop-wc' ),
                'allowedWeeks'        => __( 'Allowed weeks', 'plantshop-wc' ),
                'noWeeksSelected'     => __( 'No weeks selected', 'plantshop-wc' ),
                'pleaseEnterStartEnd' => __( 'Please enter start and end week (1-53).', 'plantshop-wc' ),
            ),
        ) );
        ?>
        <div id="<?php 
        echo esc_attr( $helper_id );
        ?>" class="mt-[.5rem] bg-white border border-[#e5e7eb] rounded-lg" data-for-textarea="<?php 
        echo esc_attr( $field_id );
        ?>">
            <div class="flex items-center justify-between p-[.5rem_.75rem] border-b border-[#e5e7eb] gap-[.5rem]">
                <div class="flex flex-wrap gap-[.5rem]">
                    <button type="button" class="btn m-0" data-clear="1"><?php 
        echo esc_html__( 'Clear', 'plantshop-wc' );
        ?></button>
                </div>
                <div class="plantshop-wc-stats text-[#374151] text-[12px] mb-[.5rem]" aria-live="polite"></div>
            </div>
            <div class="pt-[.5rem] pb-[.75rem] px-[.75rem]">
                <div class="flex flex-wrap items-center gap-x-[.75rem] gap-y[.5rem] mb-[.5rem] *:text-[12px]" role="group" aria-label="Add week range">
                    <label>
                        <?php 
        echo esc_html__( 'Start week', 'plantshop-wc' );
        ?>:
                        <span>W</span>
                        <input type="number" class="plantshop-wc-w1 w-[6rem]" min="1" max="53" step="1" placeholder="ww" />
                    </label>
                    <label>
                        <?php 
        echo esc_html__( 'End week', 'plantshop-wc' );
        ?>:
                        <span>W</span>
                        <input type="number" class="plantshop-wc-w2 w-[6rem]" min="1" max="53" step="1" placeholder="ww" />
                    </label>
                    <button type="button" class="btn btn-primary" data-add-range="1"><?php 
        echo esc_html__( 'Add range', 'plantshop-wc' );
        ?></button>
                </div>
                <div class="plantshop-wc-chips grid grid-cols-[repeat(auto-fill,minmax(84px,1fr))] gap-[.5rem] max-h-[180px] overflow-auto" aria-live="polite"></div>
                <div class="plantshop-wc-errors text-[#b91c1c] text-[12px] mt-[.5rem]" role="status"></div>
            </div>
        </div>
        <?php 
    }

}
