<?php get_template_part('templates/master/head'); ?>
<div class="main">
    <div class="video-container">
        <div class="post-data" data-icon="<?php echo THEME_TEMPLATE_URL . '/assets/front/js/plyr/data/plyr.svg'; ?>" data-postid="<?php echo $post_id; ?>" data-season="<?php echo (!empty($season_id)) ? $season_id : ''; ?>" data-episode="<?php echo (!empty($episode_id)) ? $episode_id : ''; ?>" data-time="<?php echo (!empty($_GET['time']) && is_numeric($_GET['time'])) ? $_GET['time'] : ''; ?>"></div>
        <div class="post-info d" id="post-info">
            <div class="title-wrapper">
                <?php if ($multi_lang == true) : ?>
                    <div class="select-lang">
                        <a href="<?php echo add_query_arg(array('dubbed' => 0)); ?>" class="<?php echo ($my_type == 'sub') ? 'active' : ''; ?>">زبان اصلی</a>
                        <a href="<?php echo add_query_arg(array('dubbed' => 1)); ?>" class="<?php echo ($my_type == 'dub') ? 'active' : ''; ?>">دوبله فارسی</a>
                    </div>
                <?php endif; ?>
            </div>
            <a href="<?php echo $post_link; ?>" class="back" title="بازگشت به صفحه مطلب"><span>بازگشت</span> <i class="fa fa-arrow-left"></i></a>
        </div>

        <?php if ($post_type == 'series') {
            if (!empty($html_link)) {
                $play_link = '';
                if (array_key_exists($my_type, $html_link)) $play_link = $html_link[$my_type];
                else $play_link = $html_link[array_key_first($html_link)];
                echo '<div class="player-embed">';
                echo $play_link;
                echo '</div>';
            } else {
                if (!empty($play_box)) :
                    $s_i = 1;
                    $ep_id = (int) ($episode_id - 1);
                    foreach ($play_box as $season_name => $season) :
                        if (!empty($season_id) && $s_i == $season_id) :
                            if (is_array($season) && array_key_exists($ep_id, $season) && is_array($season[$ep_id])) :
                                $qualities = array();
                                if (array_key_exists($my_type, $season[$ep_id])) $qualities = $season[$ep_id][$my_type];
                                else $qualities = $season[$ep_id][array_key_first($season[$ep_id])]; ?>
                                <video class="video-player" id="video-player" poster="<?php echo (!empty($post_thumb)) ? $post_thumb : ''; ?>" controls width="100" height="100" playsinline <?php echo (!empty($hls_link[$my_type])) ? 'data-hls="on"' : ''; ?>>
                                    <?php if (!empty($qualities)) : ?>
                                        <?php if (!empty($hls_link[$my_type])) : ?>
                                            <source src="<?php echo $hls_link[$my_type]; ?>">
                                            <?php foreach ($qualities as $quality_name => $links) : ?>
                                                <?php if (!empty($links['fasub_link'])) : ?>
                                                    <track label="فارسی" kind="captions" type="text/vtt" srclang="fa" data-src="<?php echo $links['fasub_link']; ?>">
                                                <?php endif; ?>
                                                <?php if (!empty($links['ensub_link'])) : ?>
                                                    <track label="انگلیسی" kind="captions" type="text/vtt" srclang="en" data-src="<?php echo $links['ensub_link']; ?>">
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        <?php else : ?>
                                            <?php foreach ($qualities as $quality_name => $links) : ?>
                                                <source src="<?php echo (!empty($links['play_link'])) ? $links['play_link'] : ''; ?>" size="<?php echo $quality_name; ?>">
                                                <?php if (!empty($links['fasub_link'])) : ?>
                                                    <track label="<?php echo $quality_name . 'p'; ?> - فارسی" kind="captions" type="text/vtt" srclang="fa" data-src="<?php echo $links['fasub_link']; ?>">
                                                <?php endif; ?>
                                                <?php if (!empty($links['ensub_link'])) : ?>
                                                    <track label="<?php echo $quality_name . 'p'; ?> - انگلیسی" kind="captions" type="text/vtt" srclang="en" data-src="<?php echo $links['ensub_link']; ?>">
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </video>
                        <?php
                            endif;
                        endif;
                        $s_i++;
                    endforeach;
                endif;
            }
        } else if ($post_type == 'movies') {
            if (!empty($html_link)) {
                $play_link = '';
                if (array_key_exists($my_type, $html_link)) $play_link = $html_link[$my_type];
                else $play_link = $html_link[array_key_first($html_link)];
                echo '<div class="player-embed">';
                echo $play_link;
                echo '</div>';
            } else {
                if (!empty($play_box) && is_array($play_box)) {
                    $items = array();
                    if (array_key_exists($my_type, $play_box)) $items = $play_box[$my_type];
                    else $items = $play_box[array_key_first($play_box)];
                    if (!empty($items) && is_array($items)) { ?>
                        <video class="video-player" id="video-player" poster="<?php echo (!empty($post_thumb)) ? $post_thumb : ''; ?>" controls width="100" height="100" playsinline <?php echo (!empty($hls_link[$my_type])) ? 'data-hls="on"' : ''; ?>>
                            <?php if (!empty($hls_link[$my_type])) : ?>
                                <source src="<?php echo $hls_link[$my_type]; ?>">
                                <?php foreach ($items as $item) { ?>
                                    <?php if (!empty($item['fasub_link'])) : ?>
                                        <track label="فارسی" kind="captions" type="text/vtt" srclang="fa" data-src="<?php echo $item['fasub_link']; ?>">
                                    <?php endif; ?>
                                    <?php if (!empty($item['ensub_link'])) : ?>
                                        <track label="انگلیسی" kind="captions" type="text/vtt" srclang="en" data-src="<?php echo $item['ensub_link']; ?>">
                                    <?php endif; ?>
                                <?php } ?>
                            <?php else : ?>
                                <?php foreach ($items as $item) { ?>
                                    <source src="<?php echo (!empty($item['play_link'])) ? $item['play_link'] : ''; ?>" size="<?php echo (!empty($item['quality_link'])) ? $item['quality_link'] : ''; ?>">
                                    <?php if (!empty($item['fasub_link'])) : ?>
                                        <track label="<?php echo (!empty($item['quality_link'])) ? $item['quality_link'] : ''; ?> - فارسی" kind="captions" type="text/vtt" srclang="fa" data-src="<?php echo $item['fasub_link']; ?>">
                                    <?php endif; ?>
                                    <?php if (!empty($item['ensub_link'])) : ?>
                                        <track label="<?php echo (!empty($item['quality_link'])) ? $item['quality_link'] : ''; ?> - انگلیسی" kind="captions" type="text/vtt" srclang="en" data-src="<?php echo $item['ensub_link']; ?>">
                                    <?php endif; ?>
                                <?php } ?>
                            <?php endif; ?>
                        </video>
            <?php }
                }
            } ?>

        <?php } ?>
    </div>
</div>
<?php get_template_part('templates/master/foot'); ?>