<?php

/**
 * @package         Smile Pack
 * @version         1.2.3 Pro
 * 
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            http://www.tassos.gr
 * @copyright       Copyright © 2024 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
*/

defined('_JEXEC') or die('Restricted access');

if (!$value = $params->get('value'))
{
	return;
}

$value = json_decode(json_encode($value), true);

$gallery_items = isset($value['items']) ? $value['items'] : [];
if (!$gallery_items)
{
	return;
}

require_once dirname(__DIR__) . '/fields/helper.php';

$arrayParams = $params->toArray();




$style = $params->get('provider', 'grid');
$tags = SPGalleryHelper::getOrderedTags($gallery_items);


// Grid columns
$columns = $style === 'grid' && isset($arrayParams['columns']) ? $arrayParams['columns'] : null;


// Masonry columns
$columns = $style === 'masonry' && isset($arrayParams['masonry_columns']) ? $arrayParams['masonry_columns'] : $columns;


$payload = [
    'items' => SPGalleryHelper::prepareItems($gallery_items, $tags),
    'columns' => $columns,
    'gap' => isset($arrayParams['gap']) ? $arrayParams['gap'] : null,
    
    
    'tags' => $tags,
    'limit_files' => 0,
    'ordering' => $params->get('ordering', 'default'),
    'module' => $params->get('module', ''),
    'justified_item_height' => $params->get('justified_item_height'),
    'lightbox' => $params->get('lightbox', '0') === '1',
    'style' => $style,
    'tags_position' => $params->get('tags.position', 'disabled'),
    'tags_ordering' => $params->get('tags.ordering', 'default'),
    'all_tags_item_label' => $params->get('tags.all_tags_item_label', 'All'),
    'tags_mobile' => $params->get('tags.mobile', 'show'),
    'tags_text_color' => $params->get('tags.text_color', '#555'),
    'tags_text_color_hover' => $params->get('tags.text_color_hover', '#fff'),
    'tags_bg_color_hover' => $params->get('tags.bg_color_hover', '#1E3148')
    
];


// Add Slideshow-related payload
if ($style === 'slideshow')
{
    $payload = array_merge($payload, [
        'slides_per_view' => isset($arrayParams['slides_per_view']) ? $arrayParams['slides_per_view'] : ['desktop' => 1],
        'space_between_slides' => isset($arrayParams['space_between_slides']) ? $arrayParams['space_between_slides'] : ['desktop' => 10],
        'infinite_loop' => $params->get('infinite_loop', '0') === '1',
        'keyboard_control' => $params->get('keyboard_control', '0') === '1',
        'autoplay' => $params->get('autoplay', '0') === '1',
        'autoplay_delay' => $params->get('autoplay_delay', 3000),
        'autoplay_progress' => $params->get('autoplay_progress', '0') === '1',
        'show_thumbnails' => $params->get('show_thumbnails', '0') === '1',
        'show_thumbnails_arrows' => $params->get('show_thumbnails_arrows', '0') === '1',
        'nav_controls' => $params->get('nav_controls', 'none'),
        'theme_color' => $params->get('theme_color', '#333'),
        'transition_effect' => $params->get('transition_effect', 'slide')
    ]);
}

$widgetName = $style === 'slideshow' ? 'Slideshow' : 'Gallery';


echo \NRFramework\Widgets\Helper::render($widgetName, $payload);