<?php
/** @var App\Entity\Station $station */

$this->layout('main', [
    'title' => __('Profile'),
    'manual' => true,
    'sidebar_tab' => 'profile',
]);

/** @var \App\Assets $assets */
$assets
    ->load('vue')
    ->load('radio')
    ->load('clipboard')
    ->addInlineJs($this->fetch('stations/profile/index.js', ['nowplaying' => $nowplaying]), 99);
?>

<div class="row">
    <div class="col-sm-6">

        <section class="card" role="region">
            <div class="card-header ch-alt">
                <h2><?=__('On the Air') ?></h2>
            </div>
            <div class="card-body card-padding nowplaying" id="profile-nowplaying">
                <div class="row">

                    <div class="col-sm-4">
                        <div class="m-l-25" style="position: relative;">
                            <div style="position: absolute; top: 0; left: -22px;"><i class="zmdi-hc-lg zmdi zmdi-collection-music"></i></div>

                            <h6><?=__('Now Playing') ?>:</h6>

                            <h4 class="media-heading">
                                {{ np.now_playing.song.title }}
                            </h4>
                            <div>
                                {{ np.now_playing.song.artist }}
                            </div>
                            <div class="nowplaying-progress" v-if="time_display">
                                {{ time_display }}
                            </div>
                        </div>
                    </div>

                    <div class="col-sm-4" v-if="!np.live.is_live && np.playing_next">
                        <div class="m-l-25" style="position: relative;">
                            <div style="position: absolute; top: 0; left: -22px;"><i class="zmdi-hc-lg zmdi zmdi-skip-next"></i></div>

                            <h6><?=__('Playing Next') ?>:</h6>

                            <h4 class="media-heading">
                                {{ np.playing_next.song.title }}
                            </h4>
                            <div>
                                {{ np.playing_next.song.artist }}
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-4" v-if="np.live.is_live">
                        <div class="m-l-25" style="position: relative;">
                            <div style="position: absolute; top: 0; left: -22px;"><i class="zmdi-hc-lg zmdi zmdi-account"></i></div>

                            <h6><?=__('Now Streaming') ?>:</h6>

                            <h4 class="media-heading">
                                {{ np.live.streamer_name }}
                            </h4>
                        </div>
                    </div>

                    <div class="col-sm-3">
                        <div class="m-l-25" style="position: relative;">
                            <div style="position: absolute; top: 0; left: -22px;"><i class="zmdi-hc-lg zmdi zmdi-headset"></i></div>

                            <h6><?=__('Listeners') ?>:</h6>

                            <h4><span>{{ np.listeners.total }}</span> <?=__('Total') ?></h4>
                            <div>
                                <span>{{ np.listeners.unique }}</span> <?=__('Unique') ?>
                            </div>
                        </div>
                    </div>

                </div>

            <?php if ($backend_type == 'liquidsoap' && $acl->userAllowed($user, 'manage station broadcasting', $station->getId())): ?>
                <div class="buttons pull-right p-t-15">
                    <a id="btn_skip_song" class="btn btn-sm btn-primary" role="button" href="<?=$router->fromHere('api:stations:backend', ['do' => 'skip']) ?>" v-if="!np.live.is_live"><i class="zmdi zmdi-skip-next"></i> <?=__('Skip Song') ?></a>
                    <a id="btn_disconnect_streamer" class="btn btn-sm btn-primary" role="button" href="<?=$router->fromHere('api:stations:backend', ['do' => 'disconnect']) ?>" v-if="np.live.is_live"><i class="zmdi zmdi-volume-off"></i> <?=__('Disconnect Streamer') ?></a>
                </div>
                <div class="clearfix"></div>
            <?php endif; ?>
            </div>
        </section>

        <section class="card" role="region">
            <div class="card-header ch-alt">
                <h2><?=__('Profile') ?></h2>

                <?php if ($acl->userAllowed($user, 'manage station profile', $station->getId())): ?>
                    <a class="btn bgm-blue btn-float" role="button" title="<?=__('Edit') ?>" href="<?=$router->fromHere('stations:profile:edit') ?>">
                        <span class="sr-only"><?=__('Edit') ?></span>
                        <i class="zmdi zmdi-edit"></i>
                    </a>
                <?php endif; ?>
            </div>
            <table class="table table-striped table-responsive">
                <colgroup>
                    <col width="30%">
                    <col width="70%">
                </colgroup>
                <tbody>
                <tr>
                    <td><?=__('Name') ?></td>
                    <td><?=$this->e($station->getName()) ?></td>
                </tr>
                <tr>
                    <td><?=__('Description') ?></td>
                    <td><?=$this->e($station->getDescription()) ?></td>
                </tr>
                <?php if ($backend->supportsRequests()): ?>
                    <tr>
                        <td><?=__('Song Requests') ?></td>
                        <td>
                            <?php if ($station->getEnableRequests()): ?>
                                <span class="text-success"><?=__('Enabled') ?></span>

                                <div class="form-field">
                                    <textarea id="request_embed_url" class="full-width form-control text-preformatted" spellcheck="false" style="height: 70px;"><iframe src="<?=$router->named('public:embedrequests', ['station' => $station->getShortName()], [], true) ?>" frameborder="0" allowtransparency="true" style="width: 100%; min-height: 850px; border: 0;"></iframe></textarea>
                                </div>
                                <button class="btn btn-copy btn-link btn-xs" data-clipboard-target="#request_embed_url"><i class="zmdi zmdi-copy"></i> <?=__('Copy to Clipboard') ?></button>
                            <?php else: ?>
                                <span class="text-danger"><?=__('Disabled') ?></span><br><small><?=__('Edit station profile to enable.') ?></small>
                            <?php endif; ?>
                        </td>
                    </tr>
                <? endif; ?>
                <?php if ($backend->supportsStreamers()): ?>
                    <tr>
                        <td><?=__('Streamers/DJs') ?></td>
                        <td>
                            <?php if ($station->getEnableStreamers()): ?>
                                <span class="text-success"><?=__('Enabled') ?></span><br>
                                <a href="<?=$router->fromHere('stations:streamers:index') ?>"><?=__('Manage streamer accounts') ?></a>
                            <?php else: ?>
                                <span class="text-danger"><?=__('Disabled') ?></span><br><small><?=__('Edit station profile to enable.') ?></small>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endif; ?>
                <tr>
                    <td><?=__('Base Directory') ?></td>
                    <td class="text-preformatted"><?=$this->e($station->getRadioBaseDir()) ?></td>
                </tr>
                <tr>
                    <td><?=__('Media Directory') ?></td>
                    <td class="text-preformatted"><?=$this->e($station->getRadioMediaDir()) ?></td>
                </tr>
                <tr>
                    <td><?=__('Player Embed Code') ?></td>
                    <td class="form-field">
                        <textarea id="player_embed_url" class="full-width form-control text-preformatted" spellcheck="false" style="height: 70px;"><iframe src="<?=$router->named('public:embed', ['station' => $station->getShortName()], [], true) ?>" frameborder="0" allowtransparency="true" style="width: 100%; min-height: 150px; border: 0;"></iframe></textarea>
                        <button class="btn btn-copy btn-link btn-xs" data-clipboard-target="#player_embed_url"><i class="zmdi zmdi-copy"></i> <?=__('Copy to Clipboard') ?></button>
                    </td>
                </tr>
                </tbody>
            </table>
        </section>
    </div>
    <div class="col-sm-6">
    <?php if (count($stream_urls['local']) > 0 || count($stream_urls['remote']) > 0): ?>
        <section class="card" role="region">
            <div class="card-header ch-alt">
                <h2><?=__('Streams') ?></h2>

                <ul class="actions">
                    <a href="" data-toggle="dropdown" aria-expanded="false">
                        <i class="zmdi zmdi-download"></i>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-right">
                        <li><a href="<?=$router->fromHere('public:playlist', ['format' => 'pls']) ?>"><?=__('Download %s', 'PLS') ?></a></li>
                        <li><a href="<?=$router->fromHere('public:playlist', ['format' => 'm3u']) ?>"><?=__('Download %s', 'M3U') ?></a></li>
                    </ul>
                </ul>
            </div>
            <table class="table table-condensed table-striped">
                <colgroup>
                    <col width="10%">
                    <col width="90%">
                </colgroup>
            <?php if (count($stream_urls['local']) > 0): ?>
                <thead>
                    <tr>
                        <th colspan="2"><?=__('Local Streams') ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach($stream_urls['local'] as $stream_url): ?>
                    <tr class="input">
                        <td>
                            <a class="btn-audio" href="#" data-url="<?=$this->e($stream_url) ?>">
                                <i class="zmdi zmdi-play"></i>
                            </a>
                        </td>
                        <td>
                            <a href="<?=$this->e($stream_url) ?>"><?=$this->e($stream_url) ?></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            <?php endif; ?>
            <?php if (count($stream_urls['remote']) > 0): ?>
                <thead>
                <tr>
                    <th colspan="2"><?=__('Remote Relays') ?></th>
                </tr>
                </thead>
                <tbody>
                <?php foreach($stream_urls['remote'] as $stream_url): ?>
                    <tr class="input">
                        <td>
                            <a class="btn-audio" href="#" data-url="<?=$this->e($stream_url) ?>">
                                <i class="zmdi zmdi-play"></i>
                            </a>
                        </td>
                        <td>
                            <a href="<?=$this->e($stream_url) ?>"><?=$this->e($stream_url) ?></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            <?php endif; ?>
            </table>
        </section>
    <?php endif; ?>

    <?php if ($frontend_type !== \App\Radio\Adapters::FRONTEND_REMOTE): ?>
        <section class="card" role="region">
            <div class="card-header <?php if ($frontend_is_running): ?>bgm-green<?php else: ?>bgm-red<?php endif; ?> ch-alt">
                <h2>
                    <?php if ($frontend_type === \App\Radio\Adapters::FRONTEND_ICECAST): ?>
                        <?=__('Icecast (Broadcasting Service)') ?>
                    <?php elseif ($frontend_type === \App\Radio\Adapters::FRONTEND_SHOUTCAST): ?>
                        <?=__('SHOUTcast DNAS 2 (Broadcasting Service)') ?>
                    <?php endif; ?>
                    <br><small><?php if ($frontend_is_running): ?><?=__('Running') ?><?php else: ?><?=__('Not Running') ?><?php endif; ?></small>
                </h2>

                <?php if ($acl->userAllowed($user, 'manage station broadcasting', $station->getId())): ?>
                <ul class="actions">
                    <li class="dropdown">
                        <a href="" data-toggle="dropdown" aria-expanded="false">
                            <i class="zmdi zmdi-more-vert"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-right">
                            <li><a class="api-call" href="<?=$router->fromHere('api:stations:frontend', ['do' => 'restart']) ?>"><?=__('Restart Service') ?></a></li>
                            <li><a class="api-call" href="<?=$router->fromHere('api:stations:frontend', ['do' => 'start']) ?>"><?=__('Start Service') ?></a></li>
                            <li><a class="api-call" href="<?=$router->fromHere('api:stations:frontend', ['do' => 'stop']) ?>"><?=__('Stop Service') ?></a></li>
                        </ul>
                    </li>
                </ul>
                <?php endif; ?>
            </div>
            <table class="table table-striped table-responsive">
                <colgroup>
                    <col width="30%">
                    <col width="70%">
                </colgroup>
                <tbody>
                    <tr>
                        <td><?=__('Administration URL') ?></td>
                        <td><a href="<?=$this->e($frontend->getAdminUrl()) ?>"><?=$this->e($frontend->getAdminUrl()) ?></a></td>
                    </tr>
                    <tr>
                        <td><?=__('Administrator Password') ?></td>
                        <td>
                            <span id="frontend_admin_pw"><?=$this->e($frontend_config['admin_pw']) ?></span>
                            <button class="btn btn-copy btn-link btn-xs" data-clipboard-target="#frontend_admin_pw"><i class="zmdi zmdi-copy"></i><span class="sr-only"><?=__('Copy to Clipboard') ?></span></button>
                        </td>
                    </tr>
                    <tr>
                        <td><?=__('Source Password') ?></td>
                        <td>
                            <span id="frontend_source_pw"><?=$this->e($frontend_config['source_pw']) ?></span>
                            <button class="btn btn-copy btn-link btn-xs" data-clipboard-target="#frontend_source_pw"><i class="zmdi zmdi-copy"></i><span class="sr-only"><?=__('Copy to Clipboard') ?></span></button>
                        </td>
                    </tr>
                <?php if ($frontend_type === \App\Radio\Adapters::FRONTEND_ICECAST): ?>
                    <tr>
                        <td><?=__('Relay Password') ?></td>
                        <td>
                            <span id="frontend_relay_pw"><?=$this->e($frontend_config['relay_pw']) ?></span>
                            <button class="btn btn-copy btn-link btn-xs" data-clipboard-target="#frontend_relay_pw"><i class="zmdi zmdi-copy"></i><span class="sr-only"><?=__('Copy to Clipboard') ?></span></button>
                        </td>
                    </tr>
                <?php endif; ?>
                </tbody>
            </table>
        </section>
        <?php endif; ?>

        <?php if ($backend_type == 'none'): ?>
        <section class="card" role="region">
            <div class="card-header bgm-bluegray ch-alt">
                <h2><?=__('AutoDJ Disabled') ?></h2>
            </div>
            <div class="card-body card-padding">
                <p><?=__('AutoDJ has been disabled for this station. No music will automatically be played when a source is not live.') ?></p>
            </div>
        </section>
        <?php else: ?>
        <section class="card" role="region">
            <div class="card-header <?php if ($backend_is_running): ?>bgm-green<?php else: ?>bgm-red<?php endif; ?> ch-alt">
                <h2>
                <?php if ($backend_type === \App\Radio\Adapters::BACKEND_LIQUIDSOAP): ?>
                    <?=__('Liquidsoap (AutoDJ Service)') ?><br>
                <?php endif; ?>
                    <small><?php if ($backend_is_running): ?><?=__('Running') ?><?php else: ?><?=__('Not Running') ?><?php endif; ?></small>
                </h2>

                <?php if ($acl->userAllowed($user, 'manage station broadcasting', $station->getId())): ?>
                <ul class="actions">
                    <li class="dropdown">
                        <a href="" data-toggle="dropdown" aria-expanded="false">
                            <i class="zmdi zmdi-more-vert"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-right">
                            <li><a class="api-call" href="<?=$router->fromHere('api:stations:backend', ['do' => 'restart']) ?>"><?=__('Restart Service') ?></a></li>
                            <li><a class="api-call" href="<?=$router->fromHere('api:stations:backend', ['do' => 'start']) ?>"><?=__('Start Service') ?></a></li>
                            <li><a class="api-call" href="<?=$router->fromHere('api:stations:backend', ['do' => 'stop']) ?>"><?=__('Stop Service') ?></a></li>
                        </ul>
                    </li>
                </ul>
                <?php endif; ?>
            </div>
            <div class="card-body card-padding">
                <p><?=sprintf(__('LiquidSoap is currently shuffling from <b>%d uploaded songs</b> in <b>%d playlists</b>.'), $num_songs, $num_playlists) ?></p>

                <?php if ($acl->userAllowed($user, 'manage station media', $station->getId())): ?>
                    <div class="buttons">
                        <a class="btn btn-primary" href="<?=$router->fromHere('stations:files:index') ?>"><?=__('Music Files') ?></a>
                        <a class="btn btn-primary" href="<?=$router->fromHere('stations:playlists:index') ?>"><?=__('Playlists') ?></a>
                    </div>
                <?php endif; ?>
            </div>
        </section>
        <?php endif; ?>
    </div>
</div>
