<?php
/**
 * @package     RN.Module
 * @subpackage  mod_rn_vintage_watche
 * @copyright   Copyright (C) 2025 RN. All rights reserved.
 * @license     GNU General Public License version 3 or later; see LICENSE.txt
 */

\defined('_JEXEC') or die;

use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;

$mediaUrl        = Uri::root() . 'media/mod_rn_vintage_watche/';
$dialImage       = $params->get('dial_image', 'cover-1.webp');
$dialImageCustom = trim($params->get('dial_image_custom', ''));
$clockSize       = (int) $params->get('clock_size', 300);
$showSeconds     = (int) $params->get('show_seconds', 1);
$handColor       = $params->get('hand_color', '#2c1a0e');
$secondColor     = $params->get('second_color', '#8b0000');
$handStyle       = $params->get('hand_style', 'classic');
$showShadow      = (int) $params->get('show_shadow', 1);
$borderStyle     = $params->get('border_style', 'none');
$borderWidth     = (int) $params->get('border_width', 12);
$borderColor     = $params->get('border_color', '#8b6914');
$borderColor2    = $params->get('border_color2', '#f5e6a0');
$customCss       = trim($params->get('custom_css', ''));
$playSound       = (int) $params->get('play_sound', 0);
$soundVolume     = (int) $params->get('sound_volume', 50);
$glassEffect     = $params->get('glass_effect', 'none');
$moduleId        = 'rn-vintage-clock-' . $module->id;

if ($dialImageCustom !== '') {
    $imageUrl = strpos($dialImageCustom, 'http') === 0
        ? $dialImageCustom
        : Uri::root() . ltrim($dialImageCustom, '/');
} else {
    $imageUrl = Uri::root() . 'media/mod_rn_vintage_watche/images/' . htmlspecialchars($dialImage, ENT_QUOTES, 'UTF-8');
}

$svgStyles  = ['dauphine', 'breguet', 'cathedral', 'skeleton', 'royal'];
$isSvgStyle = in_array($handStyle, $svgStyles, true);

function rnGetSvgHandPath(string $style, string $type): string
{
    switch ($style) {

        case 'dauphine':
            if ($type === 'hour') {
                return 'M5,2 C7,15 9,35 8,55 C9,70 7,85 5,100 C3,85 1,70 2,55 C1,35 3,15 5,2 Z
                        M5,2 L5,100';
            }
            if ($type === 'minute') {
                return 'M5,1 C6.5,12 8.5,30 7.5,52 C8.5,68 7,84 5,100 C3,84 1.5,68 2.5,52 C1.5,30 3.5,12 5,1 Z
                        M5,1 L5,100';
            }
            return 'M5,0 C5.8,20 6.5,45 6,70 L5,100 L4,70 C3.5,45 4.2,20 5,0 Z';

        case 'breguet':
            if ($type === 'hour') {
                return 'M5,18 m-4,0 a4,4 0 1,0 8,0 a4,4 0 1,0 -8,0
                        M5,14 L4.5,55 L3.5,100 L6.5,100 L5.5,55 Z';
            }
            if ($type === 'minute') {
                return 'M5,12 m-3.5,0 a3.5,3.5 0 1,0 7,0 a3.5,3.5 0 1,0 -7,0
                        M5,8.5 L4.5,50 L3.5,100 L6.5,100 L5.5,50 Z';
            }
            return 'M5,10 m-2.5,0 a2.5,2.5 0 1,0 5,0 a2.5,2.5 0 1,0 -5,0
                    M5,7.5 L4.7,55 L4,100 L6,100 L5.3,55 Z';

        case 'cathedral':
            if ($type === 'hour') {
                return 'M5,2 L8,30 L8.5,60 Q8.5,75 7,85 Q6,95 5,100 Q4,95 3,85 Q1.5,75 1.5,60 L2,30 Z
                        M5,55 Q3.5,65 3,75 Q4,80 5,82 Q6,80 7,75 Q6.5,65 5,55 Z';
            }
            if ($type === 'minute') {
                return 'M5,1 L7.5,22 L8,55 Q8,70 6.5,82 Q5.8,92 5,100 Q4.2,92 3.5,82 Q2,70 2,55 L2.5,22 Z
                        M5,50 Q3.5,60 3,70 Q4,75 5,77 Q6,75 7,70 Q6.5,60 5,50 Z';
            }
            return 'M5,0 L6.5,25 L7,55 Q7,72 6,84 L5,100 L4,84 Q3,72 3,55 L3.5,25 Z';

        case 'skeleton':
            if ($type === 'hour') {
                return 'M5,3 L8,28 L8,85 Q8,95 5,100 Q2,95 2,85 L2,28 Z
                        M5,3 L7,27 L7,84 Q7,93 5,97 Q3,93 3,84 L3,27 Z';
            }
            if ($type === 'minute') {
                return 'M5,2 L7.5,22 L7.5,86 Q7.5,95 5,100 Q2.5,95 2.5,86 L2.5,22 Z
                        M5,2 L6.5,21 L6.5,85 Q6.5,93 5,97 Q3.5,93 3.5,85 L3.5,21 Z';
            }
            return 'M5,1 L6.5,20 L6.5,88 Q6.5,96 5,100 Q3.5,96 3.5,88 L3.5,20 Z
                    M5,1 L5.8,19 L5.8,87 Q5.8,94 5,97 Q4.2,94 4.2,87 L4.2,19 Z';

        case 'royal':
            if ($type === 'hour') {
                return 'M5,3 L9,25 L9,45 Q9,52 7,55 L6,100 L4,100 L3,55 Q1,52 1,45 L1,25 Z
                        M5,28 Q3.5,36 3.5,44 Q4.2,50 5,51 Q5.8,50 6.5,44 Q6.5,36 5,28 Z';
            }
            if ($type === 'minute') {
                return 'M5,2 L8.5,20 L8.5,42 Q8.5,50 6.5,53 L5.5,100 L4.5,100 L3.5,53 Q1.5,50 1.5,42 L1.5,20 Z
                        M5,23 Q3.5,31 3.5,40 Q4.2,47 5,48 Q5.8,47 6.5,40 Q6.5,31 5,23 Z';
            }
            return 'M5,1 L7.5,18 L7.5,40 Q7.5,47 6,50 L5.5,100 L4.5,100 L4,50 Q2.5,47 2.5,40 L2.5,18 Z';

        default:
            return '';
    }
}
?>

<link rel="stylesheet" href="<?php echo $mediaUrl; ?>css/style.css">
<script src="<?php echo $mediaUrl; ?>js/clock.js"></script>

<?php if ($customCss !== '') : ?>
<style><?php echo strip_tags($customCss); ?></style>
<?php endif; ?>

<div class="rn-vintage-clock-wrap<?php echo $showShadow ? ' rn-clock-shadow' : ''; ?>"
     style="padding: <?php echo $borderStyle !== 'none' ? $borderWidth : 12; ?>px;">
    <div
        id="<?php echo $moduleId; ?>"
        class="rn-vintage-clock<?php echo $glassEffect !== 'none' ? ' rn-glass-' . htmlspecialchars($glassEffect, ENT_QUOTES, 'UTF-8') : ''; ?><?php echo $handStyle !== 'classic' ? ' rn-hands-' . htmlspecialchars($handStyle, ENT_QUOTES, 'UTF-8') : ''; ?>"
        style="
            width: <?php echo $clockSize; ?>px;
            height: <?php echo $clockSize; ?>px;
            background-image: url('<?php echo $imageUrl; ?>');
            background-size: <?php echo $clockSize; ?>px;
            --clock-size: <?php echo $clockSize; ?>px;
            --hand-color: <?php echo htmlspecialchars($handColor, ENT_QUOTES, 'UTF-8'); ?>;
            --second-color: <?php echo htmlspecialchars($secondColor, ENT_QUOTES, 'UTF-8'); ?>;
            --border-width: <?php echo $borderWidth; ?>px;
            --border-color: <?php echo htmlspecialchars($borderColor, ENT_QUOTES, 'UTF-8'); ?>;
            --border-color2: <?php echo htmlspecialchars($borderColor2, ENT_QUOTES, 'UTF-8'); ?>;
            opacity: 0;
        "
        data-show-seconds="<?php echo $showSeconds; ?>"
        data-play-sound="<?php echo $playSound; ?>"
        data-sound-url="<?php echo $playSound ? htmlspecialchars($mediaUrl . 'sound/the-ticking-of-a-quartz-clock.mp3', ENT_QUOTES, 'UTF-8') : ''; ?>"
        data-sound-volume="<?php echo $soundVolume; ?>"
        aria-label="<?php echo htmlspecialchars(Text::_('MOD_RN_VINTAGE_WATCHE'), ENT_QUOTES, 'UTF-8'); ?>"
        role="img"
    >
        <div class="rn-clock-center"></div>

        <?php if ($isSvgStyle) : ?>

        <svg class="rn-clock-hand rn-hand-hour" id="<?php echo $moduleId; ?>-hour"
             viewBox="0 0 10 100" xmlns="http://www.w3.org/2000/svg">
            <path d="<?php echo rnGetSvgHandPath($handStyle, 'hour'); ?>"
                  fill="var(--hand-color)" stroke="none"/>
        </svg>

        <svg class="rn-clock-hand rn-hand-minute" id="<?php echo $moduleId; ?>-minute"
             viewBox="0 0 10 100" xmlns="http://www.w3.org/2000/svg">
            <path d="<?php echo rnGetSvgHandPath($handStyle, 'minute'); ?>"
                  fill="var(--hand-color)" stroke="none"/>
        </svg>

        <?php else : ?>

        <div class="rn-clock-hand rn-hand-hour" id="<?php echo $moduleId; ?>-hour"></div>
        <div class="rn-clock-hand rn-hand-minute" id="<?php echo $moduleId; ?>-minute"></div>
        <?php if ($showSeconds) : ?>
        <div class="rn-clock-hand rn-hand-second" id="<?php echo $moduleId; ?>-second"></div>
        <?php endif; ?>

        <?php endif; ?>

        <?php if ($borderStyle !== 'none') : ?>
        <div class="rn-clock-ring rn-border-<?php echo htmlspecialchars($borderStyle, ENT_QUOTES, 'UTF-8'); ?>"></div>
        <?php endif; ?>
    </div>
</div>

<script>
(function () {
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function () {
            RnVintageClock.init('<?php echo $moduleId; ?>');
        });
    } else {
        RnVintageClock.init('<?php echo $moduleId; ?>');
    }
})();
</script>
