<?php

/**
 * The WooCommerce settings tab for the plugin.
 *
 * @package CartGiftWrapping
 */
defined( 'ABSPATH' ) || exit;
/**
 * Class CGWFW_Settings_Page
 */
class CGWFW_Settings_Page extends WC_Settings_Page {
    /**
     * Settings helper.
     *
     * @var CGWFW_Settings
     */
    private $settings;

    /**
     * Constructor.
     *
     * @param CGWFW_Settings $settings Settings helper.
     */
    public function __construct( $settings ) {
        $this->settings = $settings;
        $this->id = 'cgwfw';
        $this->label = __( 'Gift Wrapping', 'tinyplugs-gift-wrapping' );
        parent::__construct();
    }

    /**
     * Build the settings array.
     *
     * @return array
     */
    public function get_settings_for_default_section() {
        $d = $this->settings->defaults();
        $settings = array();
        $settings[] = array(
            'name' => __( 'Gift Wrapping', 'tinyplugs-gift-wrapping' ),
            'type' => 'title',
            'desc' => __( 'Offer gift wrapping in the cart. By Tinyplugs.com.', 'tinyplugs-gift-wrapping' ),
            'id'   => 'cgwfw_general',
        );
        $settings[] = array(
            'id'       => 'cgwfw_mode',
            'name'     => __( 'Wrapping mode', 'tinyplugs-gift-wrapping' ),
            'desc_tip' => __( 'Per item shows a button under each cart product. Per order shows a single option below the cart totals.', 'tinyplugs-gift-wrapping' ),
            'type'     => 'select',
            'default'  => $d['mode'],
            'options'  => array(
                'per_order' => __( 'Per order (one option for the whole cart)', 'tinyplugs-gift-wrapping' ),
                'per_item'  => __( 'Per item (button under each cart product — classic cart only)', 'tinyplugs-gift-wrapping' ),
            ),
        );
        $settings[] = array(
            'id'                => 'cgwfw_fee',
            'name'              => __( 'Flat fee per wrap', 'tinyplugs-gift-wrapping' ),
            'desc_tip'          => __( 'The amount charged for each wrapped item. In per-order mode it is charged once.', 'tinyplugs-gift-wrapping' ),
            'type'              => 'text',
            'default'           => $d['fee'],
            'custom_attributes' => array(
                'step' => '0.01',
                'min'  => '0',
            ),
        );
        $settings[] = array(
            'id'       => 'cgwfw_fee_per',
            'name'     => __( 'Charge the fee', 'tinyplugs-gift-wrapping' ),
            'desc_tip' => __( 'Per item multiplies the fee by the quantity (4 of the same product = 4 fees). Per line charges one fee per wrapped row regardless of quantity. Only applies to per-item mode.', 'tinyplugs-gift-wrapping' ),
            'type'     => 'select',
            'default'  => $d['fee_per'],
            'options'  => array(
                'unit' => __( 'Per item (multiply by quantity)', 'tinyplugs-gift-wrapping' ),
                'line' => __( 'Per cart line (one fee regardless of quantity)', 'tinyplugs-gift-wrapping' ),
            ),
        );
        $settings[] = array(
            'id'      => 'cgwfw_taxable',
            'name'    => __( 'Charge tax on the fee?', 'tinyplugs-gift-wrapping' ),
            'type'    => 'select',
            'default' => $d['taxable'],
            'options' => array(
                'no'  => __( 'No', 'tinyplugs-gift-wrapping' ),
                'yes' => __( 'Yes', 'tinyplugs-gift-wrapping' ),
            ),
        );
        $settings[] = array(
            'id'       => 'cgwfw_tax_class',
            'name'     => __( 'Fee tax class', 'tinyplugs-gift-wrapping' ),
            'desc_tip' => __( 'The tax class applied to the wrapping fee when tax is charged. Use Standard unless gift wrapping is taxed at a different rate in your region.', 'tinyplugs-gift-wrapping' ),
            'type'     => 'select',
            'default'  => $d['tax_class'],
            'options'  => ( function_exists( 'wc_get_product_tax_class_options' ) ? wc_get_product_tax_class_options() : array(
                '' => __( 'Standard', 'tinyplugs-gift-wrapping' ),
            ) ),
        );
        $settings[] = array(
            'type' => 'sectionend',
            'id'   => 'cgwfw_general',
        );
        // --- Gift message ---
        $settings[] = array(
            'name' => __( 'Gift Message', 'tinyplugs-gift-wrapping' ),
            'type' => 'title',
            'id'   => 'cgwfw_message_section',
        );
        $settings[] = array(
            'id'      => 'cgwfw_enable_message',
            'name'    => __( 'Allow a gift message?', 'tinyplugs-gift-wrapping' ),
            'type'    => 'checkbox',
            'default' => $d['enable_message'],
            'desc'    => __( 'Let customers add a gift message when they wrap an item.', 'tinyplugs-gift-wrapping' ),
        );
        $settings[] = array(
            'id'                => 'cgwfw_message_limit',
            'name'              => __( 'Message character limit', 'tinyplugs-gift-wrapping' ),
            'type'              => 'number',
            'default'           => $d['message_limit'],
            'custom_attributes' => array(
                'min'  => '0',
                'step' => '1',
            ),
        );
        $settings[] = array(
            'type' => 'sectionend',
            'id'   => 'cgwfw_message_section',
        );
        // --- Labels ---
        $settings[] = array(
            'name' => __( 'Labels & Text', 'tinyplugs-gift-wrapping' ),
            'type' => 'title',
            'id'   => 'cgwfw_labels_section',
        );
        $settings[] = array(
            'id'      => 'cgwfw_button_label',
            'name'    => __( 'Button / switch label (not wrapped)', 'tinyplugs-gift-wrapping' ),
            'type'    => 'text',
            'default' => $d['button_label'],
        );
        $settings[] = array(
            'id'      => 'cgwfw_button_label_added',
            'name'    => __( 'Button label (wrapped)', 'tinyplugs-gift-wrapping' ),
            'type'    => 'text',
            'default' => $d['button_label_added'],
        );
        $settings[] = array(
            'id'       => 'cgwfw_order_style',
            'name'     => __( 'Per-order display style', 'tinyplugs-gift-wrapping' ),
            'desc_tip' => __( 'How the whole-order gift wrap option looks. Only used in per-order mode.', 'tinyplugs-gift-wrapping' ),
            'type'     => 'select',
            'default'  => $d['order_style'],
            'options'  => array(
                'checkbox' => __( 'Checkbox (simple, in cart totals)', 'tinyplugs-gift-wrapping' ),
                'card'     => __( 'Card (promo box with icon & button)', 'tinyplugs-gift-wrapping' ),
            ),
        );
        $settings[] = array(
            'id'       => 'cgwfw_order_show_on',
            'name'     => __( 'Show the per-order option on', 'tinyplugs-gift-wrapping' ),
            'desc_tip' => __( 'Where the whole-order gift wrap option appears. Only used in per-order mode.', 'tinyplugs-gift-wrapping' ),
            'type'     => 'select',
            'default'  => $d['order_show_on'],
            'options'  => array(
                'both'     => __( 'Cart and checkout', 'tinyplugs-gift-wrapping' ),
                'cart'     => __( 'Cart only', 'tinyplugs-gift-wrapping' ),
                'checkout' => __( 'Checkout only', 'tinyplugs-gift-wrapping' ),
            ),
        );
        $settings[] = array(
            'id'      => 'cgwfw_order_label',
            'name'    => __( 'Per-order option label / card title', 'tinyplugs-gift-wrapping' ),
            'type'    => 'text',
            'default' => $d['order_label'],
        );
        $settings[] = array(
            'id'       => 'cgwfw_order_subtitle',
            'name'     => __( 'Card subtitle', 'tinyplugs-gift-wrapping' ),
            'desc_tip' => __( 'Small text under the card title. Card style only.', 'tinyplugs-gift-wrapping' ),
            'type'     => 'text',
            'default'  => $d['order_subtitle'],
        );
        $settings[] = array(
            'id'      => 'cgwfw_card_add_label',
            'name'    => __( 'Card button (not added)', 'tinyplugs-gift-wrapping' ),
            'type'    => 'text',
            'default' => $d['card_add_label'],
        );
        $settings[] = array(
            'id'      => 'cgwfw_card_added_label',
            'name'    => __( 'Card button (added)', 'tinyplugs-gift-wrapping' ),
            'type'    => 'text',
            'default' => $d['card_added_label'],
        );
        $settings[] = array(
            'id'      => 'cgwfw_message_label',
            'name'    => __( 'Gift message field label', 'tinyplugs-gift-wrapping' ),
            'type'    => 'text',
            'default' => $d['message_label'],
        );
        $settings[] = array(
            'id'      => 'cgwfw_fee_label',
            'name'    => __( 'Fee label (cart totals & order)', 'tinyplugs-gift-wrapping' ),
            'type'    => 'text',
            'default' => $d['fee_label'],
        );
        $settings[] = array(
            'id'      => 'cgwfw_enable_tooltip',
            'name'    => __( 'Show an info tooltip?', 'tinyplugs-gift-wrapping' ),
            'type'    => 'checkbox',
            'default' => $d['enable_tooltip'],
            'desc'    => __( 'Add a small "?" icon next to the gift wrap option. Hovering or focusing it shows a short explanation. Works in both per-item and per-order modes.', 'tinyplugs-gift-wrapping' ),
        );
        $settings[] = array(
            'id'                => 'cgwfw_tooltip_text',
            'name'              => __( 'Tooltip text', 'tinyplugs-gift-wrapping' ),
            'desc_tip'          => __( 'The text shown inside the tooltip (max 200 characters).', 'tinyplugs-gift-wrapping' ),
            'type'              => 'text',
            'default'           => $d['tooltip_text'],
            'css'               => 'min-width:320px;',
            'custom_attributes' => array(
                'maxlength' => '200',
            ),
        );
        $settings[] = array(
            'type' => 'sectionend',
            'id'   => 'cgwfw_labels_section',
        );
        // --- Button styling ---
        $settings[] = array(
            'name' => __( 'Button Style', 'tinyplugs-gift-wrapping' ),
            'type' => 'title',
            'desc' => __( 'Customize the look of the per-item gift wrap button.', 'tinyplugs-gift-wrapping' ),
            'id'   => 'cgwfw_style_section',
        );
        $settings[] = array(
            'id'      => 'cgwfw_btn_bg',
            'name'    => __( 'Button background', 'tinyplugs-gift-wrapping' ),
            'type'    => 'color',
            'default' => $d['btn_bg'],
        );
        $settings[] = array(
            'id'      => 'cgwfw_btn_color',
            'name'    => __( 'Button text color', 'tinyplugs-gift-wrapping' ),
            'type'    => 'color',
            'default' => $d['btn_color'],
        );
        $settings[] = array(
            'type' => 'sectionend',
            'id'   => 'cgwfw_style_section',
        );
        // --- Advanced ---
        $settings[] = array(
            'name' => __( 'Advanced', 'tinyplugs-gift-wrapping' ),
            'type' => 'title',
            'id'   => 'cgwfw_advanced_section',
        );
        $settings[] = array(
            'id'      => 'cgwfw_delete_on_uninstall',
            'name'    => __( 'Delete settings on uninstall', 'tinyplugs-gift-wrapping' ),
            'type'    => 'checkbox',
            'default' => $d['delete_on_uninstall'],
            'desc'    => __( 'Remove all of this plugin\'s settings from the database when the plugin is deleted. Leave unchecked to keep your settings if you reinstall later.', 'tinyplugs-gift-wrapping' ),
        );
        $settings[] = array(
            'type' => 'sectionend',
            'id'   => 'cgwfw_advanced_section',
        );
        return apply_filters( 'cgwfw_settings', $settings );
    }

    /**
     * Output the section. In the free version, prepend a short note about the
     * separately available Pro plugin *outside* the settings form table so it
     * cannot affect field alignment, then render the fields as usual.
     *
     * @return void
     */
    public function output() {
        if ( !$this->settings->is_pro() ) {
            $this->render_pro_note();
        }
        parent::output();
        $this->render_support_link();
    }

    /**
     * Render the priority-support link below the settings. Pro only: the free
     * version shows no support link.
     *
     * @return void
     */
    public function render_support_link() {
        if ( !$this->settings->is_pro() ) {
            return;
        }
        $url = 'https://www.tinyplugs.com/support/';
        $label = __( 'Need help? Open a priority support ticket', 'tinyplugs-gift-wrapping' );
        printf( '<p style="margin-top:18px;color:#50575e;"><a href="%s" target="_blank" rel="noopener noreferrer">%s &rarr;</a></p>', esc_url( $url ), esc_html( $label ) );
    }

    /**
     * Render a short note about the separately available Pro plugin (shown in
     * the free version only). The Pro version is a separate plugin distributed
     * outside WordPress.org; nothing in this plugin is locked or gated.
     *
     * @return void
     */
    public function render_pro_note() {
        $url = ( function_exists( 'cgwfw_' ) ? cgwfw_()->get_upgrade_url() : 'https://www.tinyplugs.com/plugins/gift-wrapping-for-woocommerce-cart-checkout/' );
        ?>
		<div style="border:1px solid #dcdcde;border-left:4px solid #2271b1;background:#fff;border-radius:4px;padding:14px 16px;max-width:760px;margin:12px 0 4px;">
			<strong style="font-size:14px;"><?php 
        esc_html_e( 'Gift Wrapping Pro', 'tinyplugs-gift-wrapping' );
        ?></strong>
			<p style="margin:6px 0 10px;color:#50575e;">
				<?php 
        esc_html_e( 'A separate Pro plugin is also available with additional features: multiple wrap designs, product-page wrapping, category & product targeting, a "gift wrapping included" badge, and extra display styles.', 'tinyplugs-gift-wrapping' );
        ?>
			</p>
			<a href="<?php 
        echo esc_url( $url );
        ?>" class="button button-primary"><?php 
        esc_html_e( 'Learn more about Pro', 'tinyplugs-gift-wrapping' );
        ?></a>
		</div>
		<?php 
    }

    /**
     * Product category options for the multiselect.
     *
     * @return array
     */
    private function get_category_options() {
        $terms = get_terms( array(
            'taxonomy'   => 'product_cat',
            'hide_empty' => false,
        ) );
        $options = array();
        if ( !is_wp_error( $terms ) ) {
            foreach ( $terms as $term ) {
                $options[$term->term_id] = $term->name;
            }
        }
        return $options;
    }

    /**
     * Build id => name options for the currently selected products, so the
     * product-search multiselect can display them. The AJAX search itself
     * (wc-product-search) supplies the rest.
     *
     * @param array $ids Selected product IDs.
     * @return array
     */
    private function get_product_options( $ids ) {
        $options = array();
        foreach ( (array) $ids as $id ) {
            $product = wc_get_product( (int) $id );
            if ( $product ) {
                $options[$id] = wp_strip_all_tags( $product->get_formatted_name() );
            }
        }
        return $options;
    }

    /**
     * Persist the wrap designs. WooCommerce verifies the settings nonce before
     * calling save(), so $_POST is already validated here.
     *
     * @return void
     */
    public function save() {
        parent::save();
    }

}
