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

require_once 'lib/tv/tv.php';
require_once 'lib/tv/default_group.php';
require_once 'lib/tv/favorites_group.php';
require_once 'lib/tv/all_channels_group.php';

abstract class AbstractTv implements Tv
{
	const MODE_CHANNELS_1_TO_N = false;
	const MODE_CHANNELS_N_TO_M = true;

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

	private $mode;
	private $favorites_supported;
	private $playback_url_is_stream_url;
	private	$plugin;

	protected $channels;
	protected $groups;

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

	protected function __construct($mode, $favorites_supported, $playback_url_is_stream_url)
	{
		$this->mode = $mode;
		$this->favorites_supported = $favorites_supported;
		$this->playback_url_is_stream_url = $playback_url_is_stream_url;
	}

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

	public function is_favorites_supported()
	{
		return $this->favorites_supported;
	}

	public function get_all_channel_group_id()
	{
		return '__all_channels';
	}

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

	public function unload_channels()
	{
		$channels = null;
		$groups = null;
	}

	protected abstract function load_channels(&$plugin_cookies);

	public function ensure_channels_loaded(&$plugin_cookies)
	{
		if (!isset($this->channels))
			$this->load_channels($plugin_cookies);
	}

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

	public function get_channels()
	{ return $this->channels; }

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

	public function get_channel($channel_id)
	{
		$c = $this->channels->get($channel_id);
		if (is_null($c))
			throw new Exception("Unknown channel: $channel_id");
		return $c;
	}

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

public function get_timeshift_hours($channel_id)
{
$c = $this->channels->get($channel_id);
return $c;
}

////////////////////////////////////////////
public function get_epg_url($channel_id)
{
$c = $this->channels->get($channel_id);
return $c;
}


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

	public function get_groups()
	{ return $this->groups; }

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

	public function get_group($group_id)
	{
		$g = $this->groups->get($group_id);

		if (is_null($g))
			throw new Exception("Unknown group: $group_id");

		return $g;
	}

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

	public function get_tv_info(MediaURL $media_url, &$plugin_cookies)
	{
		return null;
	}

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

	public function get_tv_stream_url($media_url, &$plugin_cookies)
	{ throw new Exception('Error: get_tv_stream_url() is not supported.'); }

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

	public function get_vod_stream_url($playback_url, &$plugin_cookies)
	{ throw new Exception('Error: get_vod_stream_url() is not supported.'); }

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

	public function get_tv_playback_url($channel_id, $archive_ts, $protect_code, &$plugin_cookies)
	{
		$this->ensure_channels_loaded($plugin_cookies);
		return $this->get_channel($channel_id)->get_streaming_url();
	}

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

	protected abstract function get_day_epg_iterator($channel_id, $day_start_ts, &$plugin_cookies);

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

	public function get_day_epg($channel_id, $day_start_ts, &$plugin_cookies)
	{
		$epg = array();
		foreach ($this->get_day_epg_iterator($channel_id, $day_start_ts, $plugin_cookies) as $e)
		{
			$epg[] =
				array
				(
					PluginTvEpgProgram::start_tm_sec => $e->get_start_time(),
					PluginTvEpgProgram::end_tm_sec => $e->get_finish_time(),
					PluginTvEpgProgram::name => $e->get_title(),
					PluginTvEpgProgram::description => $e->get_description()
				);
		}
		return $epg;
	}

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

	public function is_favorite_channel_id($channel_id, &$plugin_cookies)
	{
		if (!$this->is_favorites_supported())
			return false;
		$fav_channel_ids = $this->get_fav_channel_ids($plugin_cookies);
		$k = array_search($channel_id, $fav_channel_ids);
		return $k !== false;
	}

	public function change_tv_favorites($fav_op_type, $channel_id, &$plugin_cookies)
	{
		$fav_channel_ids = $this->get_fav_channel_ids($plugin_cookies);
		if ($fav_op_type === PLUGIN_FAVORITES_OP_ADD)
		{
			array_push($fav_channel_ids, $channel_id);
		}
		else if ($fav_op_type === PLUGIN_FAVORITES_OP_REMOVE)
		{
			$k = array_search($channel_id, $fav_channel_ids);
			if ($k !== false)
				unset ($fav_channel_ids[$k]);
		}
		else if ($fav_op_type === PLUGIN_FAVORITES_OP_MOVE_UP)
		{
			$k = array_search($channel_id, $fav_channel_ids);
			if ($k !== false && $k !== 0)
			{
				$t = $fav_channel_ids[$k - 1];
				$fav_channel_ids[$k - 1] = $fav_channel_ids[$k];
				$fav_channel_ids[$k] = $t;
			}
		}
		else if ($fav_op_type === PLUGIN_FAVORITES_OP_MOVE_DOWN)
		{
			$k = array_search($channel_id, $fav_channel_ids);

			if ($k !== false && $k !== count($fav_channel_ids) - 1)
			{
				$t = $fav_channel_ids[$k + 1];
				$fav_channel_ids[$k + 1] = $fav_channel_ids[$k];
				$fav_channel_ids[$k] = $t;
			}
		}

		$this->set_fav_channel_ids($plugin_cookies, $fav_channel_ids);

		return ActionFactory::invalidate_folders(
			array(TvFavoritesScreen::get_media_url_str()));
	}

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

	public function get_fav_channel_ids(&$plugin_cookies)
	{
		$fav_channel_ids = array();
		if (isset($plugin_cookies->{'favorite_channels'}))
			$fav_channel_ids = preg_split('/,/', $plugin_cookies->{'favorite_channels'});
		return $fav_channel_ids;
	}

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

	public function set_fav_channel_ids(&$plugin_cookies, $ids)
	{
		$plugin_cookies->{'favorite_channels'} = join(',', $ids);
	}

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

	///////////////////////////////////////////////////////////////////////
	// Archive.

	public function get_archive(MediaURL $media_url)
	{ return null; }

	///////////////////////////////////////////////////////////////////////
	// Hooks.

	public function folder_entered(MediaURL $media_url, &$plugin_cookies)
	{ /* Nop */ }

	// Hook for adding special group items.
	public function add_special_groups(&$items)
	{ /* Nop */ }
}

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