<?php

$protocol       = $this->getParam('protocol', '');
$domain         = $this->getParam('domain', '');
$endpoint       = $this->getParam('endpoint', '');
$platforms      = $this->getParam('platforms', []);
$version        = $this->getParam('version', '0.0.0');
$isDev          = $this->getParam('isDev', false);
$litespeed      = $this->getParam('litespeed', true);
$analytics      = $this->getParam('analytics', 'UA-26264668-9');
$mode           = $this->getParam('mode', '');
$canonical      = $this->getParam('canonical', '');
$image          = $this->getParam('image', '/images/logo.png');
$locale         = $this->getParam('locale', null);
$runtimes       = $this->getParam('runtimes', null);

if(!empty($platforms)) {
    $platforms = array_map(function($platform) {
        return [
            'key' => $platform['key'],
            'name' => $platform['name'],
            'languages' => array_map(function($language) {
                return [
                    'key' => $language['key'],
                    'name' => $language['name'] . (($language['beta']) ? ' (beta)' : ''),
                ];
            }, array_filter($platform['languages'], function($node) {
                return ($node['enabled']);
            }))
        ];
    }, $platforms);
}

?><!DOCTYPE html><!--
<?php echo $locale->getText('settings.inspire'); ?>

--><html lang="<?php echo $locale->getText('settings.locale'); ?>" class="<?php echo $this->getParam('class', 'none'); ?>  <?php echo $mode; ?>">
<head>
    <link rel="manifest" href="/manifest.json">
    <title><?php echo $this->getParam('title', ''); ?></title>
    <meta name="description" content="<?php echo $this->getParam('description', ''); ?>" />
    <link rel="stylesheet" media="all" type="text/css" href="/dist/styles/default-<?php echo $locale->getText('settings.direction'); ?>.css?v=<?php echo APP_CACHE_BUSTER; ?>.<?php echo $version; ?>" />
    <link rel="icon" type="image/png" href="<?php echo $this->escape($this->getParam('icon', '')); ?>?v=<?php echo APP_CACHE_BUSTER; ?>" />
    <link rel="apple-touch-icon" href="/images/apple.png">
    <!-- <link rel="preconnect" href="" /> -->
    <?php if (!empty($canonical)): ?>
        <link rel="canonical" href="<?php echo $this->escape($canonical); ?>" />
    <?php endif; ?>
    <?php foreach ($this->getParam('prefetch', []) as $prefetch): ?>
        <link rel="prefetch" href="<?php echo $this->escape($prefetch); ?>" />
    <?php endforeach; ?>
    <base href="/">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="theme-color" content="#f02e65">
    <meta property="og:type" content="website" />
    <meta property="og:site_name" content="<?php echo APP_NAME; ?>">
    <meta property="og:title" content="<?php echo $this->escape($this->getParam('title', '')); ?>" />
    <meta property="og:description" content="<?php echo $this->escape($this->getParam('description', '')); ?>" />
    <?php if (!empty($canonical)): ?>
        <meta property="og:url" content="<?php echo $this->escape($canonical); ?>" />
    <?php endif; ?>
    <meta property="og:image"  content="<?php echo $this->escape($endpoint); ?><?php echo $this->escape($image); ?>?v=<?php echo APP_CACHE_BUSTER; ?>" />
    <meta name="twitter:site" content="@<?php echo APP_SOCIAL_TWITTER_HANDLE; ?>">
    <meta name="twitter:title" content="<?php echo $this->escape($this->getParam('title', '')); ?>">
    <meta name="twitter:image:src" content="<?php echo $this->escape($endpoint); ?><?php echo $this->escape($image); ?>?v=<?php echo APP_CACHE_BUSTER; ?>">
    <meta name="twitter:card" content="summary_large_image">
    <script>

        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

        ga('create', '<?php echo $analytics; ?>', 'auto', {});

        var APP_ENV = {
            NAME: '<?php echo APP_NAME; ?>',
            VERSION: '<?php echo $this->escape($version); ?>',
            CACHEBUSTER: '<?php echo $this->escape($version); ?>/<?php echo APP_CACHE_BUSTER; ?>',
            PROTOCOL: '<?php echo $this->escape($protocol); ?>',
            ENDPOINT: '<?php echo $this->escape($endpoint); ?>',
            DOMAIN: '<?php echo $this->escape($domain); ?>',
            HOME: '<?php echo $this->escape($this->getParam('home')); ?>',
            SETUP: '<?php echo $this->escape($this->getParam('setup')); ?>',
            API: '/v1',
            PROJECT: 'console',
            RUNTIMES: <?php echo json_encode($runtimes); ?>,
            PLATFORMS: <?php echo json_encode($platforms); ?>,
            LOCALE: '<?php echo $this->escape($locale->getText('settings.locale')); ?>',
            PREFIX: '<?php echo $this->escape($this->getParam('prefix')); ?>',
            ROLES: <?php echo json_encode($this->getParam('roles', [])); ?>,
            PAGING_LIMIT: <?PHP echo APP_PAGING_LIMIT; ?>
        };

        <?php if ($litespeed): ?>
        document.addEventListener("DOMContentLoaded", function() {
            var head      = document.getElementsByTagName('head')[0];
            var script    = document.createElement('script');

            script.type   = 'text/javascript';
            script.async  = true;
            script.src    = '/dist/scripts/app-all.js?v=<?php echo APP_CACHE_BUSTER; ?>.<?php echo $this->escape($version); ?>';
            script.onload = function() {
                window.ls.run(window);
            };

            head.appendChild(script);
        });
        <?php endif; ?>
    </script>
    <?php echo $this->exec($this->getParam('head', [])); ?>
</head>

<body class="theme-light" data-general-scroll-to data-general-scroll-direction>
    <script>
        let theme = window.localStorage.getItem('user-theme');

        if(theme === 'theme-dark') {
            document.body.classList.remove('theme-light');
            document.body.classList.add('theme-dark');
        }
    </script>
    <?php echo $this->exec($this->getParam('header', [])); ?>

    <main data-ls-router data-first-from-server="true" data-acl data-analytics-pageview>
        <?php echo $this->exec($this->getParam('body', [])); ?>
   </main>

    <div class="loader"></div>

    <div data-cookies="We are using cookies to make this website easier to use."></div>

    <section class="alerts">
        <ul data-ls-loop="alerts.list" data-ls-as="alert">
            <li data-forms-remove>
                <div data-ls-attrs="class={{alert.class}} message">
                    <i class="icon-cancel" data-forms-run="alert.remove"></i>
                    
                    <span data-ls-bind="{{alert.text}}"></span>

                    <span data-ls-if="undefined !== {{alert.link}}">
                        <a data-ls-attrs="href={{alert.link}}" data-ls-bind="{{alert.label}}" target="_blank" data-remove></a>
                    </span>
                </div>
            </li>
        </ul>
    </section>

    <?php echo $this->exec($this->getParam('footer', [])); ?>

    <!-- Version <?php echo $this->escape($version); ?> -->
</body>
</html>
