/*
 * Copyright (c) 2000, 2001 Red Hat, Inc.
 * Copyright (c) 2003 Robert Collins <rbtcollins@hotmail.com>
 * Copyright (c) 2005-2006 Tom N Harris <telliamed@whoopdedo.org>
 *
 *     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.
 *
 *     A copy of the GNU General Public License can be found at
 *     http://www.gnu.org/
 *
 * Written by DJ Delorie <dj@cygnus.com>
 * Written by Tom N Harris <telliamed@whoopdedo.org>
 *
 */

#include "win32.h"
#include <commctrl.h>

#include "Choose.h"
#include "PropSheet.h"
#include "resource.h"
#include "globals.h"
#include "dark.h"

//#include "threebar.h"
//#include "Generic.h"
#include "ControlAdjuster.h"

#include "InstallInfo.h"
#include "Installer.h"

using namespace Setup;
using namespace std;

//extern ThreeBarProgressPage Progress;

String ChooserPage::m_gameT1 = String::Load(IDS_DARKGAME_THIEF1);
String ChooserPage::m_gameT2 = String::Load(IDS_DARKGAME_THIEF2);
String ChooserPage::m_gameSS2 = String::Load(IDS_DARKGAME_SSHOCK2);
String ChooserPage::m_gameALL = String::Load(IDS_DARKGAME_ALL);

String ChooserPage::GameString(int game)
{
	if (game == DARKGAME_ALL)
		return m_gameALL;
	if (game & DARKGAME_T1)
	{
		String str = m_gameT1;
		if (game & DARKGAME_T2)
		{
			str += TEXT(",");
			str += m_gameT2;
		}
		if (game & DARKGAME_SS2)
		{
			str += TEXT(",");
			str += m_gameSS2;
		}
		return str;
	}
	else if (game & DARKGAME_T2)
	{
		String str = m_gameT2;
		if (game & DARKGAME_SS2)
		{
			str += TEXT(",");
			str += m_gameSS2;
		}
		return str;
	}
	else if (game & DARKGAME_SS2)
	{
		return m_gameSS2;
	}
	// No flags actually defaults to all games. Go fig.
	return m_gameALL;
}


/*
  Sizing information.
 */
static ControlAdjuster::ControlInfo ChooserControlsInfo[] = {
	{IDC_LISTVIEW_POS, 		CP_RIGHT,   CP_TOP},
	{IDC_CHOOSE_LIST,		CP_STRETCH, CP_STRETCH},
	{0, CP_LEFT, CP_TOP}
};

ChooserPage::ChooserPage(Installer &i)
	: m_installer(i)
{
	m_sizeProcessor.addControlInfo(ChooserControlsInfo);
}

ChooserPage::~ChooserPage()
{
}

void
ChooserPage::createListView()
{
	m_chooser = new PickView();
	RECTWrapper r = getDefaultListViewSize();
	if (!m_chooser->create(this, WS_CHILD | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE,
				&r, MAKEINTRESOURCE(IDC_CHOOSE_LIST)))
		// TODO throw exception
		exit(11);
	m_chooser->insertColumn(String::Load(IDS_PICK_COLUMN_NAME), r.width() / 4);
	m_chooser->insertColumn(String::Load(IDS_PICK_COLUMN_VERSION));
	m_chooser->insertColumn(String::Load(IDS_PICK_COLUMN_GAME));
	m_chooser->show(SW_SHOW);
}

bool
ChooserPage::create()
{
	return PropertyPage::create(IDD_CHOOSE);
}

RECT
ChooserPage::getDefaultListViewSize()
{
	RECT result = getDlgItemRect(IDC_LISTVIEW_POS);
	result.top += 2;
	result.bottom -= 2;
	return result;
}

void
ChooserPage::onInit()
{
	createListView();
	loadInstallInfo();
}

void
ChooserPage::onActivate()
{
	m_chooser->refreshList();
	if (m_chooser->getNumRows() == 0)
	{
		enableNext(true);
		return;
	}
	bool checked = false;
	for (int r = 0; r < m_chooser->getNumRows(); ++r)
	{
		if (m_chooser->getRow(r)->getCheckState() == PickLine::CheckStateChecked)
		{
			checked = true;
			break;
		}
	}
	enableNext(checked);
}

long
ChooserPage::onNext()
{
	if (m_chooser->getNumRows() == 0)
	{
		return IDD_INSTALLNOTHING;
	}
	m_installer.Reset();
	for (int r = 0; r < m_chooser->getNumRows(); ++r)
	{
		PickLine* line = m_chooser->getRow(r);
		if (! line->isContainer()
		   && line->getCheckState() == PickLine::CheckStateChecked)
		{
			m_installer.AddInstall(line->key());
		}
	}
	if (! m_installer.PreCollectFiles(getHWND()))
	{
		return AbortPageChange;
	}
	return NextPage;
}

long
ChooserPage::onBack()
{
	return PreviousPage;
}

bool
ChooserPage::onMessageCmd(int id, HWND hwndctl, UINT code)
{
	if (id == IDC_CHOOSE_LIST && code == VIEW_SELCHANGED)
	{
		bool checked = false;
		for (int r = 0; r < m_chooser->getNumRows(); ++r)
		{
			if (m_chooser->getRow(r)->getCheckState() == PickLine::CheckStateChecked)
			{
				checked = true;
				break;
			}
		}
		enableNext(checked);
		return true;
	}

	// Wasn't recognized or handled.
	return false;
}

void
ChooserPage::loadInstallInfo()
{
	for (size_t s = 0; s < m_installer.numSofts(); ++s)
	{
		const InstallSoftware* soft = m_installer.getSoft(s);
		m_chooser->insertCategory(soft->name());
		for (size_t t = 0; t < soft->numTargets(); ++t)
		{
			const InstallTarget* targ = m_installer.getTarget(soft->getTarget(t));
			if (!targ)
				continue;
			String gamestr = GameString(targ->gameId());
			m_chooser->insertItem(soft->name(), targ->id().c_str(),
					      &targ->name(), &targ->version(), &gamestr);
		}
	}
}

