%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Key aliases for default video player handling in server mode
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% In inherited modes configuration files it needs to load:
%(Init)=\
%	Include($(CfgDir)/Utils/aliases-server.cfg);\
%       Include($(CfgDir)/Utils/aliases-server-fbrowser-v3i.cfg);\

[Protocol]=Server

%
% It needs to define $(app_manage_mode) variable before
% In inherited modes APP_IS_RUN macro must define $(app_run) variable (1== app is run, 0 == app does not run)
%
[Mode]=default_video_player_init

(EnterMode)=\
	Set(popup,show,Loading...);\
	Make(var,app_run,by_value,0);\
	Make(var,open_file,by_value,);\
	Macro(APP_IS_RUN);\
	Macro(APP_MANAGE,expr 1 - $(app_run));\
	Macro(APP_CHOOSE_FILE,echo $(app_run));\
	Set(popup,hide);

APP_CHOOSE_FILE=\
	Make(mode,simple_file_manager);

APP_MANAGE=\
	Make(mode,$(app_manage_mode));

[ModeEnd]


% Variable $(open_file) should be defined in simple_file_manager mode

%
% Screen setup
%
% It needs to define $(app_name) variable before, like this:
% ...
% Make(var,app_name,by_value,Totem);\
% Macro(SETUP_DEFAULT_PLAYER);\
% ...
% It is good ides to define $(app_name) variable in (Init)
%

[Mode]=default_video_player_base

% Android support
VOL-=Macro(DEFAULT_VOLUME_DOWN);
VOL+=Macro(DEFAULT_VOLUME_UP);
SlideDown =Macro(DEFAULT_VOLUME_DOWN);
SlideUp   =Macro(DEFAULT_VOLUME_UP);
COVER     =Macro(PLAY);

InCall($$)=Macro(PAUSE);
EndCall($$)=Macro(PLAY);

% Default action on text screen closing
Back=\
	Set(text,close);\
	Macro(APP_MENU);

% Default action on list screen closing
Back($$)=\
	Set(list,close);\
	Macro(APP_MENU);
	
% Rarely used
Cancel=\
    Macro(QUIT);\
    Make(exit);

[ModeEnd]

[Mode]=default_video_player : default_video_player_base

(EnterMode)=\
	Macro(SETUP_DEFAULT_POWER_MANAGEMENT);\
	Macro(APP_SETUP);\
	Macro(APP_START_IF);\
	Macro(APP_RAISE);\
	Macro(SETUP_DEFAULT_PLAYER);\
	Macro(APP_MENU);\
	Macro(APP_POST_SETUP);

SETUP_DEFAULT_PLAYER=\
    Set(layout,default);\
	Set(status,);\
	Set(title,$(open_file));\
	Set(icons,$(app_name),\
		1,vol_down,\
		2,mute,\
		3,vol_up,\
		4,rewind,\
		5,play,\
		6,forward,\
		7,folder,\
		8,stop,\
		9,fullscreen,\
		*,question,\
		0,pause,\
		#,no)

1=Macro(DEFAULT_VOLUME_DOWN);
2=Macro(DEFAULT_TOGGLE_MUTE);
3=Macro(DEFAULT_VOLUME_UP);

4=Macro(RWD);
5=Macro(PLAY);
6=Macro(FF);

7=Macro(APP_QUIT);\
	Make(mode,simple_file_manager);
8=Macro(STOP);
9=Macro(APP_FULLSCREEN);

* *=Macro(DEFAULT_HELP);
0=Macro(PAUSE);
#=Macro(APP_QUIT);\
	Make(mode,simple_file_manager);

% Default menu
APP_MENU=\
	Set(menu,replace,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

DEFAULT_HELP=\
    Set(text,replace,Help,1 Vol-\n2 Mute\n3 Vol+\n4 Rewind\n5 Play\n6 FF\n7 File Browser\n8 Stop\n9 Fullscreen\n* * Help\n0 Pause\n# Quit $(app_name))

% Android support
SlideLeft =Macro(NEXT);
SlideRight=Macro(PREV);

[ModeEnd]

[Mode]=default_video_player_5x1 : default_video_player_base

(EnterMode)=\
	Make(var,enter_action,if [ "x$(do_app_init)" = "x1" ]; \
                          then echo DO_ENTER_ACTION; \
                          else echo DO_REFRESH_ACTION; \
                          fi);\
	Macro($(enter_action));

DO_ENTER_ACTION=\
	Macro(SETUP_DEFAULT_POWER_MANAGEMENT);\
	Macro(TEST_LYNX);\
	Macro(TEST_CURL);\
	Macro(APP_SETUP);\
	Macro(APP_START_IF);\
	Macro(APP_RAISE);\
	Macro(SETUP_DEFAULT_PLAYER);\
	Macro(APP_MENU);\
	Macro(APP_POST_SETUP);

DO_REFRESH_ACTION=\
	Make(var,do_app_init,by_value,1);\
	Macro(APP_MENU);\
	Macro(APP_POST_SETUP);

SETUP_DEFAULT_PLAYER_LAYOUT=\
	Set(layout,7x1,choose,2,ticker,up,VOL+,down,VOL-);\
	Set(status,);\
	Set(title,$(open_file));\
	Set(icons,$(app_name),\
		1,rewind,\
		2,play,\
		3,forward,\
		4,pause,\
		5,stop,\
		6,none,\
		7,none,\
		8,none,\
		9,none,\
		*,none,\
		0,none,\
		#,none);\

SETUP_DEFAULT_PLAYER=\
	Macro(SETUP_DEFAULT_PLAYER_LAYOUT);\
	Exec(if [ ! -d $(TmpDir)/imdb ]; then mkdir $(TmpDir)/imdb; fi);\
	Exec(if [ ! -d $(TmpDir)/imdb/posters ]; then mkdir $(TmpDir)/imdb/posters; fi);\
	Make(var,cover_force,by_value,);\
	Make(var,cover_index,by_value,1);\
	Get(cover_size);

1=Macro(RWD);
2=Macro(PLAY);
3=Macro(FF);
4=Macro(PAUSE);
5=Macro(STOP);\
	Macro(APP_QUIT);\
	Make(mode,simple_file_manager); 

% Default menu
APP_MENU=\
	Set(menu,replace,Help,Mute,Files,Quit,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

DEFAULT_HELP=\
	Set(text,replace,Help,Joystick Down Vol-\nJoystick Up Vol+\n1 Rewind\n2 Play\n3 FastForward\n4 Pause\n5 Stop and quit $(app_name));

% Some players does not supports FF/RWD
DEFAULT_HELP_NO_FF_FWD=\
	Set(text,replace,Help,Joystick Down Vol-\nJoystick Up Vol+\n1 \n2 Play\n3 \n4 Pause\n5 Stop and quit $(app_name));

Help=\
	Macro(DEFAULT_HELP);

Quit=\
	Macro(APP_QUIT);

Mute=\
	Macro(DEFAULT_TOGGLE_MUTE);

Files=\
	Macro(STOP);\
	Macro(APP_QUIT);\
	Make(mode,simple_file_manager); 

IMDB=\
    Make(mode,imdb_browser);

% Android support
SlideLeft =Macro(FF);
SlideRight=Macro(RWD);

CoverSize($$)=\
	Make(var,cover_size,by_value,$(Index));\
	Macro(CHECK_IMDB_POSTER);

%
% Retrieve poster from IMDB
%

% Rely on $(open_file) variable
CHECK_IMDB_POSTER=\
	Make(var,movie_file_hash,$(CfgDir)/Utils/generate_poster.sh -hash "$(fbrowser_dir)/$(open_file)");\
	Make(var,scaled_poster_hash,by_value,$(movie_file_hash)-$(cover_size).png);\
	Make(var,poster_action,if [ -s $(TmpDir)/imdb/posters/$(scaled_poster_hash) ]; \
                           then echo SET_IMDB_POSTER; else echo LOAD_IMDB_POSTER; fi);\
	Macro($(poster_action));

SET_IMDB_POSTER=\
	Set(cover,noname,$(TmpDir)/imdb/posters/$(scaled_poster_hash));

LOAD_IMDB_POSTER=\
	Exec($(CfgDir)/Utils/generate_poster.sh -poster "$(fbrowser_dir)/$(open_file)" $(cover_size) "$(TmpDir)/imdb" "$(CfgDir)");\
	Timer(WAIT_IMDB_POSTER,2,15);

% timeout time is 30 = 2*15
WAIT_IMDB_POSTER=\
	Make(var,poster_wait_action,if [ -s $(TmpDir)/imdb/posters/$(scaled_poster_hash) ]; \
                                then echo SET_IMDB_POSTER; else echo DUMMY_IMDB_POSTER; fi);\
	Macro($(poster_wait_action));
    
DUMMY_IMDB_POSTER=\
	Make(var,default_scaled_poster,by_value,default-$(cover_size).png);\
	Make(var,dummy,\
         if [ ! -s $(TmpDir)/imdb/posters/$(scaled_poster_hash) ];\
         then \
             convert -resize $(cover_size)x$(cover_size) -depth 8 -background transparent \
                $(CfgDir)/Icons/common/cover-video.png $(TmpDir)/imdb/posters/$(default_scaled_poster) 2> /dev/null; \
         fi);\
	Set(cover,noname,$(TmpDir)/imdb/posters/$(default_scaled_poster));

TEST_LYNX=\
	Exec(P=`which lynx|grep lynx|grep -v no|wc -l|tr -d " "`; \
	     if [ "x$P" != "x1" ]; then \
	         $(CfgDir)/Utils/message.sh "ERROR: lynx not found"; \
	     fi);

TEST_CURL=\
	Exec(P=`which curl|grep curl|grep -v no|wc -l|tr -d " "`; \
	     if [ "x$P" != "x1" ]; then \
	         $(CfgDir)/Utils/message.sh "ERROR: curl not found"; \
	     fi);

[ModeEnd]

[Mode]=imdb_browser

% It needs to define $(app_manage_mode) and $(open_file) variables before

% Rely on $(open_file) variable
(EnterMode)=\
	Make(var,movie_file_hash,$(CfgDir)/Utils/generate_poster.sh -hash "$(fbrowser_dir)/$(open_file)");\
	Macro(IMDB_SETUP);

IMDB_SETUP=\
	Set(list,replace,IMDB,IMDB data,Change Poster,Delete IMDB data,Delete all IMDB data);\
	Set(menu,replace,Choose);

Choose(IMDB data)=\
	ExecAndSet(text,replace,IMDB,cat $(TmpDir)/imdb/$(movie_file_hash)|grep -v 'Poster URL'|grep -v 'IMDB movie URL');

Choose(Change Poster)=\
	Make(mode,imdb_choose_name);

Choose(Delete IMDB data)=\
	Make(mode,imdb_remove_one);

Choose(Delete all IMDB data)=\
	Make(mode,imdb_remove_all);

Push(IMDB data)=\
	Macro(Choose(IMDB data));

Push(Change Poster)=\
	Macro(Choose(Change Poster));

Push(Delete IMDB data)=\
	Macro(Choose(Delete IMDB data));

Push(Delete all IMDB data)=\
	Macro(Choose(Delete all IMDB data));

Back=\
	Macro(IMDB_SETUP);

% Rely on $(app_manage_mode) variable
Back($$)=\
	Set(list,close);\
	Make(mode,$(app_manage_mode));

(ExitMode)=\
	Make(var,do_app_init,by_value,0);

[ModeEnd]

[Mode]=imdb_choose_name : default_video_player_5x1

(EnterMode)=\
	Make(var,def_title,echo "$(open_file)"|sed "s/.avi//g;s/.mkv//g;s/.mp4//g");\
	Set(editfield,Enter Movie Title,Movie,$(def_title));

Ok($$)=\
	Exec($(CfgDir)/Utils/generate_poster.sh -force_poster "$(fbrowser_dir)/$(open_file)" $(cover_size) "$(TmpDir)/imdb" "$(CfgDir)" "$(Param)");\
	Set(list,close);\
	Set(cover,clear);\
	Timer(WAIT_IMDB_POSTER,2,15);\
	Make(mode,$(app_manage_mode));

Back($$)=\
	Make(mode,imdb_browser);

(ExitMode)=\
	Make(var,do_app_init,by_value,0);

[ModeEnd]

[Mode]=imdb_remove_one

(EnterMode)=\
	Set(list,replace,Remove,Yes,No);\
	Set(menu,replace,Choose);

Back($$)=\
	Make(mode,imdb_browser);
	
Push($$)=\
	Macro(Choose($$));
    
Choose($$)=\
	Exec(if [ "x$(Param)" = "xYes" ];\
	     then \
		rm -f $(TmpDir)/imdb/$(movie_file_hash);\
		ls $(TmpDir)/imdb/posters|grep $(movie_file_hash)|xargs -i rm -f $(TmpDir)/imdb/posters/{}; \
	     fi);\
	Make(mode,imdb_browser);

[ModeEnd]

[Mode]=imdb_remove_all

(EnterMode)=\
	Set(list,replace,Remove,Yes,No);\
	Set(menu,replace,Choose);

Back($$)=\
	Make(mode,imdb_browser);
	
Push($$)=\
	Macro(Choose($$));

Choose($$)=\
	Exec(if [ "x$(Param)" = "xYes" ];\
	     then \
		rm -rf $(TmpDir)/imdb; \
	     fi);\
	Make(mode,imdb_browser);

[ModeEnd]

[End]
