#!/bin/bash
#
#Copyright 2012 - 2013 CurlyMo & Hexagon & mk01 <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/>

#[ -z "$DEFAULTWIDTH" ] && DEFAULTWIDTH=55
[ -z "$DEFAULTWIDTH" ] && DEFAULTWIDTH=55
DEFAULTHEIGHT=9;
DEFAULTCOLSEP="!"
DEFAULTDELIMITER="π"

# Show a normal dialog (messagebox)
#  $1 Text
#  $2 (optional) Height
#  $3 (optional) Width
#  $4 (optional) OK Label
showMsgDlg() {
	OKLABEL="$(_ dialog.close)"

	# Default width and height
	HEIGHT=$DEFAULTHEIGHT
	WIDTH=$DEFAULTWIDTH
	# Override width and height
	if [ $# -gt 1 ]; then
		HEIGHT=$2
	fi
	if [ $# -gt 2 ]; then
		WIDTH=$3
	fi
	if [ $# -gt 3 ]; then
		OKLABEL=$4
	fi
	
	# Show messagebox
	dialog \
		--backtitle "$BACKTITLE" \
		--title "$TITLE" \
		--ok-label "$OKLABEL" \
		--msgbox "$1" $HEIGHT $WIDTH 
}

# Show a information box
#  $1 Text
#  $2 (optional) Height
#  $3 (optional) Width
showInfoDlg() {
	# Default width and height
	HEIGHT=$DEFAULTHEIGHT
	WIDTH=$DEFAULTWIDTH
	# Override width and height
	if [ $# -gt 1 ]; then
		HEIGHT=$2
	fi
	if [ $# -gt 2 ]; then
		WIDTH=$3
	fi
	
	# Show messagebox
	dialog \
		--backtitle "$BACKTITLE" \
		--title "$TITLE" \
		--infobox "\n$1" $HEIGHT $WIDTH 
}

# Show a text dialog
#  $1 File
#  $2 (optional) Height
#  $3 (optional) Width
#  $4 (optional) Close label
showTextDlg() {
	CLOSELABEL="$(_ dialog.close)"

	# Default width and height
	HEIGHT=$DEFAULTHEIGHT
	WIDTH=$DEFAULTWIDTH
	# Override width and height
	if [ $# -gt 1 ]; then
		HEIGHT=$2
	fi
	if [ $# -gt 2 ]; then
		WIDTH=$3
	fi
	if [ $# -gt 3 ]; then
		CLOSELABEL=$4
	fi
	
	# Show messagebox
	dialog \
		--backtitle "$BACKTITLE" \
		--title "$TITLE" \
		--ok-label "$CLOSELABEL" \
		--textbox "$1" $HEIGHT $WIDTH 
}

# Show a yes/no dialog
#  $1 Text
#  $2 (optional) Height
#  $3 (optional) Width
#  $4 (optional) Yes label
#  $5 (optional) No label
showConfirmDlg() {
	YESLABEL="$(_ dialog.yes)"
	NOLABEL="$(_ dialog.no)"

	# Default width and height
	HEIGHT=$DEFAULTHEIGHT
	WIDTH=$DEFAULTWIDTH
	
	# Override width and height
	if [ $# -gt 1 ]; then
		HEIGHT=$2
	fi
	if [ $# -gt 2 ]; then
		WIDTH=$3
	fi
	if [ $# -gt 3 ]; then
		YESLABEL=$4
	fi
	if [ $# -gt 4 ]; then
		NOLABEL=$5
	fi
	# Show yes/no dialog
	dialog \
		--backtitle "$BACKTITLE" \
		--title "$TITLE" \
		--yes-label $YESLABEL \
		--no-label $NOLABEL \
		--yesno "$1" $HEIGHT $WIDTH 
}

#Show menu
#  $1 Descriptive text
#  $2 Menu items
#  $3 (optional) Rows
#  $4 (optional) Height
#  $5 (optional) Width
#  $6 (optional) Select label
#  $7 (optional) Return Label
function showMenuDlg() {
	YESLABEL="$(_ dialog.select)"
	NOLABEL="$(_ dialog.return)"	
	
	if [ $# -gt 2 ]; then
		ROWS=$3;
	else
		ROWS=5;
	fi
	HEIGHT=$(($ROWS+5))
	WIDTH=$(($DEFAULTWIDTH+11));

	if [ $# -gt 3 ]; then
		HEIGHT=$4
	fi
	if [ $# -gt 4 ]; then
		WIDTH=$5
	fi
	if [ $# -gt 5 ]; then
		YESLABEL=$6
	fi
	if [ $# -gt 6 ]; then
		NOLABEL=$7
	fi

        IFS=$DEFAULTDELIMITER;
	RETURN=$( \
		dialog \
			--no-kill \
			--backtitle "$BACKTITLE" \
			--title "$TITLE" \
			--ok-label $YESLABEL \
			--colors \
			--cancel-label $NOLABEL \
			--column-separator "$DEFAULTCOLSEP" \
                        --no-collapse \
			--menu "\n$1" $HEIGHT $WIDTH $ROWS \
			$2 3>&1 1>&2 2>&3)
}

# Show radio list
#  $1 Descriptive text
#  $2 Items
#  $3 Rows
#  $4 (optional) Height
#  $5 (optional) Width
#  $6 (optional) Select label
#  $7 (optional) Return label
function showRadioDlg() {
	YESLABEL="$(_ dialog.select)"
	NOLABEL="$(_ dialog.return)"
	HEADER=$1;

	if [ $# -gt 2 ]; then
		ROWS=$3;
	else
		ROWS=5;
	fi
	if [ $# -gt 4 ]; then
		WIDTH=$5
	else
		WIDTH=$(($DEFAULTWIDTH+15))
	fi
	centerTxtFn "$(_ dialog.space_option_select)" $(($WIDTH-4))

	HEIGHT=$(($DEFAULTHEIGHT+5));
	if [ $# -gt 3 ]; then
		HEIGHT=$4
	fi
	if [ $# -gt 5 ]; then
		YESLABEL=$6
	fi
	if [ $# -gt 6 ]; then
		NOLABEL=$7
	fi

	IFS=$DEFAULTDELIMITER;
	RETURN=$( \
		dialog \
			--no-kill \
			--ok-label $YESLABEL \
			--colors \
			--cancel-label $NOLABEL \
			--title "$TITLE" \
			--backtitle "$BACKTITLE" \
			--column-separator "$DEFAULTCOLSEP" \
			--no-collapse \
			--radiolist "\n$CENTEREDTXT\n  $HEADER" $HEIGHT $WIDTH $ROWS \
			$2 3>&1 1>&2 2>&3);
}

# Show checklist
#  $1 Descriptive text
#  $2 Items
#  $3 Rows
#  $4 (optional) Height
#  $5 (optional) Width
#  $6 (optional) Select label
#  $7 (optional) Return Label
#  $8 (optional) Opt. label
function showChecklistDlg() {
	YESLABEL="$(_ dialog.select)"
	NOLABEL="$(_ dialog.return)"
	OPTLABEL=0;
	HEADER=$1;

	if [ $# -gt 2 ]; then
		ROWS=$3;
	else
		ROWS=5;
	fi
	WIDTH=$(($DEFAULTWIDTH+20))
	if [ $# -gt 4 ]; then
		WIDTH=$5
	fi
	
	centerTxtFn "$(_ dialog.space_check_select)" $(($DEFAULTWIDTH+16));
	HEIGHT=$(($DEFAULTHEIGHT+5));
	if [ $# -gt 3 ]; then
		HEIGHT=$4
	fi
	if [ $# -gt 5 ]; then
		YESLABEL=$6
	fi
	if [ $# -gt 6 ]; then
		NOLABEL=$7
	fi
	if [ $# -gt 7 ]; then
		OPTLABEL="$8"
	fi

	IFS=$DEFAULTDELIMITER;
	RETURN=$( \
		dialog \
			--no-kill \
			--ok-label $YESLABEL \
			--colors \
			--cancel-label $NOLABEL \
			--title "$TITLE" \
			--backtitle "$BACKTITLE" \
			--column-separator "$DEFAULTCOLSEP" \
			--no-collapse \
			--checklist "\n$CENTEREDTXT\n  $HEADER" $HEIGHT $WIDTH $ROWS \
			$2 3>&1 1>&2 2>&3);
}

# Show input dialog
#  $1 Descriptive text
#  $2 Value
#  $3 (optional) Height
#  $4 (optional) Width
#  $5 (optional) Change Label
#  $6 (optional) Cancel Label
function showInputDlg() {
	CHANGELABEL="$(_ dialog.change)"
	CANCELLABEL="$(_ dialog.cancel)"
	VALUE=$2;
	WIDTH=$DEFAULTWIDTH;
	HEIGHT=$(($DEFAULTHEIGHT+1))
	
	if [ $# -gt 2 ]; then
		HEIGHT=$3
	fi
	if [ $# -gt 3 ]; then
		WIDTH=$4
	fi
	if [ $# -gt 4 ]; then
		CHANGELABEL=$5
	fi
	if [ $# -gt 5 ]; then
		CANCELLABEL=$6
	fi

	RETURN=$( \
		dialog \
			--backtitle "$BACKTITLE" \
			--title "$TITLE" \
			--ok-label $CHANGELABEL \
			--cancel-label $CANCELLABEL \
			--inputbox "$1" $HEIGHT $WIDTH $VALUE \
			3>&1 1>&2 2>&3);
}

# Show password dialog
#  $1 Descriptive text
#  $3 (optional) Height
#  $4 (optional) Width
#  $5 (optional) Change Label
#  $6 (optional) Cancel Label
function showPassDlg() {
	WIDTH=$DEFAULTWIDTH;
	HEIGHT=$(($DEFAULTHEIGHT+2))
	CHANGELABEL=$(_ dialog.change)
	CANCELLABEL=$(_ dialog.cancel)
	
	if [ $# -gt 2 ]; then
		HEIGHT=$3
	fi
	if [ $# -gt 3 ]; then
		WIDTH=$4
	fi
	if [ $# -gt 4 ]; then
		CHANGELABEL=$5
	fi
	if [ $# -gt 5 ]; then
		CANCELLABEL=$6
	fi

	VALUES=$(dialog \
			--ok-label "$CHANGELABEL" \
			--cancel-label "$CANCELLABEL" \
			--backtitle "$BACKTITLE" \
			--title "$TITLE" \
			--insecure \
			--passwordform "$1" \
			$HEIGHT $WIDTH 0 \
			"$(_ dialog.password)" 1 3 "" 1 26 21 0 \
			"$(_ dialog.confirm)"  2 3 "" 2 26 21 0 \
		3>&1 1>&2 2>&3);
}

# Standard error dialog
function showErrorDlg() {
	centerTxtFn "$(_ dialog.unexpected_error)" $(($DEFAULTWIDTH-4))
	valignTxtFn "$CENTEREDTXT"
	showMsgDlg "$ALIGNEDTXT" $DEFAULTHEIGHT;
}

function showLoadingDlg() {
	centerTxtFn "$(_ dialog.loading_module)" $(($DEFAULTWIDTH-4))
	valignTxtFn "$CENTEREDTXT"
	showInfoDlg "$ALIGNEDTXT" $DEFAULTHEIGHT;
}

# Show an mixed form dialog
#  $1 Descriptive text
#  $2 (optional) Height
#  $3 (optional) Width
#  $4 (optional) Change label
#  $5 (optional) Cancel Label
function showInputFormDlg() {
	CHANGELABEL="$(_ dialog.change)"
	CANCELLABEL="$(_ dialog.cancel)"

	HEIGHT=$(($DEFAULTHEIGHT+3))
	WIDTH=$DEFAULTWIDTH;

	if [ $# -gt 1 ]; then
		HEIGHT=$2
	fi
	if [ $# -gt 2 ]; then
		WIDTH=$3
	fi
	if [ $# -gt 3 ]; then
		CHANGELABEL=$4
	fi
	if [ $# -gt 4 ]; then
		CANCELLABEL=$5
	fi	

	CONTENT=();
	I=0;
	X=0;
	for VALUE in "${ELEMENTS[@]}"; do
		IFS=",";
		ELEMENT=($VALUE);
		if [ $X -ne ${ELEMENT[0]} ]; then
			X=${ELEMENT[0]}
			I=$(($I+1));
		fi
		CONTENT+=("${ELEMENT[2]}" "${ELEMENT[0]}" "${ELEMENT[1]}" "${ELEMENT[3]}" "${ELEMENT[0]}" "${ELEMENT[4]}" "${ELEMENT[5]}" "${ELEMENT[6]}" "${ELEMENT[7]}");
	done;

	# Limit text (lines and length) so it can be displayed properly
	local R=$(stty size | awk '{print $1}'); local C=$(stty size | awk '{print $2}')
	local T=$(echo -ne "$1" | sed "$((R-I-7))q;s/^\(.\{$((C-6))\}\).*/\1/g")

	RETURN=$(dialog \
			--backtitle "$BACKTITLE" \
			--title "$TITLE" \
			--ok-label "$CHANGELABEL" \
			--cancel-label "$CANCELLABEL" \
			--mixedform "$T" $HEIGHT $WIDTH $I "${CONTENT[@]}" \
			3>&1 1>&2 2>&3);
}
