/*
 * Copyright (c) 2001, Gary R. Van Sickle.
 * Copyright (c) 2005-2006, Tom N Harris.
 *
 *     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 Gary R. Van Sickle <g.r.vansickle@worldnet.att.net>
 * Written by Tom N Harris <telliamed@whoopdedo.org>
 *
 */

// This is the implementation of the ProgressPage class.  It is a fairly generic
// progress indicator property page.

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

#include "Installer.h"

#include "Progress.h"
#include "resource.h"

#include "PropSheet.h"
#include "ControlAdjuster.h"

using namespace Setup;

/*
  Sizing information.
 */
static ControlAdjuster::ControlInfo ProgressControlsInfo[] = {
	{IDC_INS_ACTION, 	CP_LEFT,    CP_TOP},
	{IDC_INS_DEST, 		CP_LEFT,    CP_TOP},
	{IDC_INS_FILE, 		CP_LEFT,    CP_TOP},
	{IDC_INS_IPROGRESS, 	CP_STRETCH, CP_TOP},
	{0, CP_LEFT, CP_TOP}
};

ProgressPage::ProgressPage(Installer & i)
	: m_installer(i),
	  m_action(NULL), m_destname(NULL), m_filename(NULL), m_progress(NULL),
	  m_copier(NULL)
{
	m_sizeProcessor.addControlInfo(ProgressControlsInfo);
}

ProgressPage::~ProgressPage()
{
	delete m_copier;
}

bool 
ProgressPage::create()
{
	return PropertyPage::create(IDD_INSTATUS);
}

void
ProgressPage::onInit()
{
	// Get HWNDs to the dialog controls
	m_action = getDlgItem(IDC_INS_ACTION);
	m_destname = getDlgItem(IDC_INS_DEST);
	m_filename = getDlgItem(IDC_INS_FILE);
	m_progress = getDlgItem(IDC_INS_IPROGRESS);
}

void
ProgressPage::onActivate()
{
	// Disable back and next buttons
	getOwner()->setButtons(0);

	// Start the install thread.
	m_copier = m_installer.CopyFiles(*this);
	if (m_copier)
		m_copier->start();
}

bool
ProgressPage::onCancel()
{
	if (m_copier && m_copier->isAlive())
	{
		m_copier->suspend();
		String title = String::Load(IDS_WARN_TITLE);
		String msg = String::Load(IDS_CONFIRM_CANCEL);
		int ret = ::MessageBox(getHWND(), msg.c_str(), title.c_str(),
				       MB_ICONWARNING | MB_YESNO | MB_TASKMODAL);
		if (ret == IDNO)
		{
			m_copier->resume();
			return true;
		}
		m_copier->stop();
		m_copier->waitForExit(5000);
	}
	return false;
}

bool
ProgressPage::onMessageApp(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_APP_INSTALL_BEGIN:
	{
		return true;
	}
	case WM_APP_INSTALL_COMPLETE:
	{
		// Install is complete
		//getOwner()->setButtons(PSWIZB_NEXT);
		//getOwner()->pressButton(PSBTN_NEXT);
		m_copier->waitForExit(500);
		getOwner()->setActivePageByID(IDD_INSTALLDONE);
		return true;
	}
	case WM_APP_INSTALL_CANCEL:
	{
		// Abort install
		m_copier->waitForExit(500);
		//getOwner()->pressButton(PSBTN_CANCEL);
		getOwner()->setActivePageByID(IDD_INSTALLCANCEL);
		return true;
	}
	case WM_APP_INSTALL_NOTHING:
	{
		getOwner()->setActivePageByID(IDD_INSTALLNOTHING);
		return true;
	}
	}

	return false;
}

void
ProgressPage::setAction(DWORD id)
{
	switch (id)
	{
	case IDS_ACTION_COPYFILE:
	case IDS_ACTION_CREATEDIR:
	case IDS_ACTION_UPDATECONFIG:
	case IDS_ACTION_BACKUPFILES:
	case IDS_ACTION_RESTOREFILES:
		break;
	default:
		return;
	}
	String s = String::Load(id);
	::SetWindowText(m_action, s.c_str());
	//::SetWindowText(m_destname, TEXT(""));
	::SetWindowText(m_filename, TEXT(""));
}

void
ProgressPage::setDirectoryName(String const& s)
{
	::SetWindowText(m_destname, s.c_str());
}

void
ProgressPage::setFileName(String const& s)
{
	::SetWindowText(m_filename, s.c_str());
}

void 
ProgressPage::initProgress(long limit)
{
	::SendMessage(m_progress, PBM_SETRANGE, 0, MAKELPARAM(0,limit));
	::SendMessage(m_progress, PBM_SETSTEP, 1, 0);
	::SendMessage(m_progress, PBM_SETPOS, 0, 0);

	::SetWindowText(m_action, TEXT(""));
	::SetWindowText(m_destname, TEXT(""));
	::SetWindowText(m_filename, TEXT(""));
}

void
ProgressPage::setProgress(long pos)
{
	::SendMessage(m_progress, PBM_SETPOS, (WPARAM)pos, 0);
}

void 
ProgressPage::advanceProgress()
{
	::SendMessage(m_progress, PBM_STEPIT, 0, 0);
}

void 
ProgressPage::advanceProgress(long delta)
{
	::SendMessage(m_progress, PBM_DELTAPOS, (WPARAM)delta, 0);
}

