<!--
  ~ Copyright (C) 2023 Xibo Signage Ltd
  ~
  ~ Xibo - Digital Signage - https://xibosignage.com
  ~
  ~ This file is part of Xibo.
  ~
  ~ Xibo is free software: you can redistribute it and/or modify
  ~ it under the terms of the GNU Affero General Public License as published by
  ~ the Free Software Foundation, either version 3 of the License, or
  ~ any later version.
  ~
  ~ Xibo is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  ~ GNU Affero General Public License for more details.
  ~
  ~ You should have received a copy of the GNU Affero General Public License
  ~ along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
  -->
<module>
    <id>core-countdown-days</id>
    <name>Countdown - Days</name>
    <author>Core</author>
    <description>A module for displaying a Countdown timer for days</description>
    <icon>fa fa-hourglass-o</icon>
    <class></class>
    <compatibilityClass>\Xibo\Widget\Compatibility\CountDownWidgetCompatibility</compatibilityClass>
    <type>countdown-days</type>
    <group id="countdown" icon="fa fa-hourglass-o">Countdown</group>
    <legacyType condition="templateId==countdown4">countdown</legacyType>
    <dataType></dataType>
    <schemaVersion>2</schemaVersion>
    <assignable>1</assignable>
    <regionSpecific>1</regionSpecific>
    <renderAs>html</renderAs>
    <defaultDuration>60</defaultDuration>
    <thumbnail>countdown-days-thumb</thumbnail>
    <startWidth>400</startWidth>
    <startHeight>150</startHeight>
    <settings></settings>
    <properties>
        <property id="countdownType" type="dropdown" mode="single">
            <title>Countdown Type</title>
            <helpText>Please select the type of countdown.</helpText>
            <default>1</default>
            <options>
                <option name="1">Widget Duration</option>
                <option name="2">Custom Duration</option>
                <option name="3">Use Date</option>
            </options>
        </property>
        <property id="countdownDuration" type="number">
            <title>Countdown Duration</title>
            <helpText>The duration in seconds.</helpText>
            <default></default>
            <visibility>
                <test type="or">
                    <condition field="countdownType" type="eq">2</condition>
                </test>
            </visibility>
            <rule>
                <test type="or" message="Please enter a positive countdown duration">
                    <condition type="gt">0</condition>
                    <condition field="countdownType" type="neq">2</condition>
                </test>
            </rule>
        </property>
        <property id="countdownDate" type="date" format="DD/MM/YYYY HH:mm:ss" variant="dateTime">
            <title>Countdown Date</title>
            <helpText>Select the target date and time.</helpText>
            <default>10/10/2010 10:10:10</default>
            <visibility>
                <test type="or">
                    <condition field="countdownType" type="eq">3</condition>
                </test>
            </visibility>
            <rule>
                <test>
                    <condition type="required"></condition>
                </test>
            </rule>
        </property>
        <property id="countdownWarningDuration" type="number">
            <title>Warning Duration</title>
            <helpText>The countdown will show in a warning mode from the end duration entered.</helpText>
            <default></default>
            <visibility>
                <test type="or">
                    <condition field="countdownType" type="eq">1</condition>
                    <condition field="countdownType" type="eq">2</condition>
                </test>
            </visibility>
            <rule onStatus="false">
                <test type="or" message="Warning duration needs to be lower than the countdown main duration.">
                    <condition type="eq"></condition>
                    <condition field="countdownType" type="neq">2</condition>
                    <condition field="countdownDuration" type="lt"></condition>
                </test>
                <test type="or" message="Warning duration needs to be lower than the widget duration.">
                    <condition field="countdownType" type="neq">1</condition>
                    <condition field="duration" type="lt"></condition>
                    <condition type="eq"></condition>
                </test>
                <test type="or" message="Please enter a positive warning duration">
                    <condition type="gt">0</condition>
                    <condition type="eq"></condition>
                </test>
            </rule>
        </property>
        <property id="countdownWarningDate" type="date" format="DD/MM/YYYY HH:mm:ss" variant="dateTime">
            <title>Warning Date</title>
            <helpText>The countdown will show in a warning mode from the warning date entered.</helpText>
            <default></default>
            <visibility>
                <test type="or">
                    <condition field="countdownType" type="eq">3</condition>
                </test>
            </visibility>
            <rule>
                <test type="or" message="Warning date needs to be before countdown date.">
                    <condition field="countdownType" type="neq">3</condition>
                    <condition field="countdownDate" type="lt"></condition>
                    <condition type="eq"></condition>
                </test>
            </rule>
        </property>
        <property id="alignmentH" type="dropdown" mode="single">
            <title>Horizontal Align</title>
            <helpText>How should this widget be horizontally aligned?</helpText>
            <default>center</default>
            <options>
                <option name="left">Left</option>
                <option name="center">Centre</option>
                <option name="right">Right</option>
            </options>
        </property>
        <property id="alignmentV" type="dropdown" mode="single">
            <title>Vertical Align</title>
            <helpText>How should this widget be vertically aligned?</helpText>
            <default>middle</default>
            <options>
                <option name="top">Top</option>
                <option name="middle">Middle</option>
                <option name="bottom">Bottom</option>
            </options>
        </property>
        <property id="fontFamily" type="fontSelector">
            <title>Text Font</title>
            <helpText>Select a custom font - leave empty to use the default font.</helpText>
        </property>
        <property id="textColor" type="color">
            <title>Text Colour</title>
            <default>#1b1e56</default>
        </property>
        <property id="textBackgroundColor" type="color">
            <title>Text Card Background Colour</title>
        </property>
        <property id="borderColor" type="color">
            <title>Text Card Border Colour</title>
            <default>#1b1e56</default>
        </property>
        <property id="labelFontFamily" type="fontSelector">
            <title>Label Font</title>
            <helpText>Select a custom font - leave empty to use the default font.</helpText>
        </property>
        <property id="labelTextColor" type="color">
            <title>Label Colour</title>
            <default>#333</default>
        </property>
        <property id="warningBackgroundColor" type="color">
            <title>Warning Background Colour</title>
            <default>darkgoldenrod</default>
        </property>
        <property id="finishedBackgroundColor" type="color">
            <title>Finished Background Colour</title>
            <default>darkred</default>
        </property>
    </properties>
    <preview></preview>
    <stencil>
        <width id="width">600</width>
        <height id="height">170</height>
        <hbs><![CDATA[
<div class="countdown-container">
    <div id="clockdiv">
        <div>[DD]
            <div class="smalltext">||Days||</div>
        </div>
        <div>[hh]
            <div class="smalltext">||Hours||</div>
        </div>
        <div>[mm]
            <div class="smalltext">||Minutes||</div>
        </div>
        <div>[ss]
            <div class="smalltext">||Seconds||</div>
        </div>
    </div>
</div>
        ]]></hbs>
        <style><![CDATA[
body {
    width: 600px !important;
    height: 170px !important;
}

#clockdiv {
    width: 100%;
    font-family: sans-serif;
    color: #333;
    display: inline-block;
    text-align: center;
    font-size: 0;
}

#clockdiv>div {
    padding: 12px 8px;
    display: inline-block;
    width: 130px;
    font-size: 70px;
}

#clockdiv div>span {
    text-align: center;
    padding: 10px 0px;
    width: 100%;
    border-radius: 20px;
    font-weight: bold;
    display: inline-block;
    color: #1b1e56;
    border: 2px solid #1b1e56;
}

.smalltext {
    padding-top: 10px;
    font-size: 30px;
}

{% if textColor %}#clockdiv div>span { color: {{textColor}} !important; }{% endif %}
{% if fontFamily %}#clockdiv div>span { font-family: {{fontFamily}} !important; }{% endif %}
{% if textBackgroundColor %}#clockdiv div>span { background-color: {{textBackgroundColor}} !important; }{% endif %}
{% if borderColor %}#clockdiv div>span { border-color: {{borderColor}} !important; }{% endif %}
{% if labelTextColor %}#clockdiv { color: {{labelTextColor}} !important; }{% endif %}
{% if labelFontFamily %}#clockdiv { font-family: {{labelFontFamily}} !important; }{% endif %}
{% if warningBackgroundColor %}.warning #clockdiv > div > span {border-color: {{warningBackgroundColor}} !important;color: {{warningBackgroundColor}} !important;}{% endif %}
{% if finishedBackgroundColor %}.finished #clockdiv > div > span {border-color: {{finishedBackgroundColor}} !important;color: {{finishedBackgroundColor}} !important;}{% endif %}
        ]]></style>
    </stencil>
    <onRender><![CDATA[
// Make replacements on render for all templates
var $countdownContainer = $('.countdown-container', target);

// Get countdown container html
var countdownHTML = $countdownContainer.html();

// Make replacements
var text = countdownHTML;
var regex = /\[.*?\]/g;
countdownHTML = text.replace(regex, function (match) {
    var replacement = '';
    var matchParsed = match.replace('[', '').replace(']', '');

    // Replace tags
    switch (matchParsed) {
        case 'ss':
            replacement = '<span class="seconds"></span>';
            break;
        case 'ssa':
            replacement = '<span class="secondsAll"></span>';
            break;
        case 'mm':
            replacement = '<span class="minutes"></span>';
            break;
        case 'mma':
            replacement = '<span class="minutesAll"></span>';
            break;
        case 'hh':
            replacement = '<span class="hours"></span>';
            break;
        case 'hha':
            replacement = '<span class="hoursAll"></span>';
            break;
        case 'DD':
            replacement = '<span class="days"></span>';
            break;
        case 'WW':
            replacement = '<span class="weeks"></span>';
            break;
        case 'MM':
            replacement = '<span class="months"></span>';
            break;
        case 'YY':
            replacement = '<span class="years"></span>';
            break;
        default:
            replacement = 'NULL';
            break;
    }

    return replacement;
});

// Attach html back to container
$countdownContainer.html(countdownHTML);

// Scale the layout
$(target).xiboLayoutScaler(properties);

// Render the countdown
var $contentContainer = $(target).find('#content');
$contentContainer.xiboCountdownRender(properties, $contentContainer.html());
]]></onRender>
    <assets>
        <asset id="countdown-days-thumb" type="path" cmsOnly="true" mimeType="image/png" path="/modules/assets/template-thumbnails/countdown/countdown-days-thumb.png" />
    </assets>
</module>