#!/bin/sh
export TEXTDOMAIN=pmusic
set -a

#override main pMusic settings
gettext 'Search radio station' > $WORKDIR/filebrowser_dir

#override settings in pmusicrc to search only radio stations
export SEARCH_MYMUSIC=false
export SEARCH_COLLECTIONS=false
export SEARCH_RADIO=true
export SEARCH_ALBUMART=false
export SEARCH_WEBMUSIC=false
export SEARCH_FILES=false

. $APPDIR/gui_add
. $APPDIR/gui_playlist #svg nowplaying
. $APPDIR/gui_misc
. $APPDIR/gui_navigation
. $APPDIR/gui_mixer

[ ! -s "$STORAGE_DIR"/index_radio ] && $APPDIR/func_radio
$APPDIR/func_add -add pmusic_radio_country
echo '(00:00)' > $WORKDIR/statusbar_playlist #this holds total time of all added tracks to playqueue
echo false > $WORKDIR/UPDATE_GUI

#autoplay if played radio last time
if [ -s $WORKDIR/PLAYLIST.radio ] && [ "`grep "$(<$WORKDIR/PLAYLIST.radio)" "$STORAGE_DIR/index_radio"`" ]; then
	$APPDIR/func_store_playlist &
	sleep 0.2
	cp $WORKDIR/PLAYLIST.radio $WORKDIR/PLAYLIST
	cp $WORKDIR/PLAYLIST_ALL.radio $WORKDIR/PLAYLIST_ALL
	cp $WORKDIR/playlist.radio $WORKDIR/playlist
	$APPDIR/func_player -play -autoplay "$(<$WORKDIR/PLAYLIST)"
fi

#get rid of tree header
echo '<tree name="sourcelist" headers-visible="false" fixed-height-mode="true" selection-mode="1" sort-function="1" exported_column="0" space-expand="true" space-fill="true" column-sizing="22|200|150|150|40|60|90||60|80|60|70|" column-visible="true|true|true|true|true|true|true|false|true|true|true|true|false|true|false|false|false|false|false|false|false">' > $WORKDIR/tmp.Radio
echo "$GUI_ADD_LIST" | grep -vF '<tree name="sourcelist"' >> $WORKDIR/tmp.Radio
GUI_ADD_LIST="$(<$WORKDIR/tmp.Radio)"

GUI='
<window title="pMusic - '$(gettext 'Radio streamer')'" icon-name="pmusic" default_height="450" default_width="450">
<vbox space-expand="true" space-fill="true">
  <vbox space-expand="false" space-fill="false">
    <hbox space-expand="false" space-fill="false">
      '"`grep -B1 -A100 -F 'HISTORY_UNDO' <<< "$GUI_ADD_SEARCH" | grep -B100 -Fm1 '</entry>'`"'
      <button tooltip-text="'$(gettext 'Browse radio stations by country')'">
        <label>'$(gettext 'Country')'</label>
        <action>'$APPDIR'/func_add -add pmusic_radio_country</action>
        <action>refresh:SOURCE</action>
      </button>
      <button tooltip-text="'$(gettext 'Browse radio stations by genre')'">
        <label>'$(gettext 'Genre')'</label>
        <action>'$APPDIR'/func_add -add pmusic_radio_genre</action>
        <action>refresh:SOURCE</action>
      </button>
      <button>
        <input file>'$APPDIR'/themes/'$THEME'/icon_favorites.svg</input>
        <height>22</height>
        <action>echo -e "pmusic_favorite_lists pmusic_favorite_marks .Radio|__ '$(gettext 'F A V O R I T E S')' __" > '$WORKDIR'/sourcelist; grep -F "gtk-audio-url" "'$STORAGE_DIR'/favorites" > '$WORKDIR'/tmp_favorites; '$APPDIR'/func_rating -buildlist 1 '$WORKDIR'/tmp_favorites | sort -rnst"|" -k14,14 >> '$WORKDIR'/sourcelist; echo "pmusic_rating|" >> '$WORKDIR'/sourcelist</action>
        <action>refresh:SOURCE</action>
      </button>
    </hbox>
  </vbox>
  <vbox space-expand="true" space-fill="true">
    '$GUI_ADD_LIST'
  </vbox>
  <vbox space-expand="false" space-fill="false">
    <hbox space-expand="true" space-fill="true">
      <hbox space-expand="true" space-fill="true">
        '$GUI_SVG_NOWPLAYING'
      </hbox>
      <vbox space-expand="false" space-fill="false">
        <hbox space-expand="false" space-fill="false">
          <button>
            <input file>'$APPDIR'/themes/'$THEME'/icon_play.svg</input>
            <height>22</height>
            <action>'$APPDIR'/func_player -stop; '$APPDIR'/func_player -play</action>
          </button>
          <button>
            <input file>'$APPDIR'/themes/'$THEME'/icon_stop.svg</input>
            <height>22</height>
            <action>'$APPDIR'/func_player -stop</action>
          </button>
          <button tooltip-text="'$(gettext 'Record radio stream into seperated tracks')'">
            <input file stock="gtk-media-record"></input>
            <action>'$APPDIR'/func_radiohits</action>
          </button>
          <button><input file stock="gtk-preferences"></input><action>'$APPDIR'/func_config -radio</action></button>
        </hbox>
        <hbox height-request="16" space-expand="true" space-fill="true">
          <text width-request="12" space-expand="false" space-fill="false"><label>""</label></text>
          <pixmap space-expand="false" space-fill="false">
            <input file>'$APPDIR'/themes/'$THEME'/volume.svg</input>
            <height>14</height>
          </pixmap>
          '$GUI_VOLUME'
        </hbox>
      </vbox>
    </hbox>
  </vbox>
  <vbox visible="false" space-expand="false" space-fill="false">
    '$GUI_PROGRESSBAR'
    '$GUI_PROGRESS'
  </vbox>
  <vbox space-expand="false" space-fill="false">
    '$GUI_STATUSBAR'
  </vbox>
</vbox>
<action signal="show">'$APPDIR'/func_mixer -get_levels</action>
<action signal="show">refresh:VOLUME</action>
<action signal="show">echo > '$WORKDIR'/gui_is_running</action>
<action signal="delete-event">cp '$WORKDIR'/PLAYLIST '$WORKDIR'/PLAYLIST.radio; cp '$WORKDIR'/PLAYLIST_ALL '$WORKDIR'/PLAYLIST_ALL.radio; cp '$WORKDIR'/playlist '$WORKDIR'/playlist.radio</action>
<action signal="delete-event">'$APPDIR'/func -quit</action>
<action signal="delete-event">exit:Exit</action>
</window>'

#echo "$GUI" > /root/gtk
