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

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

class MyPlugIN_VodCategoryListScreen extends AbstractPreloadedRegularScreen
{
    const ID = 'vod_category_list';

	public static function get_media_url_str($category_id, $subcategory_id=false, $name=false)
	{
		$arr['screen_id'] = self::ID;
		$arr['category_id'] = $category_id;
		if ($subcategory_id == true)
			$arr['subcategory_id'] = $subcategory_id;
		if ($name == true)
			$arr['name'] = $name;
		return MediaURL::encode($arr);
	}

    //////////////////////////////////////////////////////////////////////
    private $session;
    private $category_list;
    private $category_index;
    ///////////////////////////////////////////////////////////////////////


    public function __construct($session)
    {
        parent::__construct(
            self::ID, $this->get_folder_views());
	$this->session = $session;
    }


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

    public function get_action_map(MediaURL $media_url, &$plugin_cookies)
    {
        return array(GUI_EVENT_KEY_ENTER => ActionFactory::open_folder(),
        );
    }

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

	public function get_all_folder_items(MediaURL $media_url, &$plugin_cookies)
	{	
		$this->session->get_smart_token($plugin_cookies);
		
		//////////////////FAVORITES
		if (MyPlugIN_Config::VOD_FAVORITES_SUPPORTED && !isset($media_url->category_id))
		{
			$icon_fav = 'plugin_file://icons/vod/fav.png';
			$icon_fav_sel = 'plugin_file://icons/vod/fav_sel.png';
			$items[] = array
			(
			PluginRegularFolderItem::media_url => VodFavoritesScreen::get_media_url_str(),
			PluginRegularFolderItem::caption => 'Избранное',
			PluginRegularFolderItem::view_item_params => array
			(
			ViewItemParams::icon_path => $icon_fav,
			ViewItemParams::icon_sel_path => $icon_fav_sel,
			ViewItemParams::item_detailed_icon_path =>  $icon_fav,
			)
			);
		}
		
		//////////////////SEARCH
		if(!isset($media_url->category_id))
		{
			$icon_search = 'plugin_file://icons/vod/vod_search.png';
			$icon_search_sel = 'plugin_file://icons/vod/vod_search_sel.png';
			$items[] = array
			(
			PluginRegularFolderItem::media_url => VodSearchScreen::get_media_url_str(),
			PluginRegularFolderItem::caption => 'Поиск' ,
			PluginRegularFolderItem::view_item_params => array
			(
			ViewItemParams::icon_path => $icon_search,
			ViewItemParams::icon_sel_path => $icon_search_sel,
			ViewItemParams::item_detailed_icon_path => $icon_search ,
			)
			);
			/////////////////////////////////////////////////////////////////////////////////
			$icon_all_film = 'plugin_file://icons/vod/vod_all.png';
			$icon_all_sel = 'plugin_file://icons/vod/vod_all_sel.png';
			$items[] = array
			(
			PluginRegularFolderItem::media_url => MyPlugIN_VodListScreen::get_media_url_str('all'),
			PluginRegularFolderItem::caption => 'Все фильмы' ,
			PluginRegularFolderItem::view_item_params => array
			(
			ViewItemParams::icon_path => $icon_all_film,
			ViewItemParams::icon_sel_path => $icon_all_sel,
			ViewItemParams::item_detailed_icon_path => $icon_all_film,
			)
			);
		}

////////////////////////////////////
        if (is_null($this->category_index))
            $this->fetch_vod_categories($plugin_cookies);
        $category_list = $this->category_list;   
        if (isset($media_url->category_id))
        {
            if (!isset($this->category_index[$media_url->category_id]))
            {
                hd_print("Error: parent category (id: " .
                    $media_url->category_id . ") not found."); 
                throw new Exception('No parent category found');
            }
            $parent_category = $this->category_index[$media_url->category_id];
            $category_list = $parent_category->get_sub_categories();
        }
        foreach ($category_list as $c)
        {               
		$is_movie_list = is_null($c->get_sub_categories());
		$media_url_str = $is_movie_list ? MyPlugIN_VodListScreen::get_media_url_str($c->get_id(), null) : self::get_media_url_str($c->get_id(), null);
		
		$icon_genres = 'plugin_file://icons/vod/vod_genres.png';
		$icon_genres_sel = 'plugin_file://icons/vod/vod_genres_sel.png';
		$items[] = array
		(
		PluginRegularFolderItem::media_url => $media_url_str,
		PluginRegularFolderItem::caption => $c->get_caption(),
		PluginRegularFolderItem::view_item_params => array
		(
		ViewItemParams::icon_path => $icon_genres,
		ViewItemParams::icon_sel_path => $icon_genres_sel,
		ViewItemParams::item_detailed_icon_path => $icon_genres,
		)
		);
	}
		return $items;
	}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private function fetch_vod_categories($plugin_cookies)
    {
	$url = "http://smart.prostotv.com/smartup/vod/catalog";
	$post_data="platform=sandroid&duid=" . $plugin_cookies->duid . "&lid=ru&token=" . $plugin_cookies->smart_token . "&get=categories";
	$category_list=HD::http_post_document($url, $post_data);
	$category_list = json_decode($category_list);

	foreach ($category_list->categories as $v)
	{
		$v = strval($v);
		$categories[$v] = $v;
	}

	$categories = array_merge(array("Все категории"=>"all_categories"), $categories);

	$this->category_list = array();
	$this->category_index = array();
	$this->fill_categories($categories, $this->category_list, $plugin_cookies);
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	private function fill_categories($categories, &$obj_arr, $plugin_cookies)
	{
		foreach ($categories as $title=>$url)
		{
			$genres=array();
			$cat = new DemoVodCategory(
					strval($url),
					strval($title),
					"plugin_file://icons/folder.png"
					);
			if (!preg_match("|/films/animation|", $url) && !preg_match("|/tv|", $url))
			{
				$AAA = $this->session->smart_get_vod_genres($title, $plugin_cookies);
				foreach ($AAA->genres as $v)
					$genres[$v] = $v;

				$genres = array_merge(array("Все жанры"=>"all_genres"), $genres);
				$gen_arr = array();	
					foreach($genres as $k=>$v)
					{
						$genre =
						new DemoVodCategory(
							strval($url . "/" . $k),
							trim(strval($k)),//жанр
							"plugin_file://icons/folder.png"
							);
						array_push($gen_arr, $genre);
					}
				$cat->set_sub_categories($gen_arr);
			}
			$obj_arr[] = $cat;
			$this->category_index[$cat->get_id()] = $cat;
		}
	}

    ///////////////////////////////////////////////////////////////////////
    public function get_archive(MediaURL $media_url)
    { 
        return $this->session->get_archive();
    }
    ///////////////////////////////////////////////////////////////////////

    private function get_folder_views()
    {
    	return MyPlugIN_Config::GET_VOD_CATEGORY_LIST_FOLDER_VIEWS();
    }
}

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