<?php

require_once 'lib/vod/vod_list_screen.php';

class I24hVodListScreen extends VodListScreen
{
    private $session;
    private $tv;
    private $amedia;
    
    public static function get_media_url_str($cat_id, $arg = null)
    {
        $arr['screen_id'] = self::ID;
        	$arr['category_id'] = $cat_id;
        	$arr['genre'] = $arg;
        return MediaURL::encode($arr);
    }

    public function __construct(Vod $vod, $session, $tv)
    {
        parent::__construct($vod);
        $this->session = $session;
        $this->tv = $tv;
    }

    protected function get_short_movie_range(MediaURL $media_url, $from_ndx,
        &$plugin_cookies)
    {
        $this->session->ensure_logged_in($plugin_cookies);

        if ($this->session->get_channel_need_reloading()){
            $this->session->set_channel_need_reloading(false);
            $this->tv->unload_channels();
            hd_print("!!!!!!!!! get_short_movie_range !!!!!!!!!");
        }

        $this->tv->ensure_channels_loaded($plugin_cookies);

    	if ($media_url->page_name === 'search') {
    	    $url = sprintf($this->session->get_api_server() . I24hConfig::MOVIE_SEARCH_URL, urlencode($media_url->pattern), $this->session->get_token());
    	    try {
                $doc = $this->session->api_call($url);
            } catch (Exception $e) {
                throw new DuneException(
                    'HTTP network error', 0,
                    ActionFactory::show_error(
                        true,
                        'Ошибка связи',
                        array('Не удаётся подключиться к серверу.', 'Проверьте подключение к сети Интернет.')));
            }

            $movies = array();
            $itemssearch = array();
            foreach ($doc as $search) {

                if (!empty($search->program)) {
                    $prod = $search->program;
                    $movie = $search->program;
                    $frame = '';
                    $poster = '';
                    $cover = '';
                    foreach ($movie->img as $i) {
                        if ($i->type == 'poster') {
                            $poster = strval($i->src);
                        }elseif ($i->type == 'frame'){
                            $frame = strval($i->src);
                        }elseif ($i->type == 'cover'){
                            $cover = strval($i->src);
                        }
                    }
                    if (!empty($cover)){
                        $img = $cover;
                    }elseif (!empty($poster)){
                        $img = $poster;
                    }else{
                        $img = $frame;
                    }
                    $itemssearch[] = new ShortMovie(
                        strval($movie->id),
                        strval($movie->title),
                        $img."?w=320&h=180&crop=true"
                    );
                }
                if (!empty($search->video)) {
                    $movie = $search->video;
                }
            }
            return $itemssearch;
        } else {
            $limit = 20;
            $offset = $from_ndx;
            $count_video = $limit;
            $movies = array();

            $url = sprintf(
                $this->session->get_api_server() . I24hConfig::MOVIE_LIST_URL_FORMAT,
                $media_url->genre,
                $this->session->get_token(),
                $limit,$offset
            );
            try {
                $doc = $this->session->api_call($url);
            } catch (Exception $e) {
                throw new DuneException(
                    'HTTP network error', 0,
                    ActionFactory::show_error(
                        true,
                        'Ошибка связи',
                        array('Не удаётся подключиться к серверу.', 'Проверьте подключение к сети Интернет.')));
            }

            $count_video = 0;
            foreach ($doc as $movie) {
                foreach ($movie->img as $i) {
                    $frame = '';
                    $poster = '';
                    $cover = '';
                    foreach ($movie->img as $i) {
                        if ($i->type == 'poster') {
                            $poster = strval($i->src);
                        }elseif ($i->type == 'frame'){
                            $frame = strval($i->src);
                        }elseif ($i->type == 'cover'){
                            $cover = strval($i->src);
                        }
                    }
                    if (!empty($cover)){
                        $img = $cover;
                    }elseif (!empty($poster)){
                        $img = $poster;
                    }else{
                        $img = $frame;
                    }
                    $movies[] = new ShortMovie(
                        strval($movie->id),
                        strval($movie->title),
                        $img . "?w=320&h=180&crop=true"
                    );
                    $count_video++;
                }
            }
            $offset += $limit;
            $ret = new ShortMovieRange($from_ndx, count($movies), $movies);
            return $ret;
    	}
    }


    protected function get_short_movie_range_old(MediaURL $media_url, $from_ndx,
        &$plugin_cookies)
    {
        $this->session->ensure_logged_in($plugin_cookies);

        if ($this->session->get_channel_need_reloading()){
            $this->session->set_channel_need_reloading(false);
            $this->tv->unload_channels();
        }

        $this->tv->ensure_channels_loaded($plugin_cookies);

    	if ($media_url->page_name === 'search') {

            $url = sprintf($this->session->get_api_server() . I24hConfig::MOVIE_SEARCH_URL, urlencode($media_url->pattern), $this->session->get_token());
            try {
                $doc = $this->session->api_call($url);
            } catch (Exception $e) {
                throw new DuneException(
                    'HTTP network error', 0,
                    ActionFactory::show_error(
                        true,
                        'Ошибка связи',
                        array('Не удаётся подключиться к серверу.', 'Проверьте подключение к сети Интернет.')));
            }

            $movies = array();
            $itemssearch = array();
            foreach ($doc as $search) {

                if (!empty($search->program)) {
                    $prod = $search->program;

                    $movie = $search->program;

                    $frame = '';
                    $poster = '';
                    $cover = '';
                    foreach ($movie->img as $i) {
                        if ($i->type == 'poster') {
                            $poster = strval($i->src);
                        }elseif ($i->type == 'frame'){
                            $frame = strval($i->src);
                        }elseif ($i->type == 'cover'){
                            $cover = strval($i->src);
                        }
                    }
                    if (!empty($cover)){
                        $img = $cover;
                    }elseif (!empty($poster)){
                        $img = $poster;
                    }else{
                        $img = $frame;
                    }
                    $itemssearch[] = new ShortMovie(
                        strval($movie->id),
                        strval($movie->title),
                        $img . "?w=320&h=180&crop=true"
                    );

                }
                if (!empty($search->video)) {
                    $movie = $search->video;
                }
            }
            return $itemssearch;

        } else {

            $limit = 100;
            $offset = 0;
            $count_video = $limit;
            $movies = array();
            while ($count_video == $limit) {
                $url = sprintf(
                    $this->session->get_api_server() . I24hConfig::MOVIE_LIST_URL_FORMAT,
                    $media_url->genre,
                    $this->session->get_token(),
                    $limit,$offset
                );
                try {
                    $doc = $this->session->api_call($url);
                } catch (Exception $e) {
                    throw new DuneException(
                        'HTTP network error', 0,
                        ActionFactory::show_error(
                            true,
                            'Ошибка связи',
                            array('Не удаётся подключиться к серверу.', 'Проверьте подключение к сети Интернет.')));
                }

                $count_video = 0;
                foreach ($doc as $movie) {
                    $frame = '';
                    $poster = '';
                    $cover = '';
                    foreach ($movie->img as $i) {
                        if ($i->type == 'poster') {
                            $poster = strval($i->src);
                        }elseif ($i->type == 'frame'){
                            $frame = strval($i->src);
                        }elseif ($i->type == 'cover'){
                            $cover = strval($i->src);
                        }
                    }
                    if (!empty($cover)){
                        $img = $cover;
                    }elseif (!empty($poster)){
                        $img = $poster;
                    }else{
                        $img = $frame;
                    }
                    $movies[] = new ShortMovie(
                        strval($movie->id),
                        strval($movie->title),
                        $img . "?w=320&h=180&crop=true"
                    );
                    $count_video++;
                }
                $offset += $limit;
            }
            return new ShortMovieRange(0, count($movies), $movies);
        }
    }
}

?>
