<?php
/**
 * Shortcode functionality for BW Gallery
 */

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

class BW_Gallery_Shortcode {
    
    private $db;
    
    public function __construct() {
        $this->db = new BW_Gallery_DB();
    }
    
    public function init() {
        add_shortcode('bw_gallery', array($this, 'render_gallery'));
    }
    
    public function render_gallery($atts) {
        $atts = shortcode_atts(array(
            'id' => 0,
            'columns' => null,
            'gap' => null,
            'lightbox' => null,
            'show_captions' => true
        ), $atts, 'bw_gallery');
        
        $gallery_id = intval($atts['id']);
        
        if (!$gallery_id) {
            return '<p>' . __('Please specify a gallery ID.', 'bw-gallery') . '</p>';
        }
        
        $gallery = $this->db->get_gallery($gallery_id);
        
        if (!$gallery) {
            return '<p>' . __('Gallery not found.', 'bw-gallery') . '</p>';
        }
        
        $images = $this->db->get_gallery_images($gallery_id);
        
        if (empty($images)) {
            return '<p>' . __('No images found in this gallery.', 'bw-gallery') . '</p>';
        }
        
        // Get settings (shortcode attributes override gallery settings)
        $settings = $gallery['settings'];
        $global_settings = get_option('bwg_settings', array());
        
        $columns = $atts['columns'] !== null ? intval($atts['columns']) : 
                  (isset($settings['columns']) ? $settings['columns'] : $global_settings['columns']);
        
        $gap = $atts['gap'] !== null ? intval($atts['gap']) : 
               (isset($settings['gap']) ? $settings['gap'] : $global_settings['gap']);
        
        $lightbox = $atts['lightbox'] !== null ? filter_var($atts['lightbox'], FILTER_VALIDATE_BOOLEAN) : 
                    (isset($settings['lightbox']) ? $settings['lightbox'] : $global_settings['lightbox_enabled']);
        
        $enable_tabs = isset($settings['enable_tabs']) && $settings['enable_tabs'];
        
        // Organize images by tabs if enabled
        $tabs = array();
        $taxonomy = new BW_Gallery_Taxonomy();
        $available_tabs = $taxonomy->get_all_tabs();
        $tab_names = array();
        
        // Build tab name lookup array
        foreach ($available_tabs as $tab) {
            $tab_names[$tab->slug] = $tab->name;
        }
        
        if ($enable_tabs) {
            foreach ($images as $image) {
                if (!empty($image['tab_name']) && isset($tab_names[$image['tab_name']])) {
                    $tab_slug = $image['tab_name'];
                    $tab_display_name = $tab_names[$tab_slug];
                } else {
                    $tab_slug = 'general';
                    $tab_display_name = __('General', 'bw-gallery');
                }
                
                if (!isset($tabs[$tab_display_name])) {
                    $tabs[$tab_display_name] = array();
                }
                $tabs[$tab_display_name][] = $image;
            }
            
            // If no images assigned to any tab, show all in General
            if (empty($tabs)) {
                $tabs[__('General', 'bw-gallery')] = $images;
            }
        } else {
            $tabs[__('All', 'bw-gallery')] = $images;
        }
        
        ob_start();
        include BWG_PLUGIN_DIR . 'templates/frontend/gallery.php';
        return ob_get_clean();
    }
}