<?php
global $wp_query;
$multi_lang = false;
$post_id = (!empty($wp_query->get('post_id'))) ? $wp_query->get('post_id') : '';
$post_type = (!empty($post_id)) ? get_post_type($post_id) : '';
if ($post_type == 'series') {
    $season_id = (!empty($wp_query->get('season'))) ? $wp_query->get('season') : 1;
    $episode_id = (!empty($wp_query->get('episode'))) ? $wp_query->get('episode') : 1;
}
$html_link = array();
$hls_link = array();
$dubbed_arg = (!empty($_GET['dubbed'])) ? $_GET['dubbed'] : 0;
$my_type = (!empty($dubbed_arg)) ? 'dub' : 'sub';
$current_time = (!empty($_GET['time']) && is_numeric($_GET['time'])) ? $_GET['time'] : 0;
$post_link = (!empty($post_id)) ? get_the_permalink($post_id) : '';
$post_thumb = get_post_meta($post_id, 'movie_thumb_bg', true);
$post_thumb = (!empty($post_thumb)) ? wp_get_attachment_image_url($post_thumb, 'full') : '';
if (empty($post_thumb)) $post_thumb = get_the_post_thumbnail_url($post_id);
$post_title = get_post_meta($post_id, 'title_movie', true);
$post_title = (!empty($post_title)) ? $post_title : get_the_title($post_id);
$post_year = get_post_meta($post_id, 'release_movie', true);
if (!empty($post_year)) $post_title = $post_title . ' ' . $post_year;
$disabled_play = get_post_meta($post_id, 'deactive_playonline', true);
if (empty($disabled_play)) {
    if ($post_type == 'movies') {
        $new_playbox = array();
        $play_box = get_post_meta($post_id, 'movies_playlinks', true);
        $hassub = false;
        $hasdub = false;
        if (!empty($play_box) && is_array($play_box)) {
            foreach ($play_box as $item) {
                $type_name = (!empty($item['type_link'])) ? $item['type_link'] : '';
                $play_link = (!empty($item['play_link'])) ? $item['play_link'] : '';
                if (isHtml($play_link)) $html_link[$type_name] = $play_link;
                else if (get_file_extension($play_link) == 'm3u8') $hls_link[$type_name] = $play_link;
                if ($type_name == 'sub') $hassub = true;
                if ($type_name == 'dub') $hasdub = true;
                $new_playbox[$type_name][] = $item;
            }
        }
        $multi_lang = ($hassub == true && $hasdub == true) ? true : false;
        $play_box = $new_playbox;
    } elseif ($post_type == 'series') {
        $new_playbox = array();
        $play_box = get_post_meta($post_id, 'series_playlinks', true);
        if (!empty($play_box) && is_array($play_box)) {
            $s_id = 1;
            foreach ($play_box as $key => $season) {
                $e_id = 1;
                $items = (!empty($season['items']) && is_array($season['items'])) ? $season['items'] : array();
                $season_name = (!empty($season['season_name'])) ? $season['season_name'] : $key + 1;
                $quality_name = (!empty($season['quality_link'])) ? $season['quality_link'] : 0;
                $type_name = (!empty($season['type_link'])) ? $season['type_link'] : '';
                foreach ($items as $key => $item) {
                    $new_playbox[$season_name][$key][$type_name][$quality_name] = $item;
                    $e_id++;
                }
                $s_id++;
            }
        }
        $play_box = $new_playbox;
        $s_i = 1;
        foreach ($play_box as $season) {
            if ($s_i == $season_id) {
                $ep_id = (int) ($episode_id - 1);
                if (is_array($season) && array_key_exists($ep_id, $season) && is_array($season[$ep_id])) {
                    foreach ($season[$ep_id] as $type_name => $qualities) {
                        if (!empty($qualities) && is_array($qualities)) {
                            foreach ($qualities as $item) {
                                $play_link = (!empty($item['play_link'])) ? $item['play_link'] : '';
                                if (isHtml($play_link)) $html_link[$type_name] = $play_link;
                                else if (get_file_extension($play_link) == 'm3u8') $hls_link[$type_name] = $play_link;
                            }
                        }
                    }
                    $count_lang = count($season[$ep_id]);
                    $multi_lang = ($count_lang > 1) ? true : false;
                    break;
                }
            }
            $s_i++;
        }
    }
}
if ($post_type != 'movies' && $post_type != 'series') wp_redirect(SITE_URL) and exit;
if (!empty($disabled_play) || empty($play_box)) wp_redirect($post_link) and exit;
if (yascode_vip::limit_user_access_vip($post_type, 'play')) {
    include THEME_TEMPLATE_URL_PATH . '/templates/pages/play/player.php';
} else { ?>
    <?php get_template_part('templates/master/head'); ?>
    <div class="forced-to-access">
        <?php if (!is_user_logged_in()) { ?>
            <p class="desc">برای پخش آنلاین باید در سایت عضو شوید</p>
            <div class="btns">
                <a href="<?php echo LOGIN_URL; ?>" target="_blank" class="btn login">ورود به حساب</a>
                <a href="<?php echo REGISTER_URL; ?>" target="_blank" class="btn register">عضویت</a>
            </div>
        <?php } else { ?>
            <p class="desc">برای پخش آنلاین باید اشتراک تهیه نمایید</p>
            <div class="btns">
                <a href="<?php echo PANEL_URL . '/plans'; ?>" target="_blank" class="btn register">خرید اشتراک</a>
            </div>
        <?php } ?>
    </div>
    <?php get_template_part('templates/master/foot'); ?>
<?php }
