/*
 * pawsupdateroptions.cpp
 *
 * Copyright (C) 2006 Atomic Blue (info@planeshift.it, http://www.atomicblue.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 (version 2 of the License)
 * 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.
 *
 */

#include <psconfig.h>
#include "updaterglobals.h"
#include "updaterconfig.h"


// PAWS INCLUDES
#include "pawsupdateroptions.h"
#include "paws/pawsmanager.h"
#include "paws/pawstextbox.h"
#include "paws/pawsbutton.h"
#include "paws/pawscheckbox.h"

using namespace updater;

pawsUpdaterOptions::pawsUpdaterOptions()
{
    btnSave = NULL;
    btnCancel = NULL;
    chkSkipVerCheck = NULL;
    chkBackupFiles = NULL;
    chkBinaries = NULL;
    chkModule_root = NULL;
    chkModule_art = NULL;
    chkModule_data = NULL;
    chkModule_docs = NULL;
    chkProxy = NULL;
    txtThisOS = NULL;
    txtProxyHost = NULL;
    txtProxyPort = NULL;
}

bool pawsUpdaterOptions::PostSetup()
{
    txtThisOS = (pawsTextBox*)FindWidget("thisOS");
    if (!txtThisOS)
        return false;
    csString OSmsg;
    OSmsg.Format("PlaneShift Updater: %s Version", Config::GetOSName() );
    txtThisOS->SetText(OSmsg);
    txtThisOS->HorizAdjust(pawsTextBox::horizCENTRE);

    btnSave = (pawsButton*)FindWidget("SaveButton");
    if (!btnSave)
        return false;

    btnCancel = (pawsButton*)FindWidget("CancelButton");
    if (!btnCancel)
        return false;

    chkSkipVerCheck = (pawsCheckBox*)FindWidget("UpdateSelf");
    if (!chkSkipVerCheck)
        return false;

    chkBackupFiles = (pawsCheckBox*)FindWidget("BackupFiles");
    if (!chkBackupFiles)
        return false;

    chkBinaries = (pawsCheckBox*)FindWidget("UpdateBins");
    if (!chkBinaries)
        return false;

    chkModule_root = (pawsCheckBox*)FindWidget("UpdateRoot");
    if (!chkModule_root)
        return false;

    chkModule_art = (pawsCheckBox*)FindWidget("UpdateArt");
    if (!chkModule_art)
        return false;

    chkModule_data = (pawsCheckBox*)FindWidget("UpdateData");
    if (!chkModule_data)
        return false;

    chkModule_docs = (pawsCheckBox*)FindWidget("UpdateDocs");
    if (!chkModule_docs)
        return false;

    chkProxy = (pawsCheckBox*)FindWidget("UseProxy");
    if (!chkProxy)
        return false;

    txtProxyHost = (pawsEditTextBox*)FindWidget("ProxyHost");
    if (!txtProxyHost)
        return false;

    txtProxyPort = (pawsEditTextBox*)FindWidget("ProxyPort");
    if (!txtProxyPort)
        return false;


    if (!LoadOptions())
        return false;

    this->Show();
    return true;
}

bool pawsUpdaterOptions::LoadOptions()
{
    Config* config = psupdaterengine->GetUpdater()->GetConfig();
    if (!config)
        return false;

    chkSkipVerCheck->SetState( config->GetSkipVerCheck() );
    
    chkBackupFiles->SetState( config->MakeBackup() );

    chkBinaries->SetState( config->GetIncludeOS() );

    chkModule_root->SetState( config->IsUsingModule("root") );
    chkModule_art->SetState( config->IsUsingModule("art") );
    chkModule_data->SetState( config->IsUsingModule("data") );
    chkModule_docs->SetState( config->IsUsingModule("docs") );

    chkProxy->SetState( config->GetProxy()->active );
    txtProxyHost->SetText( config->GetProxy()->host );
    txtProxyPort->SetText( csString().Format("%u",config->GetProxy()->port) );

    return true;
}

bool pawsUpdaterOptions::SaveOptions()
{
    Config* config = psupdaterengine->GetUpdater()->GetConfig();
    if (!config)
        return false;

    config->SetSkipVerCheck( chkSkipVerCheck->GetState() );
    
    config->SetBackupMode( chkBackupFiles->GetState() );

    config->SetIncludeOS( chkBinaries->GetState() );

    config->SetModuleUse( "root", chkModule_root->GetState() );
    config->SetModuleUse( "art", chkModule_art->GetState() );
    config->SetModuleUse( "data", chkModule_data->GetState() );
    config->SetModuleUse( "docs", chkModule_docs->GetState() );

    config->SetProxy(chkProxy->GetState(),
                     txtProxyHost->GetText(),
                     atoi(txtProxyPort->GetText()) );

    return true;
}

bool pawsUpdaterOptions::OnButtonPressed(int button, int keyModifier, pawsWidget* widget)
{
    if ((pawsButton*)widget == btnSave)
    {
        this->SetAlwaysOnTop(false);
        this->Hide();
        if (!SaveOptions())
            psupdaterengine->OutToScreen( psupdaterengine->FindRGB(255,0,0), "ERROR: Failed to save options" );
    }
    else if ((pawsButton*)widget == btnCancel)
    {
        this->SetAlwaysOnTop(false);
        this->Hide();
        if (!LoadOptions())
            psupdaterengine->OutToScreen( psupdaterengine->FindRGB(255,0,0), "ERROR: Failed to reload options" );
    }
    else if ((pawsCheckBox*)widget == chkSkipVerCheck)
    {
        if (chkSkipVerCheck->GetState())
            PawsManager::GetSingleton().CreateWarningBox("WARNING: Downloading from an out of date server can damage "
                                                         "your game installation. It's recommended that you also "
                                                         "disable program updating if you do this. This option will "
                                                         "last only for this updater session." );
    }

    return true;
}
