<?php

require_once 'lib/abstract_preloaded_regular_screen.php';

class I24hTopcatListScreen extends AbstractPreloadedRegularScreen
{
    const ID = 'topcat_list';
    private $topcats;
    private $subcats;
    private $session;

    public static function get_media_url_str($category_id)
    {
        return MediaURL::encode(
            array
            (
                'screen_id'     => self::ID,
                'category_id'   => $category_id,
            ));
    }
    
    public function __construct($session)
    {
        parent::__construct(
            self::ID,
            array
            (
            ));
        $this->session = $session;
        
        $subcats = array();
        
    }

    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->ensure_logged_in($plugin_cookies);
        $items = array();
        if (isset($media_url->xcat_id)) {
//            hd_print('isset media_url->xcat_id');
            $subcats = $this->fetch_subcat_list($media_url->xcat_id);
            if (I24hConfig::VOD_FAVORITES_SUPPORTED)
            {
                $items[] = array
                (
                    PluginRegularFolderItem::media_url => I24hVodSeriesListScreen::get_media_url_str('favorites'),
                    PluginRegularFolderItem::caption => I24hConfig::FAV_MOVIES_CATEGORY_CAPTION,
                    PluginRegularFolderItem::view_item_params => array
                    (
                        ViewItemParams::icon_path => 'gui_skin://small_icons/folder.aai',
                    )
                );
            }

            foreach ($subcats as $m)
            {
            if ($m['name'] != 'Избранное') {
                $items[] = array
                (
                    PluginRegularFolderItem::media_url => I24hVodSeriesListScreen::get_media_url_str($media_url->xcat_id, $m['id']),
                    PluginRegularFolderItem::caption => $m['name'],
                    PluginRegularFolderItem::view_item_params => array(
                        ViewItemParams::icon_path => 'gui_skin://small_icons/folder.aai'
                    )
                );
            }
            }

        } else {
//            hd_print('not isset media_url->xcat_id');
            $this->fetch_topcat_list($media_url->ganre_id);
            foreach ($this->topcats as $m)
            {
        	$items[] = array
        	(
        	    PluginRegularFolderItem::media_url =>
        	    MediaURL::encode(
        	    	array (
        	    	    'screen_id'   => I24hTopcatListScreen::ID,
        	    	    'xcat_id'   => $m['id']
        	    	    )),
        	    PluginRegularFolderItem::caption => $m['name'],
        	    PluginRegularFolderItem::view_item_params => array (
        	    	ViewItemParams::icon_path => $m['img']
        	    	)
        	    );
            }
        }
        return $items;
    }
    
    private function fetch_topcat_list($ganre_id)
    {
            $doc = $this->session->api_call(sprintf($this->session->get_api_server() . I24hConfig::TOPCAT_LIST_URL, $this->session->get_token()));
            $img_arr = I24hConfig::GET_ARCH_GENRES_LIST();
    	    $this->topcats = array();
    	    foreach ($doc as $c) {
                        $this->topcats[] = array(
                            'id' => strval($c->id),
                            'name' => strval($c->name),
                            'img' => $img_arr[strval($c->id)] . '?w=320&h=200' //preg_replace('/\/res\//', "/resize/320x200/res/", strval($c->dune->url))
                        );
    	    }
    }
    
    private function fetch_subcat_list($subcat_id)
    {
            $doc = $this->session->api_call(sprintf($this->session->get_api_server() . I24hConfig::SUBCAT_LIST_URL,
                $subcat_id, $this->session->get_token()));
    	    $subcats = array();
    	    foreach ($doc->filters as $c) {
    	    	    $subcats[] = array ( 
    	    	    	'id' => $c->id,
    	    	    	'name' => $c->name);
    	    }
    	    return $subcats;
    }

    public function get_topcst_list()
    {
    	    return $this->topcats;
    }
    
    public function get_folder_view(MediaURL $media_url, &$plugin_cookies)
    {

        if (isset($media_url->xcat_id)) {
            $folder_view =                 array
                (
                    PluginRegularFolderView::async_icon_loading => true,

                    PluginRegularFolderView::view_params => array
                    (
                        ViewParams::num_cols => 2,
                        ViewParams::num_rows => 8,
                        ViewParams::paint_details => false,
                        ViewParams::background_path => I24HSettings::$BG_PIC,
                        ViewParams::background_width => 1920,
                        ViewParams::background_height => 1080,
                        ViewParams::background_order => 'before_all'

                    ),
                    PluginRegularFolderView::base_view_item_params => array
                    (
                        ViewItemParams::item_paint_icon => true,
                        ViewItemParams::item_layout => HALIGN_LEFT,
                        ViewItemParams::icon_valign => VALIGN_CENTER,
                        ViewItemParams::icon_dx => 15,
                        ViewItemParams::icon_dy => -5,
                        ViewItemParams::item_caption_dx => 70,
                        ViewItemParams::item_caption_font_size => FONT_SIZE_NORMAL,
                        ViewItemParams::item_caption_width => 1550,
                        ViewItemParams::icon_path => 'gui_skin://small_icons/folder.aai'
                    ),
                    PluginRegularFolderView::not_loaded_view_item_params => array (),
                );
        } else {
                $folder_view =                 array
                (
                    PluginRegularFolderView::async_icon_loading => true,

                    PluginRegularFolderView::view_params => array
                    (
                        ViewParams::num_cols => 5,
                        ViewParams::num_rows => 1,
                        ViewParams::paint_details => false,
                        ViewParams::paint_sandwich => true,
                        ViewParams::sandwich_base => 'gui_skin://special_icons/sandwich_base.aai',
                        ViewParams::sandwich_mask => 'cut_icon://{name=sandwich_mask}',
                        ViewParams::sandwich_cover => 'cut_icon://{name=sandwich_cover}',
                        ViewParams::sandwich_width => 290,
                        ViewParams::sandwich_height => 160,
                        ViewParams::sandwich_icon_upscale_enabled => true,
                        ViewParams::sandwich_icon_keep_aspect_ratio => false,
                        ViewParams::background_path => I24HSettings::$BG_PIC,
                        ViewParams::background_width => 1920,
                        ViewParams::background_height => 1080,
                        ViewParams::background_order => 'before_all'
                    ),
                    PluginRegularFolderView::base_view_item_params => array
                    (
                        ViewItemParams::item_paint_icon => true,
                        ViewItemParams::item_layout => HALIGN_CENTER,
                        ViewItemParams::icon_valign => VALIGN_CENTER,
                        ViewItemParams::item_caption_font_size => FONT_SIZE_NORMAL,
                        ViewItemParams::item_caption_width => 1550,
                        ViewParams::sandwich_width => 290,
                        ViewParams::sandwich_height => 160,
                        ViewItemParams::item_paint_caption => false,
                    	ViewItemParams::icon_path => 'plugin_file://icons/mov_unset.png',
                    	ViewItemParams::item_detailed_icon_path => 'missing://',

                    ),
                    PluginRegularFolderView::not_loaded_view_item_params => array (),
                );

        }

        $folder_view[PluginRegularFolderView::actions] =
            $this->get_action_map($media_url, $plugin_cookies);

        $folder_view[PluginRegularFolderView::initial_range] = 
            $this->get_folder_range($media_url, 0, $plugin_cookies);

        $archive = $this->get_archive($media_url);
        $archive_def = is_null($archive) ? null :
            $archive->get_archive_def();

        return array
        (
            PluginFolderView::multiple_views_supported  => 0,
            PluginFolderView::archive                   => $archive_def,
            PluginFolderView::view_kind                 => PLUGIN_FOLDER_VIEW_REGULAR,
            PluginFolderView::data                      => $folder_view
        );
    }

}
?>
