<?php
/**
 * Pinterest Follow Us Widget functionality
 *
 * @link       https://pinnerpress.com
 * @since      0.1.0
 *
 * @package    PinnerPress
 * @subpackage PinnerPress/includes
 */

if (!defined('WPINC')) {
    die;
}

/**
 * Pinterest Follow Button Widget Class
 */
if (!class_exists('PinnerPress_Follow_Widget')) {
class PinnerPress_Follow_Widget extends WP_Widget {

    /**
     * Constructor
     */
    public function __construct() {
        parent::__construct(
            'pinnerpress_follow_widget',
            __('Pinterest Follow Button', 'pinnerpress'),
            array(
                'description' => __('Add a Pinterest follow button to your sidebar or widget area.', 'pinnerpress'),
                'classname' => 'pinnerpress-follow-widget'
            )
        );
    }

    /**
     * Widget output
     */
    public function widget($args, $instance) {
        // Get widget settings
        $title = !empty($instance['title']) ? $instance['title'] : '';
        $pinterest_url = !empty($instance['pinterest_url']) ? $instance['pinterest_url'] : get_option('pinnerpress_pinterest_url', '');
        
        // Don't display if no Pinterest URL
        if (empty($pinterest_url)) {
            return;
        }

        // Enqueue styles
        wp_enqueue_style(
            'pinnerpress-follow-block',
            plugin_dir_url(dirname(__FILE__)) . 'public/css/pinnerpress-follow-block.css',
            array(),
            PINNERPRESS_VERSION
        );

        echo wp_kses_post($args['before_widget']);

        if (!empty($title)) {
            echo wp_kses_post($args['before_title']) . esc_html(apply_filters('widget_title', $title)) . wp_kses_post($args['after_title']);
        }

        // Get defaults from settings
        $default_button_text = get_option('pinnerpress_follow_button_default_text', __('Follow on Pinterest', 'pinnerpress'));
        $default_button_color = get_option('pinnerpress_follow_button_default_color', '#E60023');
        $default_text_color = get_option('pinnerpress_follow_button_default_text_color', '#ffffff');
        
        // Prepare block attributes
        $block_attributes = array(
            'pinterestUrl' => esc_url($pinterest_url),
            'buttonText' => !empty($instance['button_text']) ? sanitize_text_field($instance['button_text']) : $default_button_text,
            'buttonColor' => !empty($instance['button_color']) ? sanitize_hex_color($instance['button_color']) : $default_button_color,
            'textColor' => !empty($instance['text_color']) ? sanitize_hex_color($instance['text_color']) : $default_text_color,
            'buttonSize' => !empty($instance['button_size']) && in_array($instance['button_size'], array('small', 'medium', 'large')) ? $instance['button_size'] : 'medium',
            'showIcon' => isset($instance['show_icon']) ? (bool) $instance['show_icon'] : true,
            'alignment' => !empty($instance['alignment']) && in_array($instance['alignment'], array('left', 'center', 'right')) ? $instance['alignment'] : 'center',
            'openInNewTab' => isset($instance['open_in_new_tab']) ? (bool) $instance['open_in_new_tab'] : true
        );

        // Render the button
        echo wp_kses_post(pinnerpress_render_follow_block($block_attributes));

        echo wp_kses_post($args['after_widget']);
    }

    /**
     * Widget form in admin
     */
    public function form($instance) {
        // Set default values
        $title = !empty($instance['title']) ? $instance['title'] : '';
        $pinterest_url = !empty($instance['pinterest_url']) ? $instance['pinterest_url'] : '';
        $button_text = !empty($instance['button_text']) ? $instance['button_text'] : '';
        $button_color = !empty($instance['button_color']) ? $instance['button_color'] : '';
        $text_color = !empty($instance['text_color']) ? $instance['text_color'] : '';
        $button_size = !empty($instance['button_size']) ? $instance['button_size'] : 'medium';
        $show_icon = isset($instance['show_icon']) ? (bool) $instance['show_icon'] : true;
        $alignment = !empty($instance['alignment']) ? $instance['alignment'] : 'center';
        $open_in_new_tab = isset($instance['open_in_new_tab']) ? (bool) $instance['open_in_new_tab'] : true;
        ?>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:', 'pinnerpress'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('pinterest_url')); ?>"><?php esc_html_e('Pinterest URL:', 'pinnerpress'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('pinterest_url')); ?>" name="<?php echo esc_attr($this->get_field_name('pinterest_url')); ?>" type="url" value="<?php echo esc_attr($pinterest_url); ?>" placeholder="<?php echo esc_attr(get_option('pinnerpress_pinterest_url', __('https://www.pinterest.com/yourusername', 'pinnerpress'))); ?>">
            <small><?php esc_html_e('Enter your Pinterest profile URL. Leave empty to use the default from General settings.', 'pinnerpress'); ?></small>
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('button_text')); ?>"><?php esc_html_e('Button Text:', 'pinnerpress'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('button_text')); ?>" name="<?php echo esc_attr($this->get_field_name('button_text')); ?>" type="text" value="<?php echo esc_attr($button_text); ?>" placeholder="<?php echo esc_attr(get_option('pinnerpress_follow_button_default_text', __('Follow on Pinterest', 'pinnerpress'))); ?>">
            <small><?php esc_html_e('Leave empty to use the default from General settings.', 'pinnerpress'); ?></small>
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('button_size')); ?>"><?php esc_html_e('Button Size:', 'pinnerpress'); ?></label>
            <select class="widefat" id="<?php echo esc_attr($this->get_field_id('button_size')); ?>" name="<?php echo esc_attr($this->get_field_name('button_size')); ?>">
                <option value="small" <?php selected($button_size, 'small'); ?>><?php esc_html_e('Small', 'pinnerpress'); ?></option>
                <option value="medium" <?php selected($button_size, 'medium'); ?>><?php esc_html_e('Medium', 'pinnerpress'); ?></option>
                <option value="large" <?php selected($button_size, 'large'); ?>><?php esc_html_e('Large', 'pinnerpress'); ?></option>
            </select>
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('alignment')); ?>"><?php esc_html_e('Alignment:', 'pinnerpress'); ?></label>
            <select class="widefat" id="<?php echo esc_attr($this->get_field_id('alignment')); ?>" name="<?php echo esc_attr($this->get_field_name('alignment')); ?>">
                <option value="left" <?php selected($alignment, 'left'); ?>><?php esc_html_e('Left', 'pinnerpress'); ?></option>
                <option value="center" <?php selected($alignment, 'center'); ?>><?php esc_html_e('Center', 'pinnerpress'); ?></option>
                <option value="right" <?php selected($alignment, 'right'); ?>><?php esc_html_e('Right', 'pinnerpress'); ?></option>
            </select>
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('button_color')); ?>"><?php esc_html_e('Button Color:', 'pinnerpress'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('button_color')); ?>" name="<?php echo esc_attr($this->get_field_name('button_color')); ?>" type="text" value="<?php echo esc_attr($button_color); ?>" placeholder="<?php echo esc_attr(get_option('pinnerpress_follow_button_default_color', '#E60023')); ?>">
            <small><?php esc_html_e('Enter a hex color code or leave empty to use the default from General settings.', 'pinnerpress'); ?></small>
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('text_color')); ?>"><?php esc_html_e('Text Color:', 'pinnerpress'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('text_color')); ?>" name="<?php echo esc_attr($this->get_field_name('text_color')); ?>" type="text" value="<?php echo esc_attr($text_color); ?>" placeholder="<?php echo esc_attr(get_option('pinnerpress_follow_button_default_text_color', '#ffffff')); ?>">
            <small><?php esc_html_e('Enter a hex color code or leave empty to use the default from General settings.', 'pinnerpress'); ?></small>
        </p>

        <p>
            <input class="checkbox" type="checkbox" <?php checked($show_icon); ?> id="<?php echo esc_attr($this->get_field_id('show_icon')); ?>" name="<?php echo esc_attr($this->get_field_name('show_icon')); ?>" value="1">
            <label for="<?php echo esc_attr($this->get_field_id('show_icon')); ?>"><?php esc_html_e('Show Pinterest Icon', 'pinnerpress'); ?></label>
        </p>

        <p>
            <input class="checkbox" type="checkbox" <?php checked($open_in_new_tab); ?> id="<?php echo esc_attr($this->get_field_id('open_in_new_tab')); ?>" name="<?php echo esc_attr($this->get_field_name('open_in_new_tab')); ?>" value="1">
            <label for="<?php echo esc_attr($this->get_field_id('open_in_new_tab')); ?>"><?php esc_html_e('Open in New Tab', 'pinnerpress'); ?></label>
        </p>
        <?php
    }

    /**
     * Update widget settings
     */
    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
        $instance['pinterest_url'] = (!empty($new_instance['pinterest_url'])) ? esc_url_raw($new_instance['pinterest_url']) : '';
        $instance['button_text'] = (!empty($new_instance['button_text'])) ? sanitize_text_field($new_instance['button_text']) : '';
        $instance['button_color'] = (!empty($new_instance['button_color'])) ? sanitize_hex_color($new_instance['button_color']) : '';
        $instance['text_color'] = (!empty($new_instance['text_color'])) ? sanitize_hex_color($new_instance['text_color']) : '';
        $instance['button_size'] = (!empty($new_instance['button_size']) && in_array($new_instance['button_size'], array('small', 'medium', 'large'))) ? $new_instance['button_size'] : 'medium';
        $instance['show_icon'] = !empty($new_instance['show_icon']) ? 1 : 0;
        $instance['alignment'] = (!empty($new_instance['alignment']) && in_array($new_instance['alignment'], array('left', 'center', 'right'))) ? $new_instance['alignment'] : 'center';
        $instance['open_in_new_tab'] = !empty($new_instance['open_in_new_tab']) ? 1 : 0;

        return $instance;
    }
}

/**
 * Register the widget
 */
if (!function_exists('pinnerpress_register_follow_widget')) {
function pinnerpress_register_follow_widget() {
    register_widget('PinnerPress_Follow_Widget');
}
}

}
add_action('widgets_init', 'pinnerpress_register_follow_widget'); 