#!/bin/bash

gui () {(
	case $1 in
		--grabber )
			eval zenity --list --height=333 --title \"Avatar Factory\" --text \"Select a grabber:\" --window-icon="$app_icon" --print-column=2 --column "?" --column Grabbers\
			$(avatar-factory -lg | grep -v avatar)
		;;
		--theme )
			avatar-factory -lt | zenity --list --height=333 --title "Avatar Factory" --text "Select a theme:" --window-icon="$app_icon" --column "Themes"
		;;
		--name )
			zenity --list --height=219 --title "Avatar Factory" --text "Select a name scheme:" --window-icon="$app_icon" --column '#' --column "Description" 0 "no name" 1 "number" 2 "title" 3 "number.title"
		;;
		--type )
			zenity --list --height=219 --title "Avatar Factory" --text "Select the mode:" --window-icon="$app_icon" --column '#' --column "Description" 0 "Act like a folder (nautilus)" 1 "Act like a Launcher"
		;;
		--rating )
			avatar-factory -lrt | zenity --list --height=333 --title "Avatar Factory" --text "Select a rating theme:" --window-icon="$app_icon" --column "Themes"
		;;
		--icons-size )
			[[ $icon_size ]] || icon_size=0
			zenity --scale --width=333 --title "Icon Size" --text " 0 = disable\n10 = normal\n27 = original (128px)" --max-value=99 --value=$icon_size --window-icon="$app_icon"
		;;
		--select-folders )
			if [[ $SOURCE_type = bookmarks ]]; then
				source=$(zenity --list --height=239 --title "Avatar Factory" --text "Select an option:" --window-icon="$app_icon" --column "Sources" $([[ $account_grabber ]] && echo account) "firefox" "firefox 3" "opera" "epiphany")

				if [[ $source = "firefox 3" ]]; then
					zenity --info --title Instructions --text "In firefox 3 address bar type \"about:config\" Find the setting \"browser.bookmarks.autoExportHTML\" double click it to change the boolean entry to true."
					echo firefox
				elif [[ $source = account ]]; then
					echo ~$(zenity --entry --title "Account" --text "Type your user name:" --entry-text="user_name" --window-icon="$app_icon")
					
				else
					echo $source
				fi
			else
				zenity --file-selection --directory --title "Select source folder" --window-icon="$app_icon"
			fi

		;;
		--select-files )
			SELECT_FILES=$(zenity --file-selection --multiple --title "Select source files" --window-icon="$app_icon")
			if [[ $? = 1 ]]; then
				exit 1
			fi
			echo "$SELECT_FILES" | tr '\|' '\n'
		;;
		--select-destination )
			zenity --file-selection --directory --title "Destination Folder" --window-icon="$app_icon"
		;;
		--progress-folders )
			zenity --progress --pulsate --width=333 --title "Avatar Factory" --text "Building list of folders to process..."  --auto-close --window-icon="$app_icon"
		;;
		--progress )
			zenity --progress --width=333 --title "Avatar Factory" --text "Making Avatars..." --window-icon="$app_icon"
		;;
		--select-picture )
			zenity --entry --title "$avatar_name" --text "Drag and drop a picture (file or URL):" --width 300 --window-icon="$app_icon"
		;;
		--error-picture )
			zenity --error --title "$avatar_name" --text "The file does not exist: $grabber_picture"
		;;
		--avatar-factory-old )
			AVATAR_FACTORY=$(find -L $app_config/grabbers/ -maxdepth 1 -type f | sed -e "s|$app_config/grabbers/||g" -e /~/d | sort | sed 's/^/\n/' | zenity --list  --checklist --height=244 --title "Avatar Factory" --text "Select nothing to add/edit" --window-icon="$app_icon" --column \# --column grabbers)
			if [[ $? = 1 ]]; then
				exit 1
			fi
			echo "$AVATAR_FACTORY" | sed 's/|/ /g'
		;;
		--avatar-factory )
			AVATAR_FACTORY=$(avatar-factory -lg | grep -v avatar | sed 's/^/\n/' | zenity --list  --checklist --height=333 --title "Avatar Factory" --text "Select grabber/s:" --window-icon="$app_icon" --column \# --column grabbers)
			if [[ $? = 1 ]]; then
				exit 1
			fi
			echo "$AVATAR_FACTORY" | sed 's/|/ /g'
		;;
		--configuration )
		    #Grabber conf:
			. $avatar_factory_PATH/grabbers/$grabber_name
			grabber_primary_options

		    #Launcher conf:
			eval $(grep 'launcher=' $avatar_factory_PATH/launchers/$grabber_name | head -n 1)

		    #User grabber conf:
			[[ -f $app_config/grabbers/$grabber_name ]] && . $app_config/grabbers/$grabber_name

		    #User general conf:
			[[ $SOURCE_type = bookmarks && -f $app_config/general ]] && . $app_config/general && [[ -z $SOURCE_input ]] && SOURCE_input=$bookmarks
			
		    #GUI:
			[[ -n $set_name ]] && avatar_name=$set_name

			save=yes
			option=null
			while [ "$option" != "" ]; do

				option=$(eval zenity --list --height=333 --width=333 --title "$grabber_name"\
					--text \"Chose an options to edit:\"\
					--window-icon="$app_icon"\
					--column options --column values\
					Theme $theme_name\
					$([[ $SOURCE_type = folders ]] && echo Type \"$(echo $Link_or_Application | sed -e 's/Application/Act as a launcher/' -e 's/Link/Act as a folder/')\")\
					$([[ $Link_or_Application = Application ]] && echo Launcher \"$launcher\")\
					\"Name Scheme\" \"$(echo $avatar_name | sed -e 's/0/no name/' -e 's/1/number/' -e 's/2/title/' -e 's/3/number.title/')\"\
					\"Recreate folders\" \"$(echo $folders | sed -e 's/^$/No/' -e 's/2/Yes, from level 2/' -e 's/1/Yes/')\"\
					\"Delete broken\" \"$([[ $delete = yes ]] && echo Yes || echo No)\"\
					\"Icons size\" \"$icon_size\"\
					Source \"$SOURCE_input\"\
					Destination \"${SOURCE_input[1]}\"\
					$([[ $grabber_name = music ]] && echo Widget $music_widget))

				if [[ $? = 1 ]]; then
					save=no
					break
				fi

				case $option in
					Theme )			theme_name_back=$theme_name
								theme_name=$(gui --theme)
								[[ $theme_name ]] || theme_name=$theme_name_back
					;;
					Type )			[[ $Link_or_Application = Link ]] && Link_or_Application=Application || Link_or_Application=Link
					;;
					Launcher )		launcher_back=$launcher
								launcher=$(zenity --entry --title "Launcher" --text "Takes effect immediately" --entry-text="$launcher" --window-icon="$app_icon")
								[[ $launcher ]] || launcher=$launcher_back
					;;
					"Name Scheme" )		avatar_name_back=$avatar_name
								avatar_name=$(gui --name)
								[[ $avatar_name ]] || avatar_name=$avatar_name_back
					;;
					"Recreate folders" )	folders_back=$folders
								folders=$(zenity --list --height=219 --title "Avatar Factory" --text "Recreate folder structure?" --window-icon="$app_icon" --column "Options" "No" "Yes" "Yes, from level 2" | sed -e 's/Yes, from level 2/2/' -e 's/Yes/1/')
								if [[ -z $folders ]]; then
									folders=$folders_back
								elif [[ $folders = No ]]; then
									folders=
								fi
					;;
					"Delete broken" )	[[ $delete ]] && delete= || delete=yes
					;;
					"Icons size" )		icon_size_back=$icon_size
								icon_size=$(gui --icons-size)
								[[ $icon_size ]] || icon_size=$icon_size_back
					;;
					Source )		source_back=$SOURCE_input
								SOURCE_input=$(gui --select-folders)
								[[ -z "$SOURCE_input" ]] && SOURCE_input=$source_back
					;;
					Destination )		folder2save_back=${SOURCE_input[1]}
								SOURCE_input[1]=$(gui --select-destination)
								[[ -z "${SOURCE_input[1]}" ]] && SOURCE_input[1]=$folder2save_back
					;;
					Widget )		[[ $music_widget = yes ]] && music_widget=no || music_widget=yes
					;;
					"" )
								if [[ -z "${SOURCE_input[1]}" ]]; then
									zenity --warning --text "You must set the Destination"
									option=null
								elif [[ -z "$SOURCE_input" ]]; then
									zenity --warning --text "You must set the Source"
									option=null
								fi
				esac
			done

		    #Save config:
			if [[ $SOURCE_type = bookmarks ]]; then
				{
					grep -v "bookmarks=" $app_config/general
					echo "bookmarks=$SOURCE_input"
				} | sort > $app_config/general

				SOURCE_input=${SOURCE_input[1]}
				SOURCE_input[1]=
			fi


			if [[ $save != no ]]; then
				set_name=$avatar_name

				{
					echo "theme_name=$theme_name"
					echo "Link_or_Application=$Link_or_Application"
					echo "launcher=\"$launcher\""
					echo "set_name=$set_name"
					echo "folders=$folders"
					echo "delete=$delete"
					echo "icon_size=$icon_size"
					echo "SOURCE_input=\"$SOURCE_input\""
					[[ ${SOURCE_input[1]} ]] && echo "SOURCE_input[1]=\"${SOURCE_input[1]}\""
					[[ ${SOURCE_input[2]} ]] && echo "SOURCE_input[2]=\"${SOURCE_input[2]}\""
					[[ $grabber_name = music && $mpd_music_directory ]] && echo "mpd_music_directory=\"$mpd_music_directory\""
					[[ $grabber_name = music && $mpd_port ]] && echo "mpd_port=$mpd_port"
					[[ $grabber_name = music && $mpd_host ]] && echo "mpd_host=$mpd_host"
					[[ $grabber_name = music && $music_widget ]] && echo "music_widget=$music_widget"
					[[ $grabber_name = music && $widget_name ]] && echo "widget_name=$widget_name"
					[[ $grabber_name = mame ]] && echo "mame_snaps=\"$mame_snaps\""

				} > "$app_config/grabbers/$grabber_name"

				exit 0
			else
				exit 1
			fi
		;;
		--main )
			if [[ -d $app_config/grabbers ]]; then

				while true; do
					not_configured=
					tasks=( $(gui --avatar-factory) )
					if [[ $? = 1 || -z $tasks ]]; then
						exit
				    #configure:
					elif [[ ${#tasks[*]} -gt 1 ]]; then
						for i in ${tasks[*]} ; do
							[[ -f $app_config/grabbers/$i ]] || not_configured=( ${not_configured[*]} $i )
						done

						if [[ $not_configured ]]; then
							zenity --question --text "The following grabber/s must be configured:\n${not_configured[*]}"
							[[ $? = 1 ]] &&	continue

							for i in ${not_configured[*]} ; do
								grabber_name=$i
								gui --configuration
								[[ $? = 1 ]] &&	continue 2
							done
						fi

						zenity --question --text "Launch this grabbers?:\n${tasks[*]}"
						if [[ $? = 0 ]]; then
							break
						fi
					else
						grabber_name=$tasks
						gui --configuration
						[[ $? = 1 ]] &&	continue

						zenity --question --text "Do you want to build the avatars?"
						if [[ $? = 0 ]]; then
							break
						fi
					fi
				done

			    #run:
				[[ -f $app_config/report ]] && rm $app_config/report
				total=${#tasks[*]}

				for i in ${tasks[*]} ; do
					num=$(( num+1 ))

					if [[ -f "$avatar_factory_PATH/grabbers/$i" ]]; then
						avatar-factory -g $i --config $app_config/grabbers/$i --general-progress | zenity --progress --auto-close --width 333 --title "Avatar Factory" --text "Processing \"$i\" grabber: $line $num/$total" --window-icon="$app_icon"
						if [[ $? != 0 ]]; then
							exit 1
						fi
					else
						zenity --error --title "Avatar Factory" --text "grabber does not exist: $i" --window-icon="$app_icon"
						rm $app_config/grabbers/$i
					fi

				done

			    #report:
				if [[ $? = 1 ]]; then
					exit 1
				elif [[ -f $app_config/report ]]; then
					zenity --list --title "Avatar Factory" --text "All done:" --width=333 --height=333 --window-icon="$app_icon" --column grabber --column key --column "#" $(cat $app_config/report)
				else
					zenity --error --title "Avatar Factory" --text "Something went wrong" --window-icon="$app_icon"
				fi

		    #first time:
			elif [[ -d $app_config/gui  ]]; then
				rm $app_config/gui/*
				rmdir $app_config/gui
				zenity --info --title Welcome --text "The old config is not compatible.\nYou will have to configure the grabbers again with the new and improved gui.\n Good luck :)"
				mkdir -p $app_config/grabbers
				gui --configuration
				gui --main
			else
				zenity --info --title Welcome --text "It seems to be the first time you run the new gui.\nNow you must first configure the grabbers and then you will be able to run many at the same time."
				mkdir -p $app_config/grabbers
				gui --configuration
				gui --main
			fi
	esac
)}
