<?php

require_once 'lib/vod/vod.php';
require_once 'lib/abstract_preloaded_regular_screen.php';
require_once 'lib/vod/vod_bio_screen.php';

class VodOthetScreen extends AbstractPreloadedRegularScreen implements UserInputHandler
{
	const ID = 'vod_other';

	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 $category_list;
	private $category_index;

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

	public function __construct(Vod $vod)
	{
		$this->vod = $vod;
		parent::__construct(self::ID, $vod->get_vod_list_folder_views(&$plugin_cookies));
	   UserInputHandlerRegistry::get_instance()->register_handler($this);
	}


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

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

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

	public function get_all_folder_items(MediaURL $media_url, &$plugin_cookies)
	{
	$movie2 = $this->vod->get_cached_movie($media_url->category_id);
	$this->vod->folder_entered($media_url, $plugin_cookies);
	foreach ($movie2->other as $k=>$c)
	{
		$zapros=str_replace(" (Режиссёр)", "", $k);
		$artist=urlencode(strval($zapros));
		$url="https://www.kinopoisk.ru/api/suggest/v2/?query=" . $artist;
		$doc=file_get_contents("http://ddaaff.mydune.ru/free_tv/Kinobase.php?$url");		
		$doc=json_decode($doc);
		if (!isset($doc->suggest->top->topResult->global->id)) continue;

		$id_artist=$doc->suggest->top->topResult->global->id;
		$original_name=$doc->suggest->top->topResult->global->originalName;
		$birthday=$doc->suggest->top->topResult->global->birthDate;
		if (isset($doc->suggest->top->topResult->global->poster->avatarsUrl))
		{
			$img=$doc->suggest->top->topResult->global->poster->avatarsUrl;
			$img=$img . "/360?.jpg";
			//$img="http:" . $img . "/360?.jpg";
			//$img="https://st.kp.yandex.net/images/actor_iphone/iphone360_" . $id_artist . ".jpg";
		}
		else
			$img="";		
		$description="$original_name||$birthday";

		//$media_url_str = VodListScreen::get_media_url_str(MEDIA_URL . $c . "?kp_id=$id_artist", null);
		$media_url_str = VodListScreen::get_media_url_str(MEDIA_URL . $c . "&kp_id=$id_artist", null);
					$items[] = array
					(
						PluginRegularFolderItem::media_url => $media_url_str,
						PluginRegularFolderItem::caption => $k,
						PluginRegularFolderItem::view_item_params => array
						(
							ViewItemParams::icon_path => $img,
							ViewItemParams::item_detailed_icon_path => $img,
							ViewItemParams::item_detailed_info => $description
						)
					);
	}
		return $items;
	}

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

	public function get_action_map(MediaURL $media_url, &$plugin_cookies)
	{
		$actions = array();
		$actions[GUI_EVENT_KEY_ENTER] = ActionFactory::open_folder();
		$add_action = UserInputHandlerRegistry::create_action($this, "bio");
		$add_action['caption'] = 'Биография';
		$actions[GUI_EVENT_KEY_B_GREEN] = $add_action;
		return $actions;
	}

	public function handle_user_input(&$user_input, &$plugin_cookies)
	{
		if ($user_input->control_id == "bio")
		{
			$mmm = MediaURL::decode($user_input->selected_media_url);
			$category_id = $mmm->category_id;
			//$pre=explode("?kp_id=", $category_id);
			$pre=explode("&kp_id=", $category_id);
			$kp_id=$pre[1];
			$url = "https://www.kinopoisk.ru/api/person/" . $kp_id . "/";
			return ActionFactory::open_folder(VodBioScreen::get_media_url_str($url, null, "bio"));
		}
		return null;
	}


}