/*Copyright (C) 2010-2012 Duncan Deveaux

This file is part of Hikou no mizu.

Hikou no mizu 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.

Hikou no mizu 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 Hikou no mizu.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Configuration.hpp"

Configuration::Configuration() :
m_windowWidth(1024), m_windowHeight(576), m_fullscreen(false),
m_soundVolume(100)
{
    m_playerKeys[0] = PlayerKeys(); //Player 1 : Default keys
    m_playerKeys[1] = PlayerKeys(SDLK_p, SDLK_UP, SDLK_LEFT, SDLK_RIGHT); //Player 2 : Other keys
    
    m_cfgDirectory = Directory::getHome() + "/.hikounomizu/";
}

void Configuration::load()
{
    //Main configuration
    ConfReader mainCfgReader;
    if (mainCfgReader.load(m_cfgDirectory + "main.cfg"))
    {
        //Init variables
        int windowWidth, windowHeight, soundVolume;
        bool fullscreen;
        
        //Get elements
        if (mainCfgReader.getIntItem("window_width", windowWidth) && mainCfgReader.getIntItem("window_height", windowHeight))
        {
            setWindowWidth(windowWidth);
            setWindowHeight(windowHeight);
        }
        
        if (mainCfgReader.getBoolItem("fullscreen", fullscreen))
            setFullscreen(fullscreen);
        
        if (mainCfgReader.getIntItem("sound_volume", soundVolume))
            setSoundVolume(soundVolume);
    }
    
    //Keys configuration
    ConfReader keysCfgReader;
    
    bool browsing = true;
    int i = 0;
    while (browsing)
    {
        std::ostringstream keysFilePath;
        keysFilePath << m_cfgDirectory << "keys/player" << i << ".cfg";
        
        if (keysCfgReader.load(keysFilePath.str()))
        {
            PlayerKeys keys;
            
            //Init variables
            SDLKey attack, jump, moveLeft, moveRight;
            
            if (keysCfgReader.getKeyCodeItem("attack", attack))
                keys.setAttackKey(attack);
            
            if (keysCfgReader.getKeyCodeItem("jump", jump))
                keys.setJumpKey(jump);
            
            if (keysCfgReader.getKeyCodeItem("moveLeft", moveLeft))
                keys.setMoveLeftKey(moveLeft);
            
            if (keysCfgReader.getKeyCodeItem("moveRight", moveRight))
                keys.setMoveRightKey(moveRight);
            
            setPlayerKeys(i, keys);
        }
        else
            browsing = false;
        
        i++;
    }
}

void Configuration::save()
{
    //Create directories if needed
    Directory::create(m_cfgDirectory);
    Directory::create(m_cfgDirectory + "keys/");
    
    //Main configuration
    ConfWriter mainCfgWriter;
    mainCfgWriter.addIntItem("window_width", m_windowWidth);
    mainCfgWriter.addIntItem("window_height", m_windowHeight);
    mainCfgWriter.addBoolItem("fullscreen", m_fullscreen);
    mainCfgWriter.addIntItem("sound_volume", m_soundVolume);
    
    mainCfgWriter.writeFile(m_cfgDirectory + "main.cfg");
    
    //Keys configuration
    int i = 0;
    std::map<unsigned int, PlayerKeys>::iterator it;
    for (it = m_playerKeys.begin(); it != m_playerKeys.end(); it++)
    {
        //Init variables
        SDLKey attack = it->second.getAttackKey();
        SDLKey jump = it->second.getJumpKey();
        SDLKey moveLeft = it->second.getMoveLeftKey();
        SDLKey moveRight = it->second.getMoveRightKey();
        
        //Set elements
        ConfWriter keysCfgWriter;
        keysCfgWriter.addIntItem("attack", attack);
        keysCfgWriter.addIntItem("jump", jump);
        keysCfgWriter.addIntItem("moveLeft", moveLeft);
        keysCfgWriter.addIntItem("moveRight", moveRight);
        
        std::ostringstream keysFilePath;
        keysFilePath << m_cfgDirectory << "keys/player" << i << ".cfg";
        
        keysCfgWriter.writeFile(keysFilePath.str());
        
        i++;
    }
}

//Get and set methods
    //Video
void Configuration::setWindowWidth(int windowWidth)
{
    if (windowWidth > 0)
        m_windowWidth = windowWidth;
}

int Configuration::getWindowWidth() const
{
    return m_windowWidth;
}

void Configuration::setWindowHeight(int windowHeight)
{
    if (windowHeight > 0)
        m_windowHeight = windowHeight;
}

int Configuration::getWindowHeight() const
{
    return m_windowHeight;
}

void Configuration::setFullscreen(bool fullscreen)
{
    m_fullscreen = fullscreen;
}

bool Configuration::getFullscreen() const
{
    return m_fullscreen;
}

    //Audio
void Configuration::setSoundVolume(float soundVolume)
{
    if (soundVolume >= 0)
        m_soundVolume = soundVolume;
}

float Configuration::getSoundVolume() const
{
    return m_soundVolume;
}

    //Keys binding
void Configuration::setPlayerKeys(unsigned int ix, const PlayerKeys &playerKeys)
{
    m_playerKeys[ix] = playerKeys;
}

bool Configuration::getPlayerKeys(unsigned int ix, PlayerKeys &target) const
{
    std::map<unsigned int, PlayerKeys>::const_iterator it = m_playerKeys.find(ix);
    
    if (it != m_playerKeys.end())
    {
        target = it->second;
        return true;
    }
    
    return false;
}
