#!/bin/bash
#
#Copyright 2012 CurlyMo <development@xbian.org>
#
#This file is part of XBian - XBMC on the Raspberry Pi.
#
#XBian is free software: you can redistribute it and/or modify it under the
#terms of the GNU General Public License as published by the Free Software
#Foundation, either version 3 of the License, or (at your option) any later
#version.
#
#XBian is distributed in the hope that it will be useful, but WITHOUT ANY
#WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#details.
#
#You should have received a copy of the GNU General Public License along
#with XBian. If not, see <http://www.gnu.org/licenses/>

#|------------------------------------|
#|          Global variables          |
#|------------------------------------|

# The arguments this module accepts
ARGUMENTS=(list select update date);

#|------------------------------------|
#|          Include files             |
#|------------------------------------|

source $BASEPATH/modules/timezone/functions
if [ $GUIMODE -eq 1 ]; then
	source $BASEPATH/modules/timezone/dialogs
fi

#|------------------------------------|
#|           Main program             |
#|------------------------------------|

TZCATEGORIES=(Africa America Antarctica Australia Arctic Asia Atlantic Europe Indian Pacific SystemV US Etc)

# Executes the GUI version of this module
function showGUIFn() {
	showRetrievingCurrentTZDlg;
	getCurrentTZFn;
	getCurrentDateTimeFn;
	OLDDATETIME=$DATETIME;
	showTZCategoryDlg;
	#Check if an actual selection was made.
	if [ $? -eq 0 ]; then
		if [ -z $RETURN ]; then
			showTZNoTimezoneSelectedDlg;
			showGUIFn;
		else
			TZNEWTIMEZONE=${TZCATEGORIES[$(($RETURN-1))]};
			showTZLoadingLocationsDlg;
			getTZLocationLstFn "$TZNEWTIMEZONE";
			showTZLocationDlg "${CURRENTTZ[*]}" "${TZLOCATIONLST[*]}";
			if [ $? -eq 0 ]; then
				if [ -z $RETURN ]; then
					showTZNoLocationSelectedDlg;
					showGUIFn;
				else			
					TZNEWLOCATION=${TZLOCATIONLST[$(($RETURN-1))]}
					changeCurrentTZFn $TZNEWTIMEZONE $TZNEWLOCATION
					getCurrentDateTimeFn;
					showCurrentDateTimeDlg $DATETIME $OLDDATETIME;
				fi
			else
				showGUIFn;
			fi
		fi
	fi
}

# Executes the command line version of this module
#  $1 Argument [String]
# list
#  $2 Timezone
# update
#  $2 Timezone
#  $3 Location
# Returns
#   0 Failed
#   1 Success
function showCMDFn() {
	case "$1" in
		list)
			shift;
			if [ $# -eq 0 ]; then
				for TZCATEGORY in ${TZCATEGORIES[@]}; do
					echo ${TZCATEGORY,,};
				done;
			elif [ $# -eq 1 ]; then
				checkTZValid $1;
				if [ $? -eq 1 ]; then
					getTZLocationLstFn $1;
					for TZLOCATION in ${TZLOCATIONLST[@]}; do
						echo ${TZLOCATION,,};
					done;
				else
					echo 0;
				fi
			else
				echo 0;
			fi
		;;
		select)
			shift;
			getCurrentTZFn;
			if [ ${#CURRENTTZ[@]} -eq 2 ]; then
				echo ${CURRENTTZ[@],,}
			else
				echo -1;
			fi
		;;
		update)
			shift;
			if [ $# -eq 2 ]; then
				checkTZValid $1;
				if [ $? -eq  1 ]; then
					checkTZLocationValid $1 $2;
					if [ $? -eq 1 ]; then
						changeCurrentTZFn $1 $2
						if [ $? -eq 1 ]; then
							echo 1;
						else
							echo 0;
						fi
					else
						echo 0;
					fi
				else
					echo 0;
				fi
			else
				echo 0;
			fi
		;;
		date)
			shift
			if [ $# -eq 0 ]; then
				getCurrentDateTimeFn;
				echo $DATETIME;
			else
				return 0;
			fi
		;;
	esac
}