/**
 *  Copyright © 2010 Digia Plc
 *  Copyright © 2010 Nokia Corporation
 *
 *  All rights reserved.
 *
 *  Nokia and Nokia Connecting People are registered trademarks of
 *  Nokia Corporation.
 *  Java and all Java-based marks are trademarks or registered
 *  trademarks of
 *  Sun Microsystems, Inc. Other product and company names
 *  mentioned herein may be
 *  trademarks or trade names of their respective owners.
 *
 *
 *  Subject to the conditions below, you may, without charge:
 *
 *  ·  Use, copy, modify and/or merge copies of this software and
 *     associated documentation files (the "Software")
 *
 *  ·  Publish, distribute, sub-licence and/or sell new software
 *     derived from or incorporating the Software.
 *
 *
 *  This file, unmodified, shall be included with all copies or
 *  substantial portions
 *  of the Software that are distributed in source code form.
 *
 *  The Software cannot constitute the primary value of any new
 *  software derived
 *  from or incorporating the Software.
 *
 *  Any person dealing with the Software shall not misrepresent
 *  the source of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 *  KIND, EXPRESS OR IMPLIED,
 *  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE
 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * Changes current profile
 *
 * @param newProfile The profile to use
 * @return Nothing
 */
function changeCurrent(newProfile) {
    hideAll();
    structure.currentMode = newProfile;
}

/**
 * Hides all the rows
 *
 * @return Nothing
 */
function hideAll() {
    rowTemp.visible = false;
    rowLight.visible = false;
    rowLightDim.visible = false;
    rowVolume.visible = false;
    rowVolumeMovie.visible = false;
    rowSaunaOff.visible = false;
    rowLockOpen.visible = false;
    rowLockClosed.visible = false;
    rowAlarmOn.visible = false;
    rowAlarmOff.visible = false;
}

/**
 * Sets current mode to Normal
 *
 * @return Nothing
 */
function normalMode() {
    changeCurrent( "Home" );
    rowTemp.theTemperature = 22;
    rowTemp.visible = true;
    rowLockOpen.visible = true;
    rowAlarmOff.visible = true;

    var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
    db.transaction(
        function(tx) {
            tx.executeSql('UPDATE RoomTable SET temp=22');
            tx.executeSql('UPDATE Security SET alarmOn="false", doorsLocked="false"');
        }
    )
}

/**
 * Sets current mode to Night
 *
 * @return Nothing
 */
function nightMode() {
    changeCurrent( "Night" );
    rowTemp.theTemperature = 20;
    rowTemp.visible = true;
    rowLight.visible = true;
    rowVolume.visible = true;
    rowSaunaOff.visible = true;
    rowLockClosed.visible = true;
    structure.saunaTurnedOff();

    var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
    db.transaction(
        function(tx) {
            tx.executeSql('UPDATE RoomTable SET light=0 WHERE roomCode="kitchen" OR roomCode="livingroom" OR roomCode="khh" OR roomCode="kph" OR roomCode="sauna"');
            tx.executeSql('UPDATE RoomTable SET temp=20, mute="true"');
            tx.executeSql('UPDATE Sauna SET saunaOn="false"');
            tx.executeSql('UPDATE Security SET doorsLocked="true"');
        }
    )
}

/**
 * Sets current mode to Away
 *
 * @return Nothing
 */
function awayMode() {
    changeCurrent("Away");
    rowTemp.theTemperature = 20;
    rowTemp.visible = true;
    rowLight.visible = true;
    rowVolume.visible = true;
    rowLockClosed.visible = true;
    structure.saunaTurnedOff();

    var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
    db.transaction(
        function(tx) {
            tx.executeSql('UPDATE RoomTable SET temp=20, mute="true", light=0');
            tx.executeSql('UPDATE Security SET alarmOn="false", doorsLocked="true"');
        }
    )
}

/**
 * Sets current mode to Away (long)
 *
 * @return Nothing
 */
function awayLongMode() {
    changeCurrent("Away (long)");
    rowTemp.theTemperature = 18;
    rowTemp.visible = true;
    rowLight.visible = true;
    rowVolume.visible = true;
    rowSaunaOff.visible = true;
    rowLockClosed.visible = true;
    rowAlarmOn.visible = true;
    structure.saunaTurnedOff();

    var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
    db.transaction(
        function(tx) {
            tx.executeSql('UPDATE RoomTable SET temp=18, light=0, volume=0');
            tx.executeSql('UPDATE Sauna SET saunaOn="false"');
            tx.executeSql('UPDATE Security SET alarmOn="true", doorsLocked="true"');
        }
    )
}

/**
 * Sets current mode to Movie
 *
 * @return Nothing
 */
function movieMode() {
    changeCurrent("Movie");
    rowLight.visible = true;
    rowLightDim.visible = true;
    rowVolume.visible = true;
    rowVolumeMovie.visible = true;

    var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
    db.transaction(
        function(tx) {
            tx.executeSql('UPDATE RoomTable SET light=0, mute="true"');
            tx.executeSql('UPDATE RoomTable SET light=10, volume=80, mute="false" WHERE roomCode="livingroom"');
            tx.executeSql('UPDATE Security SET alarmOn="false"');
        }
    )
}
