<?php
///////////////////////////////////////////////////////////////////////////

require_once 'lib/vod/vod_list_screen.php';

class MyPlugIN_VodListScreen extends VodListScreen
{
    private $session;
    public static function get_media_url_str($cat_id, $text = null)
    {
	$arr['screen_id'] = self::ID;
	$arr['category_id'] = $cat_id;
	if($cat_id === 'search')
		$arr['pattern'] = $text;
	else if ($cat_id === 'genres')
		$arr['genre_id'] = $text;
	return MediaURL::encode($arr);
    }

    ///////////////////////////////////////////////////////////////////////

    public function __construct($session, Vod $vod)
    {
        parent::__construct($vod);
		$this->session = $session;
		$this->vod = $vod;
		$this->total = array();
    }
    public function get_action_map(MediaURL $media_url, &$plugin_cookies)
    {
        $actions = array();

        if ($this->vod->is_movie_page_supported())
            $actions[GUI_EVENT_KEY_ENTER] = ActionFactory::open_folder();
        else
            $actions[GUI_EVENT_KEY_ENTER] = ActionFactory::vod_play();

        if ($this->vod->is_favorites_supported())
        {
            $add_favorite_action = UserInputHandlerRegistry::create_action($this, 'add_favorite');
            $add_favorite_action['caption'] = 'В избранное';
            $actions[GUI_EVENT_KEY_D_BLUE] = $add_favorite_action;
        }
        return $actions;
    }
    ///////////////////////////////////////////////////////////////////////
	public function handle_user_input(&$user_input, &$plugin_cookies)
	{
		if ($user_input->control_id == 'add_favorite')
		{
			if (!isset($user_input->selected_media_url))
				return null;		
			$media_url = MediaURL::decode($user_input->selected_media_url);
			$movie_id = $media_url->movie_id;		
			$is_favorite = $this->vod->is_favorite_movie_id($movie_id);
			if ($is_favorite)
			{
				return ActionFactory::show_title_dialog('Фильм уже в избранном');
			}
			else
			{
				$this->vod->add_favorite_movie($movie_id, $plugin_cookies);
				return ActionFactory::show_title_dialog('Фильм был добавлен в избранное');
			}
		}		
		return null;
	}

	protected function get_short_movie_range(MediaURL $media_url, $from_ndx, &$add_list, &$plugin_cookies)
	{
		$add_list += 1;
		if($media_url->category_id === 'search')
			$data = $this->session->get_vod_list('search',$media_url->pattern,$add_list,&$plugin_cookies);
		elseif($media_url->category_id === 'all')
			$data = $this->session->get_vod_list('all', 0, $add_list, &$plugin_cookies);
		elseif($media_url->category_id === 'genres')
			$data = $this->session->get_vod_list('genres',$media_url->genre_id,$add_list,&$plugin_cookies);
		else
			$data = $this->session->get_vod_list('gggggg',$media_url->category_id,$add_list,&$plugin_cookies);
			
//echo("data.............................." . print_r($data,true));
			
		$movies = array();
		if(!count($data))
			return new ShortMovieRange(0, 0);	
		if (isset($data->videos))
			$DDD="videos";
		if (isset($data->search))
			$DDD="search";

		foreach ($data->$DDD as $mov)
		{
		
			if ($mov->censored == 1 && $media_url->category_id === 'search')
			continue;
		
			if (isset($mov->cover))
			{			
				if ($mov->censored == 1)
				{
					$poster = "plugin_file://icons/mov_censored.png";
				}
				else
				{			
					$bild = $mov->cover;
					$bild  = substr($bild, 1);				
					$poster = "http://smart.prostotv.com/smartup" . $bild;
				}
			}
			else
				$poster = "plugin_file://icons/mov_unset.png";				
			$idy = intval($mov->id);			
			$movie = 
				new ShortMovie(
				$idy,
				strval($mov->name),
				$poster
				);
			$str1 = isset($mov->year)? "|Год: " . $mov->year : "";
			$str2 = isset($mov->genres)? "|Жанр: " . $mov->genres : "";
			$str3 = isset($mov->rating_kinopoisk)? "|КП: " . $mov->rating_kinopoisk : "";
			$str4 = isset($mov->rating_imdb)? "  IMDb: " . $mov->rating_imdb : "";			
			$str5 = isset($mov->category)? "|Категория: " . $mov->category : "";
			$str6 = isset($mov->rating)? "|Рейтинг: " . $mov->rating : "";
			
			$movie->info = strval($mov->name) . $str1 . $str2 . $str3 . $str4 . $str5 . $str6;
			$movies[] = $movie; 
		}
		if (!array_key_exists($media_url->category_id, $this->total))
			$this->total[$media_url->category_id] = 0;
		$this->total[$media_url->category_id] += count($movies);
		return new ShortMovieRange($from_ndx, $this->total[$media_url->category_id], $movies);		
	}

/////////////////////////////////////////////////////////////////////////////

    public function get_folder_range(MediaURL $media_url, $from_ndx, &$plugin_cookies)
    {
        $movie_range = $this->get_short_movie_range($media_url, $from_ndx, $this->vod->add_list, $plugin_cookies);
        $total = intval($movie_range->total);
        if ($total <= 0)
            return HD::create_regular_folder_range(array());

        $items = array();
        foreach ($movie_range->short_movies as $movie)
        {
            $items[] = array
            (
                PluginRegularFolderItem::media_url => VodMovieScreen::get_media_url_str($movie->id, $movie->name, $movie->poster_url, $movie->info),
                PluginRegularFolderItem::caption => $movie->name,
                PluginRegularFolderItem::view_item_params => array
                (
                    ViewItemParams::icon_path => $movie->poster_url,
		    ViewItemParams::item_detailed_info => $movie->info
                )
            );
            $this->vod->set_cached_short_movie(new ShortMovie($movie->id, $movie->name, $movie->poster_url));
        }
        return HD::create_regular_folder_range($items, $movie_range->from_ndx, $total, true);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	public function get_folder_view(MediaURL $media_url, &$plugin_cookies)
    {
       $this->vod->add_list = 0;
       return parent::get_folder_view($media_url, $plugin_cookies);
    }
	
    public function get_archive(MediaURL $media_url)
    { 
        return $this->session->get_archive();
    }
}

///////////////////////////////////////////////////////////////////////////
?>
