#!/bin/bash

export TEXTDOMAIN=pmusic
[ ! -s $WORKDIR/filebrowser_dir ] && echo $HOME > $WORKDIR/filebrowser_dir
export BROWSER_DIR=($(<"$WORKDIR/filebrowser_dir"))
echo 0 > $WORKDIR/tab_search #default tab in <notebook>

searchfield(){
echo '
<vbox>
 <hbox spacing="1">
  <text width-request="4" space-expand="false" space-fill="false"><label>""</label></text>
  <button space-expand="false" space-fill="false" tooltip-text="'$(gettext 'Favorites')'">
   <input file stock="gtk-favorites"></input>
   <action signal="button-press-event">clear:SOURCE</action>
   <action>'$APPDIR'/func_favorites -show_favorites</action> 
   <action>refresh:SOURCE</action>
   <action type="activate">ALBUMART_SEARCH_ACTIVATE</action>
  </button>
  <togglebutton space-expand="false" space-fill="false" tooltip-text="'$(gettext 'Define which sources to search')'">
   <variable>VIEW_SEARCH_OPTIONS</variable>
   <input file stock="gtk-find_more"></input>
   <default>'$VIEW_SEARCH_OPTIONS'</default>
   <action>if true show:VBOX_SEARCH_OPTIONS</action>
   <action>if false hide:VBOX_SEARCH_OPTIONS</action>
  </togglebutton>
  <togglebutton space-expand="false" space-fill="false" tooltip-text="'$(gettext 'Smart add')'">
   <variable>VIEW_ADD_OPTIONS</variable>
   <input file stock="gtk-add_more"></input>
   <default>'$VIEW_ADD_OPTIONS'</default>
   <action>if true show:VBOX_ADD_OPTIONS</action>
   <action>if false hide:VBOX_ADD_OPTIONS</action>
  </togglebutton>
  <button relief="2" space-expand="false" space-fill="false" tooltip-text="'$(gettext 'Go back in source history')'">
   <variable>HISTORY_UNDO</variable>
   <input file stock="gtk-pmusic_undo"></input>
   <height>20</height>
   <action>'$APPDIR'/func -history_undo</action>
   <action>refresh:SEARCH</action>
   <action>refresh:SOURCE</action>
   <action type="activate">ALBUMART_SEARCH_ACTIVATE</action>
  </button>
  <button relief="2" space-expand="false" space-fill="false" tooltip-text="'$(gettext 'Go forward in source history')'">
   <variable>HISTORY_REDO</variable>
   <input file stock="gtk-pmusic_redo"></input>
   <action>'$APPDIR'/func -history_redo</action>
   <action>refresh:SEARCH</action>
   <action>refresh:SOURCE</action>
   <action type="activate">ALBUMART_SEARCH_ACTIVATE</action>
  </button>
  <entry name="search" height-request="30" activates-default="true" is-focus="true" secondary-icon-stock="gtk-find" tooltip-markup="'"$(gettext "<b>Free-search</b>
  <i>'nig tnt'</i> finds 'TNT - Desperate night'
  <i>'nig -tnt'</i> does NOT find any TNT files 
<b>Add files and URLs</b> to playqueue
<b>Browse paths</b>")"'" space-expand="true" space-fill="true">
   <variable>SEARCH</variable>
   <input file>'$WORKDIR'/filebrowser_dir</input>
   <action signal="activate">'$APPDIR'/func -search_refresh</action>
   <action signal="activate">refresh:SOURCE</action>
   <action signal="activate" type="activate">ALBUMART_SEARCH_ACTIVATE</action>
##   <action signal="activate" function="break">""</action>
   <action signal="secondary-icon-release">'$APPDIR'/func -search_refresh</action>
   <action signal="secondary-icon-release">refresh:SOURCE</action>
   <action signal="secondary-icon-release" type="activate">ALBUMART_SEARCH_ACTIVATE</action>
   <action condition="command_is_true([[ `echo \"$SEARCH\" | grep -F \"'$(gettext 'Search for music')'\"` ]] && echo true)" signal="enter-notify-event">clear:SEARCH</action>'
   [ $SEARCH_WHILE_TYPING = true ] && echo '<action signal="key-release-event">'$APPDIR'/func -search &</action>'
  echo '</entry>
 </hbox>

 <vbox visible="'$VIEW_SEARCH_OPTIONS'" space-expand="true" space-fill="true">
 <hseparator></hseparator>
 <hbox spacing="5" tooltip-text="'$(gettext 'Define which sources to search')'" space-expand="true" space-fill="true">
  <text width-request="0" space-expand="false" space-fill="false"><label>""</label></text>
  <button space-expand="false" space-fill="false">
   <label>'$(gettext 'Search')'</label>
   <input file stock="gtk-pmusic_find"></input>
   <action>'$APPDIR'/func -search_refresh</action>
   <action>refresh:SOURCE</action>
  </button>
  <text width-request="15" space-expand="false" space-fill="false"><label>""</label></text>
  <vbox spacing="2" space-expand="false" space-fill="false">
   <checkbox label="'$(gettext 'My Tracks')'" height-request="17">
    <variable>SEARCH_MYMUSIC</variable>
    <input>echo $SEARCH_MYMUSIC</input>'
    [ ! -f "$STORAGE_DIR/index_mymusic" ] && echo '<sensitive>false</sensitive>'
    echo '
   </checkbox>
   <checkbox label="'$(gettext 'Collections')'" height-request="17">
    <variable>SEARCH_COLLECTIONS</variable>
    <input>echo $SEARCH_COLLECTIONS</input>'
    if [ ! -f "$STORAGE_DIR/index_mymusic" ]; then echo '<sensitive>false</sensitive>'; fi
    echo '
   </checkbox>
  </vbox>
  <vbox spacing="2" space-expand="false" space-fill="false">
   <checkbox label="'$(gettext 'Radio stations')'" height-request="17">
    <variable>SEARCH_RADIO</variable>
    <input>echo $SEARCH_RADIO</input>
   </checkbox>
   <checkbox label="'$(gettext 'Album art')'" height-request="17">
    <variable>SEARCH_ALBUMART</variable>
    <input>echo $SEARCH_ALBUMART</input>
    <action>if true show:ALBUMART_SEARCH</action>
    <action>if false hide:ALBUMART_SEARCH</action>
   </checkbox>
  </vbox>
  <vbox spacing="2" space-expand="true" space-fill="true">
   <checkbox label="'$(gettext 'Web Music')'" height-request="17" space-expand="true" space-fill="true">
    <variable>SEARCH_WEBMUSIC</variable>
    <input>echo $SEARCH_WEBMUSIC</input>
   </checkbox>
   <hbox spacing="0" space-expand="true" space-fill="true">
    <checkbox label="" height-request="17" space-expand="false" space-fill="false">
     <variable>SEARCH_FILES</variable>'
     ! type pfilesearch > /dev/null 2>&1 && echo '<sensitive>false</sensitive>'
     echo '<input>echo $SEARCH_FILES</input>
    </checkbox>
    <entry height-request="17" space-expand="true" space-fill="true">
     <variable>SEARCHPATH</variable>
     <default>"'$SEARCHPATH'"</default>'
     ! type pfilesearch > /dev/null 2>&1 && echo '<sensitive>false</sensitive>'
    echo '
    </entry>
   </hbox>
  </vbox>
 </hbox>
 <variable>VBOX_SEARCH_OPTIONS</variable>
 </vbox>
 
 <vbox visible="'$VIEW_ADD_OPTIONS'" space-expand="true" space-fill="true">
 <hseparator></hseparator>
 <hbox spacing="5" tooltip-text="'$(gettext 'Add tracks based on request')'" space-expand="true" space-fill="true">
  <text width-request="0" space-expand="false" space-fill="false"><label>""</label></text>
  <button space-expand="false" space-fill="false">
   <label>'$(gettext 'Add')'</label>
   <input file stock="gtk-pmusic_add"></input>
   ##now save the tree in case sort order has been changed. as seen below we have to rearrange since gtkdialog always execute icons in col2.
   <action>save:SOURCE</action>
   <action>cut -d"|" -f3-16 '$WORKDIR'/sourcelist_output > '$WORKDIR'/tmp_sourcelist1</action>
   <action>cut -d"|" -f2 '$WORKDIR'/sourcelist_output > '$WORKDIR'/tmp_sourcelist2</action>
   <action>cut -d"|" -f18- '$WORKDIR'/sourcelist_output > '$WORKDIR'/tmp_sourcelist3</action>
   <action>paste -d "|" '$WORKDIR'/tmp_sourcelist1 '$WORKDIR'/tmp_sourcelist2 '$WORKDIR'/tmp_sourcelist3 > '$WORKDIR'/sourcelist</action>
   ##---
   <action>'$APPDIR'/func_add -smartadd $SMARTADD_NR $SMARTADD_RANDOM $SMARTADD_RATE "$SMARTADD_SOURCE" &</action>
   <action>refresh:PLAYLIST</action>
  </button>
  <text width-request="10" space-expand="false" space-fill="false"><label>""</label></text>
  <vbox spacing="2">
   <radiobutton height-request="17" space-expand="false" space-fill="false">
    <variable>SMARTADD_SORT_ORDER</variable>
    <label>'$(gettext 'By sort order')'</label>
   </radiobutton>
   <radiobutton height-request="17" space-expand="false" space-fill="false">
    <variable>SMARTADD_RANDOM</variable>
    <input>echo '$SMARTADD_RANDOM'</input>
    <label>'$(gettext 'Random')'</label>
   </radiobutton>
  </vbox>
  <vbox space-expand="false" space-fill="false" tooltip-text="'$(gettext 'How many tracks should be added to playqueue')'">
   <comboboxentry width-request="65" space-expand="true" space-fill="true">
    <variable>SMARTADD_NR</variable>
    <default>"'$SMARTADD_NR'"</default>
    <input>echo -e "'$(gettext 'All')'\n5\n10\n20\n30\n50\n100"</input>
   </comboboxentry>
  </vbox>
  <text space-expand="false" space-fill="false" width-request="10"><label>""</label></text>
  <text space-expand="false" space-fill="false"><label>"'$(gettext 'Min rating')'"</label></text>
  <spinbutton range-max="999" width-request="47" space-expand="false" space-fill="false">
   <variable>SMARTADD_RATE</variable>
   <input>echo '$SMARTADD_RATE'</input>
  </spinbutton>
  <text space-expand="false" space-fill="false" width-request="10"><label>""</label></text>
  <text space-expand="false" space-fill="false"><label>"'$(gettext 'Source')'"</label></text>
  <vbox homogeneous="true" space-expand="false" space-fill="false">
   <comboboxtext space-expand="false" space-fill="false">
    <variable>SMARTADD_SOURCE</variable>
    <input>echo -e "'$SMARTADD_SOURCE'\n'$(gettext 'List below')'\n'$(gettext 'My local music (DB)')'"</input>
   </comboboxtext>
  </vbox>
  <text space-expand="true" space-fill="true"><label>""</label></text>
 </hbox>
 <variable>VBOX_ADD_OPTIONS</variable>
 </vbox>
</vbox>'
}


GUI_ADD_SEARCH="$(searchfield)"
for I in $(seq 1 $ALBUMART_SEARCH_NR); do
	XML_ALBUMART_SEARCH=$XML_ALBUMART_SEARCH'
	<eventbox name="search_albumart" above-child="true" visible-window="true" spacing="0" space-expand="false" space-fill="false">
	 <pixmap>
	  <variable>ALBUMART_SEARCH'$I'</variable>
	  <input file>'$WORKDIR'/albumart_search'$I'.jpg</input>
	  <height>'$ALBUMART_SEARCH_HEIGHT'</height>
	 </pixmap>
	 <action signal="enter-notify-event">TMP="`readlink '$WORKDIR'/albumart_search'$I'.jpg`"; [ "$TMP" ] && basename "$TMP" | sed "s/.jpg//" > '$WORKDIR'/filebrowser_dir</action>
	 <action signal="enter-notify-event">refresh:SEARCH</action>
	 <action signal="button-press-event">. '$APPDIR'/menu_albumart_search</action>
	 <action signal="button-release-event">refresh:SOURCE</action>
	</eventbox>'
done

GUI_ADD_LIST='
<tree name="sourcelist" rubber-banding="true" fixed-height-mode="true" selection-mode="3" sort-function="1" exported_column="0" space-expand="true" space-fill="true" column-sort-function="0|0|0|0|1|1|0|1|1|0|1|1|0" column-sizing="22|200|150|150|40|60|90||60|80|60|70|" column-resizeable="false|true|" column-header-active="false|true|" column-visible="true|true|true|true|true|true|true|false|true|true|true|true|false|true|false|false|false|false|false|false|false">
 <variable>SOURCE</variable>
 <height>100</height><width>300</width>
 <label>"|'$(gettext 'Artist / Filename')'                             |'$(gettext 'Title')'                             |'$(gettext 'Album')'                     |'$(gettext 'Nr')'  |'$(gettext 'Date')'     |'$(gettext 'Genre ')'     |'$(gettext 'Comment')'  |'$(gettext 'Rate')'|'$(gettext 'Format')'|'$(gettext 'Kb/s')'  |'$(gettext 'Length')'||'$(gettext 'Path')'                                                                                                                                                                                              ||disc-id musicbrainz.org|||||timestamps"</label>
 <input file stock-column="14">'$WORKDIR'/sourcelist</input>
 <output file>'$WORKDIR'/sourcelist_output</output>
 <action signal="button-press-event">if [ $PTR_BTN -eq 3 ]; then echo "$SOURCE" > '$WORKDIR'/SOURCE_PRESS_EVENT; . '$APPDIR'/menu_sourcelist; fi</action>
 <action>'$APPDIR'/func -browse</action>
 <action>'$APPDIR'/func_add -add &</action>
 <action>refresh:UPDATE_SOURCELIST2</action> ##<checkbox> that is TRUE if sourcelist should update, at FALSE - playlist will update
</tree>

<checkbox visible="false">
 <variable>UPDATE_SOURCELIST2</variable>
 <input file>'$WORKDIR'/UPDATE_SOURCELIST2</input>
 <action>if true refresh:SOURCE</action>
 <action>if true refresh:SEARCH</action>
 <action>if true echo false > '$WORKDIR'/UPDATE_SOURCELIST2</action>
 <action>if true refresh:UPDATE_SOURCELIST2</action>
</checkbox>
'

GUI_ADD_FIELD='
<vbox spacing="0" space-expand="true" space-fill="true">
  <vbox height="'$(($ALBUMART_SEARCH_HEIGHT+15))'" spacing="0" margin="0" space-expand="false" space-fill="false">
    <eventbox name="search_albumart" above-child="false" visible-window="true" space-expand="true" space-fill="true">
      <vbox scrollable="true" shadow-type="0" height="'$(($ALBUMART_SEARCH_HEIGHT+15))'" hscrollbar-policy="2" vscrollbar-policy="2" visible="'$SEARCH_ALBUMART'" spacing="0" margin="0" space-expand="false" space-fill="false">
        <eventbox name="search_albumart" above-child="false" visible-window="true" space-expand="true" space-fill="true">
          <hbox spacing="7" space-expand="true" space-fill="true">
            <eventbox name="search_albumart" above-child="true" visible-window="true" space-expand="false" space-fill="false">
              <pixmap>
                <variable>ALBUMART_SEARCH_PREV</variable>
                <input file>'$WORKDIR'/albumart_search_prev.svg</input>
                <height>30</height>
              </pixmap>
              <action signal="button-press-event">'$APPDIR'/func -search_albumart_update prev</action>
              <action signal="button-press-event" type="activate">ALBUMART_SEARCH_ACTIVATE</action>
            </eventbox>
            '$XML_ALBUMART_SEARCH'
            <eventbox name="search_albumart" above-child="true" visible-window="true" space-expand="false" space-fill="false">
              <pixmap>
                <variable>ALBUMART_SEARCH_NEXT</variable>
                <input file>'$WORKDIR'/albumart_search_next.svg</input>
                <height>30</height>
              </pixmap>
              <action signal="button-press-event">'$APPDIR'/func -search_albumart_update next</action>
              <action signal="button-press-event" type="activate">ALBUMART_SEARCH_ACTIVATE</action>
            </eventbox>
            <text space-expand="true" space-fill="true"><label>""</label></text>
          </hbox>
        </eventbox>
      </vbox>
      <action signal="enter-notify-event">cp -f '$WORKDIR'/filebrowser_dir '$WORKDIR'/tmp_filebrowser_dir</action>
      <action signal="leave-notify-event">cp -f '$WORKDIR'/tmp_filebrowser_dir '$WORKDIR'/filebrowser_dir</action>
      <action signal="leave-notify-event">refresh:SEARCH</action>
##    <action signal="button-press-event">. '$APPDIR'/menu_albumart_search disabled</action>
    </eventbox>
    <variable>ALBUMART_SEARCH</variable>
  </vbox>
  <vbox space-expand="true" space-fill="true">
    '$GUI_ADD_LIST'
  </vbox>
</vbox>
'