// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   This program 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 2 of the License, or
//   (at your option) any later version.
//  
//   This program 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 this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#include "../uijunk/widget.h"
#include "../uijunk/widgetcontainer.h"
#include "../uijunk/widgetbutton.h"
#include "../uijunk/widgettext.h"
#include "../uijunk/widgetpaddedbox.h"
#include "../uijunk/widgetradiogroup.h"
#include "../uijunk/widgetselectchild.h"
#include "../uijunk/widgetscrollable.h"
#include "../uijunk/widgetslider.h"
#include "../uijunk/widgetalignedbox.h"

#include "filewindow.h"
#include "junkeditor.h"

FileWindow::FileWindow(WidgetCallback okay_cb, WidgetCallback cancel_cb, void *param) {

	WidgetAlignedBox *fileSelectRoot = new WidgetAlignedBox();
	
	WidgetPaddedBox *box = new WidgetPaddedBox();
	fileSelectRoot->setChild(box);
	
	box->setBackgroundImage("editordata/np_ff2.ninepatch");
	box->setPadding(32);
	
	WidgetContainer *container = new WidgetContainer();
	box->setChild(container);
	
	container->setNumRows(3);
	container->setNumCols(1);
	container->setStretchableCol(0);
	container->setStretchableRow(0);

	WidgetContainer *fileListView = new WidgetContainer();
	fileListView->setNumCols(2);
	fileListView->setStretchableCol(0);
	fileListView->setStretchableRow(0);
	fileListView->setMinHeight(512);
	fileListView->setMinWidth(256);
	container->addChild(fileListView);
	
	WidgetScrollable *fileListScrollable = new WidgetScrollable();
	fileListView->addChild(fileListScrollable);
    fileListScrollable->setBackgroundImage("editordata/1pxbox1.ninepatch");
	
	fileListContainer = new WidgetContainer();
	fileListContainer->setStretchableRow(-1);
	fileListContainer->setStretchableCol(1);
	fileListScrollable->setChild(fileListContainer);
	
	WidgetSlider *fileListSlider = new WidgetSlider();
	setJunkEditorSliderDefaults(fileListSlider);
	fileListSlider->setSmallStep(32);
	fileListView->addChild(fileListSlider);
	fileListScrollable->setVerticalScrollBar(fileListSlider);
	
	WidgetPaddedBox *fileTextBoxPadding = new WidgetPaddedBox();
	fileTextBoxPadding->setMinHeight(20);
	fileTextBoxPadding->setPadding(2);
	fileTextBoxPadding->setBackgroundImage("editordata/1pxbox3.ninepatch");
	container->addChild(fileTextBoxPadding);
	
	fileTextBox = new WidgetText();
	fileTextBox->setEditable(true);
	fileTextBox->setMinHeight(16);
	fileTextBoxPadding->setChild(fileTextBox);
	
	WidgetContainer *buttonContainer = new WidgetContainer();
	buttonContainer->setNumCols(3);
	buttonContainer->setNumRows(1);
	buttonContainer->setStretchableCol(1);
	container->addChild(buttonContainer);
	
	WidgetButton *noButton = new WidgetButton();
	noButton->setLabel("Cancel");
	noButton->setNativeEventHandler("onClick", cancel_cb, param);
	setJunkEditorButtonDefaults(noButton);

	WidgetButton *yesButton = new WidgetButton();
	yesButton->setLabel("Confirm");
	yesButton->setNativeEventHandler("onClick", okay_cb, param);
	setJunkEditorButtonDefaults(yesButton);

	buttonContainer->addChild(noButton);
	buttonContainer->addChild(NULL);
	buttonContainer->addChild(yesButton);

	rootWidget = fileSelectRoot;
}

FileWindow::~FileWindow(void) {
	delete rootWidget;
}

Widget *FileWindow::getWidget() {
	return rootWidget;
}

void FileWindow::setDefaultFileName(const std::string &name) {
	fileTextBox->setText(name);
}

static bool fileListButtonClick(Widget *w, void *param) {
	FileWindow *window = (FileWindow*)param;
	WidgetButton *button = (WidgetButton*)w;
	
	window->setDefaultFileName(button->getLabel());
	
	return false;
}

void FileWindow::setFileList(const std::vector<std::string> &list) {

	clearFileList();
	
	for(unsigned int i = 0; i < list.size(); i++) {
		
		WidgetButton *textLine = new WidgetButton();
		
		//setJunkEditorButtonDefaults(textLine);
		textLine->setBackgroundImage("editordata/1pxbox1.ninepatch");
		textLine->setBackgroundDefault("editordata/1pxbox1.ninepatch");
		textLine->setBackgroundActive("editordata/1pxbox2.ninepatch");
		textLine->setBackgroundMouseOver("editordata/1pxbox3.ninepatch");
		
		textLine->setLabel(list[i]);
		textLine->setMinWidth(256);
		textLine->setLabelPadding(0);
		textLine->setLabelHAlign(-1);
		textLine->setNativeEventHandler("onClick", fileListButtonClick, this);
		fileListContainer->addChild(textLine);
		
	}
	
	int numRows = fileListContainer->getNumChildren();
	if(numRows == 0) numRows = 1;
	
	fileListContainer->setNumRows(numRows);	
}

std::string FileWindow::getSelectedFile(void) {
	return fileTextBox->getText();
}

void FileWindow::clearFileList(void) {
	fileListContainer->clearChildren();
}

void FileWindow::clearSelectedOption(void) {
	fileTextBox->setText("");
}
