<?php

require_once 'lib/abstract_preloaded_regular_screen.php';
require_once 'lib/tv/tv_group_list_screen.php';

class I24hTopScreen extends AbstractPreloadedRegularScreen implements UserInputHandler
{
    const ID = 'top_list';
    private $amedia;
    private $vod;
    private $session;
    private $cached_movies;

    public function __construct($session, $amedia, $vod)
    {
    	$this->session = $session;
        $this->amedia = $amedia;
        $this->vod = $vod;
        $this->cached_movies = array();
        
    	parent::__construct(
            self::ID,
            array
            (
                array
                (
                    PluginRegularFolderView::async_icon_loading => false,

                    PluginRegularFolderView::view_params => array
                    (
                        ViewParams::num_cols => 5,
                        ViewParams::num_rows => 4,
                        ViewParams::paint_details => false,
                        ViewParams::paint_sandwich => true,
                        ViewParams::sandwich_base => 'gui_skin://special_icons/sandwich_base.aai',
                        ViewParams::sandwich_mask => 'cut_icon://{name=sandwich_mask}',
                        ViewParams::sandwich_cover => 'cut_icon://{name=sandwich_cover}',
                        ViewParams::sandwich_width => 320,
                        ViewParams::sandwich_height => 168,
                        ViewParams::sandwich_icon_upscale_enabled => true,
                        ViewParams::sandwich_icon_keep_aspect_ratio => false,
                        ViewParams::background_path => I24hConfig::BG_PIC,
                        ViewParams::background_width => 1920,
                        ViewParams::background_height => 1080,
                        ViewParams::background_order => 'before_all'

                    ),

                    PluginRegularFolderView::base_view_item_params => array
                    (
                        ViewItemParams::item_paint_icon => true,
                        ViewItemParams::item_layout => HALIGN_CENTER,
                        ViewItemParams::icon_valign => VALIGN_CENTER,
                        ViewItemParams::item_caption_font_size => FONT_SIZE_NORMAL,
                        ViewItemParams::item_caption_width => 1000,
                        ViewItemParams::icon_width => 320,
                        ViewItemParams::icon_height => 168,
                        ViewItemParams::item_paint_caption => false,
                    	ViewItemParams::icon_path => 'plugin_file://icons/mov_unset.png',
                    	ViewItemParams::item_detailed_icon_path => 'missing://',

                    ),

                    PluginRegularFolderView::not_loaded_view_item_params => array (),
                )
            ));
        
        UserInputHandlerRegistry::get_instance()->register_handler($this);

    }

    public function get_handler_id()
    { return self::ID; }


    public function get_action_map(MediaURL $media_url, &$plugin_cookies)
    {
        return I24HSettings::get_top_action_map($this);
    }

    public function get_all_folder_items(MediaURL $media_url, &$plugin_cookies)
    {
        return I24HSettings::get_top_menu($this,$plugin_cookies,$this->amedia);
    }
    
    public function load_movie($movie_id)
    {

    	$this->session->ensure_logged_in($plugin_cookies);
    	
    	if (isset($this->cached_movies[$movie_id])) {
    		return $this->cached_movies[$movie_id];
    	}
    	
        $doc =
            HD::http_get_document(
                sprintf(
                    $this->session->get_api_server() . I24hConfig::MOVIE_INFO_URL_FORMAT,
                    $movie_id, $this->session->get_token()));
     
        if (is_null($doc))
            throw new Exception('Can not fetch movie info');
#        hd_print("XML-text:\n$doc\n");
        $xml = simplexml_load_string($doc);

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

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

        $poster = "";
        $frame = "";
        if (!empty($xml->img)) {

            foreach ($xml->img->children() as $s) {
                if (strval($s->type) == "poster") {
                    $poster = strval($s->src);
                    break;
                }
            }

            foreach ($xml->img->children() as $s) {
                if (strval($s->type) == "frame") {
                    $frame = strval($s->src);
                    break;
                }
            }
        }
        $movie = array(
        	'id' => intval($xml->id),
        	'name' =>  strval($xml->title),
        	'poster' =>  !empty($poster)?$poster:$frame."?w=320&h=200&crop=true");
        
        $this->cached_movies[$movie_id] = $movie;
        return $movie;
    	
    }

    public function handle_user_input(&$user_input, &$plugin_cookies)
    {
    	$media_url = MediaURL::decode($user_input->selected_media_url);
    	
    	if (isset($media_url->channel_id)) {

//    	    hd_print(print_r($media_url,true));
//    	    hd_print("PLAY Channel id ".$media_url->channel_id);

    		return ActionFactory::tv_play();
    	} else {

            if ($user_input->control_id == 'goto_settings'){
                return ActionFactory::open_folder(I24hSetupScreen::get_media_url_str(''),'Личный кабинет');
            }else {
                return ActionFactory::open_folder($user_input->selected_media_url);
            }
    	}





    }
    
    
}

?>

