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

require_once 'lib/screen.php';
require_once 'lib/vod/vod.php';
require_once 'lib/vod/vod_series_list_screen.php';
require_once 'lib/vod/vod_other_screen.php';

class VodMovieScreen implements Screen, UserInputHandler
{
	const ID = 'vod_movie';

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

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

	private $vod;

	public function __construct(Vod $vod)
	{
		$this->vod = $vod;
		UserInputHandlerRegistry::get_instance()->register_handler($this);
	}

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

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

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

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

	public function get_folder_view(MediaURL $media_url, &$plugin_cookies)
	{
		$this->vod->folder_entered($media_url, $plugin_cookies);
		$movie = $this->vod->get_loaded_movie($media_url->movie_id, $plugin_cookies);
		$this->vod->folder_movie = $media_url->get_raw_string();
		if ($movie === null)
			return null;

		if (!isset($movie->season_list))
			$mUrl = VodSeriesListScreen::get_media_url_str($movie->id);
		else
			$mUrl = VodSeasonListScreen::get_media_url_str($movie->id);

		$movie_folder_view = array
		(
		PluginMovieFolderView::movie => $movie->get_movie_array(),
		PluginMovieFolderView::has_right_button => true,
		PluginMovieFolderView::right_button_caption => "Режиссёры и актёры",
		PluginMovieFolderView::right_button_action => UserInputHandlerRegistry::create_action($this, "button_actions", array('movie_id' => $movie->id)),
		PluginMovieFolderView::has_multiple_series => true,
		PluginMovieFolderView::series_media_url => $mUrl,
		PluginMovieFolderView::params => array
				(
					PluginFolderViewParams::paint_path_box =>false,
					PluginFolderViewParams::paint_content_box_background => true,
					PluginFolderViewParams::background_url => "plugin_file://icons/bg4.jpg"
				)
		);

		return array
		(
			PluginFolderView::multiple_views_supported  => false,
			PluginFolderView::archive                   => null,
			PluginFolderView::view_kind                 => PLUGIN_FOLDER_VIEW_MOVIE,
			PluginFolderView::data                      => $movie_folder_view,
		);
	}

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

	public function handle_user_input(&$user_input, &$plugin_cookies)
	{
		if ($user_input->control_id == 'button_actions')
		{
			if (!isset($user_input->selected_media_url))
				return null;
			$media_url = MediaURL::decode($user_input->selected_media_url);
			$movie = $this->vod->get_loaded_movie($media_url->movie_id, $plugin_cookies);
			return ActionFactory::open_folder(VodOthetScreen::get_media_url_str($movie->id, null, "Режиссёры и актёры"));

		}
		return null;
	}


}

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