'use strict';

var gpower;
var laststate;

angular.module('roomstateapp.controllers', []).
    controller('StatusCtrl', function ($scope, $http, Socket) {

        $http({
            method: 'GET',
            url: '/api/simple/v2'
        }).
            success(function (data, status, headers, config) {
                $scope.simple = data;
                gpower.refresh(data.power);
            }).
            error(function (data, status, headers, config) {
                //$scope.name = 'Error!'
                console.log("error getting data");
            });

        Socket.on('sdata', function(message) {
            console.log("received data from server: " + message.data.names);
	    stateNotification(message.data.state);
            $scope.simple = message.data;
            gpower.refresh(message.data.power);
        });
});

angular.module('roomstateapp.services', []).
    factory('Socket', function ($rootScope) {
        var socket = io.connect();

        return {
            on: function(eventName, callback) {
                socket.on(eventName, function() {
                    var args = arguments;
                    $rootScope.$apply(function() {
                        callback.apply(socket, args);
                    });
                });
            },
            emit: function(eventName, data, callback) {
                if(typeof data == 'function') {
                    callback = data;
                    data = {};
                }
                socket.emit(eventName, data, function() {
                    var args = arguments;
                    $rootScope.$apply(function() {
                        if(callback) {
                            callback.apply(socket, args);
                        }
                    });
                });
            },
            emitAndListen: function(eventName, data, callback) {
                this.emit(eventName, data, callback);
                this.on(eventName, callback);
            }
        };
    });

angular.module('roomstateapp.statusfilter', []).filter('statustostring', function() {
    return function(input) {
        return input == true ? 'geöffnet' : input == false ? 'geschlossen' : 'unbekannt';
    };
});

angular.module('roomstateapp', ['roomstateapp.controllers', 'roomstateapp.services', 'roomstateapp.statusfilter']);


$( document ).ready(function() {
    console.log( "ready!" );

gpower = new JustGage({
    id: "gauge",
    value: 0,
    min: 0,
    max: 30000,
    title: " ",
    label: "Watt",
    showMinMax: false
    });

});

function stateNotification(state) {
	if (state == true && laststate != true && laststate != null && enabledNotifications) {
		laststate = true;
		showNotification("CTDO - Status", "Der Chaostreff ist nun offen.", "/img/green.png", 4);
	} else if (state == false && laststate != false && laststate != null && enabledNotifications) {
		laststate = false;
		showNotification("CTDO - Staus", "Der Chaostreff ist nun geschlossen", "/img/red.png", 4);
	} else {
	laststate = state;
	}
}

function toggle() {
    toggleNotifications();
    if (enabledNotifications === true) {
        document.getElementById("notificationButton").innerHTML = "Statusbenachrichtigungen an";
        document.getElementById("notificationButton").className = "btn btn-block btn-success";
    } else {
        document.getElementById("notificationButton").innerHTML = "Statusbenachrichtigungen aus";
        document.getElementById("notificationButton").className = "btn btn-block btn-danger";
    }
}