<?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') {
    	    $doc = HD::http_get_document(
                sprintf(
                    $this->session->get_api_server() . I24hConfig::MOVIE_SEARCH_URL,
                    urlencode($media_url->pattern),
                    $this->session->get_token()
                    ));

            if (is_null($doc))
                throw new Exception('Can not fetch movie list');

            $xml = simplexml_load_string($doc);

            if ($xml === false)
            {
                hd_print("Error: can not parse XML document.");
                hd_print("XML-text: $doc.");
                throw new Exception('Illegal XML document');
            }

            if ($xml->getName() !== 'root')
            {
                hd_print("Error: unexpected node '" . $xml->getName() . "'. Expected: 'vod_categories'");
                throw new Exception('Invalid XML document');
            }

            $movies = array();
            $itemssearch = array();
            foreach ($xml->children() as $search) {

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

                    $movie = $search->program;

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

                }
                if (!empty($search->video)) {
                    $movie = $search->video;
                }
            }
            return $itemssearch;
        } else {
            $limit = 20;
            $offset = $from_ndx;
            $count_video = $limit;
            $movies = array();

            $doc = HD::http_get_document(
                sprintf(
                    $this->session->get_api_server() . I24hConfig::MOVIE_LIST_URL_FORMAT,
                    $media_url->genre,
                    $this->session->get_token(),
                    $limit,$offset
                ));
            if (is_null($doc))
                throw new Exception('Can not fetch movie list');

            $xml = simplexml_load_string($doc);

            if ($xml === false) {
                hd_print("Error: can not parse XML document.");
                hd_print("XML-text: $doc.");
                throw new Exception('Illegal XML document');
            }

            if ($xml->getName() !== 'root') {
                hd_print("Error: unexpected node '" . $xml->getName() . "'. Expected: 'vod_categories'");
                throw new Exception('Invalid XML document');
            }

            $count_video = 0;
            foreach ($xml->children() as $movie) {
                $img = '';
                foreach ($movie->img->children() as $i) {
                    if ($i->type == 'poster') {
                        $img = strval($i->src);
                        break;
                    }
                }
                $frame = '';
                foreach ($movie->img->children() as $i) {
                    if ($i->type == 'frame') {
                        $frame = strval($i->src);
                        break;
                    }
                }

                $img = !empty($img) ? $img : $frame;
                $movies[] = new ShortMovie(
                    strval($movie->id),
                    strval($movie->title),
                    $img . "?w=320&h=200&crop=true"
                );
                $count_video++;
            }
            $offset += $limit;
            $ret = new ShortMovieRange($from_ndx, count($movies), $movies);
//            hd_print("ShortMovieRange ".print_r($ret,true));
            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') {
    	    $doc = HD::http_get_document(
                sprintf(
                    $this->session->get_api_server() . I24hConfig::MOVIE_SEARCH_URL,
                    urlencode($media_url->pattern),
                    $this->session->get_token()
                    ));

            if (is_null($doc))
                throw new Exception('Can not fetch movie list');

            $xml = simplexml_load_string($doc);

            if ($xml === false)
            {
                hd_print("Error: can not parse XML document.");
                hd_print("XML-text: $doc.");
                throw new Exception('Illegal XML document');
            }

            if ($xml->getName() !== 'root')
            {
                hd_print("Error: unexpected node '" . $xml->getName() . "'. Expected: 'vod_categories'");
                throw new Exception('Invalid XML document');
            }

            $movies = array();
            $itemssearch = array();
            foreach ($xml->children() as $search) {


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

                    $movie = $search->program;

                    $img = '';
                    foreach ($movie->img->children() as $i) {
                        if ($i->type == 'poster') {
                            $img = strval($i->src);
                            break;
                        }
                    }
                    $frame = '';
                    foreach ($movie->img->children() as $i) {
                        if ($i->type == 'frame') {
                            $frame = strval($i->src);
                            break;
                        }
                    }
                    $itemssearch[] = new ShortMovie(
                        strval($movie->id),
                        strval($movie->title),
                        !empty($img) ? strval($img) : strval($frame) . "?w=320&h=200&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) {
//                hd_print("Iteration offset = ".$offset." , limit = ".$limit);
                $doc = HD::http_get_document(
                    sprintf(
                        $this->session->get_api_server() . I24hConfig::MOVIE_LIST_URL_FORMAT,
                        $media_url->genre,
                        $this->session->get_token(),
                        $limit,$offset
                    ));

                if (is_null($doc))
                    throw new Exception('Can not fetch movie list');

                $xml = simplexml_load_string($doc);

                if ($xml === false) {
                    hd_print("Error: can not parse XML document.");
                    hd_print("XML-text: $doc.");
                    throw new Exception('Illegal XML document');
                }

                if ($xml->getName() !== 'root') {
                    hd_print("Error: unexpected node '" . $xml->getName() . "'. Expected: 'vod_categories'");
                    throw new Exception('Invalid XML document');
                }

                $count_video = 0;
                foreach ($xml->children() as $movie) {
                    $img = '';
                    foreach ($movie->img->children() as $i) {
                        if ($i->type == 'poster') {
                            $img = strval($i->src);
                            break;
                        }
                    }
                    $frame = '';
                    foreach ($movie->img->children() as $i) {
                        if ($i->type == 'frame') {
                            $frame = strval($i->src);
                            break;
                        }
                    }

                    $img = !empty($img) ? $img : $frame;
                    $movies[] = new ShortMovie(
                        strval($movie->id),
                        strval($movie->title),
                        $img . "?w=320&h=200&crop=true"
                    );
                    $count_video++;
                }
                $offset += $limit;
            }
            return new ShortMovieRange(0, count($movies), $movies);
        }
    }
}

?>
