#!/bin/bash
#pMusic - external menu
#Copyright 2014
#Sigmund Berglund

[ $UTF8 = false ] && export LC_ALL=C
echo -n > $WORKDIR/OUTPUT

if [ $PTR_BTN = 1 ]; then
	$APPDIR/func -search_refresh no_albumart &
	exit
elif [ $PTR_BTN = 2 ]; then
	OUTPUT=view
elif [ $PTR_BTN = 3 ]; then
	template(){
	echo '<hbox spacing="0">
	 <pixmap width-request="16" icon_size="1" space-expand="false" space-fill="false"><input file stock="gtk-'$3'"></input></pixmap>
	 <button height-request="20" xalign="0" can-focus="no" relief="2" '$TOOLTIP' space-expand="true" space-fill="true">
	  '$SENSITIVE'
	  <label>" '$1'"</label>
	  <action>echo '$2' > '$WORKDIR'/OUTPUT</action>
	  <action>EXIT:exit</action>
	 </button>
	</hbox>'
	}

	[ "$1" = disabled ] && SENSITIVE='<sensitive>false</sensitive>'
	S=$S"
	$(template " $(gettext 'Add album to playqueue')" add add_playque)
	$(template " $(gettext 'Search content (left-click)')" search find)
	$(template " $(gettext 'View album-art (middle-click)')" view select-color)
	$(template " $(gettext 'Edit album-art in image-editor')" edit edit)
	$(template " $(gettext 'Delete album-art image file')" delete delete)
	"

	export gtkdialog_menu='
	<window decorated="false" skip_taskbar_hint="true" resizable="false" height-request="190" width-request="280" modal="true" space-expand="true" space-fill="true">
	 <hbox scrollable="true" hscrollbar-policy="2" vscrollbar-policy="2" space-expand="true" space-fill="true">
	  <text width-request="1" space-expand="false" space-fill="false"><label>""</label></text>
	  <vbox spacing="2">
	   <text height-request="2" space-expand="false" space-fill="false"><label>""</label></text>
	   '$S'
	   <hseparator></hseparator>
	   <hbox>
	    <pixmap width-request="16" icon_size="1" space-expand="false" space-fill="false"><input file stock="gtk-preferences"></input></pixmap>
	    <text width-request="1" space-expand="false" space-fill="false"><label>""</label></text>
	    <text xalign="0" space-expand="true" space-fill="true"><label>"'$(gettext 'Images in row')'"</label></text>
	    <spinbutton editable="false" range-max="20" range-min="1" width-request="60" space-expand="false" space-fill="false">
	     <variable>ALBUMART_SEARCH_NR</variable>
	     <default>'$ALBUMART_SEARCH_NR'</default>
	    </spinbutton>
	    <text width-request="30" space-expand="false" space-fill="false"><label>""</label></text>
	   </hbox>
	   <hbox>
	    <text width-request="16" space-expand="false" space-fill="false"><label>""</label></text>
	    <text width-request="1" space-expand="false" space-fill="false"><label>""</label></text>
	    <text xalign="0" space-expand="true" space-fill="true"><label>"'$(gettext 'Image height (px)')'"</label></text>
	    <spinbutton editable="false" range-max="150" range-min="10" width-request="60" space-expand="false" space-fill="false">
	     <variable>ALBUMART_SEARCH_HEIGHT</variable>
	     <default>'$ALBUMART_SEARCH_HEIGHT'</default>
	    </spinbutton>
	    <button height-request="20" width-request="30" can-focus="no" space-expand="false" space-fill="false">
	     <input file stock="gtk-apply"></input>
	     <action>echo set_me $ALBUMART_SEARCH_NR $ALBUMART_SEARCH_HEIGHT > '$WORKDIR'/OUTPUT</action>
	     <action>EXIT:exit</action>
	    </button>
	   </hbox>
	  </vbox>
	 </hbox>
	 <action signal="focus-out-event">EXIT:exit</action>
	</window>'

	$GTKDIALOG -p gtkdialog_menu -G +"$(($PTR_X_ROOT-5))"+"$(($PTR_Y_ROOT-5))" > /dev/null
	OUTPUT="$(<"$WORKDIR/OUTPUT")"
fi

export PTR_BTN=""
SOURCE="$STORAGE_DIR/albumart/$(<$WORKDIR/filebrowser_dir).jpg"
case "$OUTPUT" in
	search)
		$APPDIR/func -search_refresh no_albumart &
		;;
	add)
		TMP="`LC_ALL=C grep -Fi "$(<$WORKDIR/filebrowser_dir).pmu|" "$STORAGE_DIR/index_mymusic" | cut -d'|' -f14`" #grep -i is helpful
		[ "$TMP" ] && $APPDIR/func_add -add "$TMP"
		;;
	view)
		if [ "$SOURCE" ] && [ "$SOURCE" != "$(gettext 'Search for music')"  ]; then
			echo '
			<window title="'"$(<$WORKDIR/filebrowser_dir)"'" window_position="2" icon-name="pmusic">
			 <eventbox above-child="true" visible-window="true" spacing="0" space-expand="false" space-fill="false">
			  <pixmap><input file>"'$SOURCE'"</input></pixmap>
			  <action signal="button-release-event">EXIT:exit</action>
			 </eventbox>
			</window>' | $GTKDIALOG -s &
		fi
		;;
	edit)
		export GTK2_RC_FILES="$HOME/.gtkrc-2.0"
		$IMAGE_EDITOR "$SOURCE" &
		;;
	delete)
		FRAME="$(gettext 'Delete source file')"; IMG="delete"; TXT1="$SOURCE"
		. $APPDIR/box_yesno
		if [ $EXIT = "yes" ]; then
			rm "$SOURCE"
			$APPDIR/func -search_albumart_update
		fi
		;;
	set_me*)
		export ALBUMART_SEARCH_HEIGHT="$(cut -d' ' -f3 <<< $OUTPUT)"
		export ALBUMART_SEARCH_NR="$(cut -d' ' -f2 <<< $OUTPUT)"
		$APPDIR/func_config -write_config
		rm -f $WORKDIR/xml_pmusic* #remove xml-files to force fresh load of all guis (langauge change) 
		pmusic #new start to load changed settings
		;;
esac
exit 0
