<?php
namespace Makai;

$router->get('/about', mkiRedirect('/'));
$router->get('/about.html', mkiRedirect('/'));
$router->get('/about.php', mkiRedirect('/'));
$router->get('/index.php', mkiRedirect('/'));
$router->get('/index.html', mkiRedirect('/'));
$router->get('/related.php', mkiRedirect('/'));
$router->get('/related.html', mkiRedirect('/'));
$router->get('/friends', mkiRedirect('/'));
$router->get('/friends.php', mkiRedirect('/'));
$router->get('/friends.html', mkiRedirect('/'));
$router->get('/related', mkiRedirect('/'));
$router->get('/etc.php', mkiRedirect('/'));
$router->get('/etc.html', mkiRedirect('/'));
$router->get('/etcetera', mkiRedirect('/'));
$router->get('/etcetera.html', mkiRedirect('/'));
$router->get('/etcetera.php', mkiRedirect('/'));
$router->get('/misc', mkiRedirect('/'));
$router->get('/misc.html', mkiRedirect('/'));
$router->get('/misc.php', mkiRedirect('/'));
$router->get('/etc', mkiRedirect('/'));

$router->get('/365', mkiRedirect('/?fw365'));

//$router->get('/donate', mkiRedirect('https://paypal.me/flashwave'));
$router->get('/donate', mkiRedirect('/'));

$router->get('/header-bgs.json', function() {
    return json_encode(FM_BGS);
});

$router->get('/now-listening', function() {
    $offset = (int)filter_input(INPUT_GET, 'offset', FILTER_SANITIZE_NUMBER_INT);

    $body = fm_component('header', [
        'title' => 'flash.moe / now listening',
        'do_fullscreen_header' => true,
        'is_now_playing' => true,
        'offset' => $offset,
    ]);

    $body .= fm_component('footer', [
        'hide' => true,
        'onload' => [
            ['fm.initIndex', 10],
        ],
    ]);

    return $body;
});

$router->get('/now-listening.json', function() {
    $lfmInfo = cache_output('lastfm', 10, function() {
        return json_decode(file_get_contents('https://now.flash.moe/get.php?u=flashwave_'));
    });

    if(empty($lfmInfo[0]->name))
        return [];

    $lfmInfo = $lfmInfo[0];

    return [
        'name' => strval($lfmInfo->name),
        'now_playing' => !empty($lfmInfo->nowplaying),
        'url' => strval($lfmInfo->url),
        'cover' => !empty($lfmInfo->images->large) ? strval($lfmInfo->images->large) : '',
        'artist' => [
            'name' => !empty($lfmInfo->artist->name) ? strval($lfmInfo->artist->name) : '',
            'url' => explode('/_/', strval($lfmInfo->url))[0],
        ],
    ];
});

$router->get('/np.php', function() {
    header('Content-Type: text/xml');
    return cache_output('lastfm-xml', 10, function() {
        return file_get_contents('https://now.flash.moe/get.php?u=flashwave_&f=xml');
    });
});

$router->get('/home', function() {
    $body = fm_component('header', [
        'title' => 'flash.moe / homepage',
        'do_fullscreen_header' => true,
    ]);

    $body .= <<<HTML
            <div class="php">
                <div class="php-time">
                    <div class="php-time-analog">
                        <div class="clock">
                            <div class="clock-background"></div>
                            <div class="clock-center"></div>
                            <div class="clock-hand clock-hand-hours"><div class="clock-hand-display"></div></div>
                            <div class="clock-hand clock-hand-minutes"><div class="clock-hand-display"></div></div>
                            <div class="clock-hand clock-hand-seconds"><div class="clock-hand-display"></div></div>
                            <div class="clock-number clock-number-3"><div class="clock-number-display"></div></div>
                            <div class="clock-number clock-number-6"><div class="clock-number-display"></div></div>
                            <div class="clock-number clock-number-9"><div class="clock-number-display"></div></div>
                            <div class="clock-number clock-number-12"><div class="clock-number-display"></div></div>
                        </div>
                    </div>
                    <div class="php-time-alter">
                        <div class="php-time-digital">
                            <div class="php-time-digital-hours">--</div>
                            <div class="php-time-digital-separator">:</div>
                            <div class="php-time-digital-minutes">--</div>
                        </div>
                        <div class="php-time-date">
                            <span class="php-date-label">Week</span>
                            <span class="php-date-week">--</span>
                            <span class="php-date-label">&nbsp;&mdash;&nbsp;</span>
                            <span class="php-date-year">----</span><span class="php-date-label">-</span><span class="php-date-month">---</span><span class="php-date-label">-</span><span class="php-date-day">--</span>
                        </div>
                    </div>
                </div>
                <form class="php-search" method="get" action="https://duckduckgo.com/">
                    <div class="php-search-input">
                        <input type="search" name="q" placeholder="Search using DuckDuckGo..."/>
                    </div>
                </form>
            </div>
HTML;

    $body .= fm_component('footer', [
        'hide' => true,
        'skip_analytics' => true,
        'onload' => [
            ['fm.initClock'],
            ['fm.initIndex', 10],
        ],
    ]);

    return $body;
});

$router->get('/', function() use ($db) {
    $legacyPage = (string)filter_input(INPUT_GET, 'p');
    if(!empty($legacyPage)) {
        $legacyPages = [
            'projects' => '/projects',
            'contact'  => '/contact',
            'about'    => '/',
            'etc'      => '/etc',
            'hosted'   => '/etc',
            'friends'  => '/related',
        ];

        if(isset($legacyPages[$legacyPage])) {
            header('Location: ' . $legacyPages[$legacyPage]);
            return 302;
        }
    }

    $is365 = isset($_GET['fw365']);

    $blogInfo = json_decode(file_get_contents(MKI_DIR_PUB . '/old_blog_posts.json'));

    $projects = (new Projects($db))->getFeatured();
    $languages = new Languages($db);

    $contact = [
        [
            'id' => 'email',
            'name' => 'E-mail',
            'icon' => 'fmi fmi-email',
            'display' => 'contact@flash.moe',
            'link' => 'mailto:contact@flash.moe',
        ],
        [
            'id' => 'flashii',
            'name' => 'Flashii',
            'icon' => 'fmi fmi-flashii',
            'display' => 'flash',
            'link' => '//flashii.net/profile.php?u=1',
        ],
        [
            'id' => 'twitter',
            'name' => 'Twitter',
            'icon' => 'fmi fmi-twitter',
            'display' => '@smugwave',
            'link' => '//twitter.com/smugwave',
        ],
    ];

    $body = fm_component('header', [
        'title' => 'flash.moe',
        'is_index' => true,
        'is_365' => $is365,
    ]);

    $body .= '<div class="index-menu">';

    for($i = 1; $i < count(FM_NAV); ++$i) {
        $link = FM_NAV[$i];
        $body .= "<a href=\"{$link['link']}\"";

        if($link['link'][0] === '/' && substr($link['link'], 0, 2) !== '//')
            $body .= ' data-fm-dynload=""';

        $body .= ">{$link['title']}</a>";
    }

    $body .= <<<HTML
    </div>
    <div class="index-featured">
HTML;

    $body .= <<<HTML
    <div class="index-feature">
        <div class="index-feature-header">
            <a href="/projects" class="index-feature-header-link" data-fm-dynload=""></a>
            <div class="index-feature-header-title">Projects</div>
            <div class="index-feature-header-more">More</div>
        </div>
HTML;

    foreach($projects as $project) {
        $links = [];
        if($project->hasHomePageUrl())
            $links[] = ['class' => 'homepage', 'text' => 'Homepage', 'url' => $project->getHomePageUrl()];
        if($project->hasSourceUrl())
            $links[] = ['class' => 'repository', 'text' => 'Source', 'url' => $project->getSourceUrl()];
        if($project->hasDiscussionUrl())
            $links[] = ['class' => 'forum', 'text' => 'Discussion', 'url' => $project->getDiscussionUrl()];

        $colour = $project->hasColour() ? $project->getColour() : $languages->getProjectColour($project);
        $colour = str_pad(dechex($colour), 6, '0', STR_PAD_LEFT);

        $body .= <<<HTML
        <div class="index-project" style="background-color: #{$colour};">
            <a href="/projects#{$project->getCleanName()}" class="index-project-anchor" data-fm-dynload=""></a>
            <div class="index-project-content">
                <div class="index-project-name">{$project->getName()}</div>
HTML;

        if($project->hasSummary())
            $body .= "<div class=\"index-project-summary\">{$project->getSummary()}</div>";

        $body .= '</div>';

        if(!empty($links)) {
            $body .= '<div class="index-project-links">';

            foreach($links as $link)
                $body .= "<a class=\"index-project-link index-project-link-{$link['class']}\" href=\"{$link['url']}\" rel=\"noopener\" target=\"_blank\">{$link['text']}</a>";

            $body .= '</div>';
        }

        $body .= '</div>';
    }

    $body .= <<<HTML
    </div>
    <div class="index-feature">
        <div class="index-feature-header">
            <a href="/contact" class="index-feature-header-link" data-fm-dynload=""></a>
            <div class="index-feature-header-title">Contact</div>
            <div class="index-feature-header-more">More</div>
        </div>
        <div class="index-contact">
HTML;

    foreach($contact as $social) {
        $body .= "<div class=\"social social-{$social['id']}\">";

        if(isset($social['link'])) {
            $body .= "<a href=\"{$social['link']}\" class=\"social-background\" target=\"_blank\" rel=\"noopener\"></a>";
        } else {
            $body .= "<div class=\"social-background\" onclick=\"fm.selectTextInElement(this.parentNode.querySelector('.social-handle')); fm.copySelectedText();\"></div>";
        }

        $body .= <<<HTML
        <div class="social-icon {$social['icon']}"></div>
        <div class="social-content">
            <div class="social-name">{$social['name']}</div>
            <div class="social-handle">{$social['display']}</div>
        </div>
    </div>
HTML;
    }

    $body .= '</div></div></div>';

    $body .= fm_component('footer', [
        'is_index' => true,
        'onload' => [
            ['fm.initIndex'],
        ],
    ]);

    return $body;
});