function AdminVM() {
    var self = this;
    self.groupsList = ko.observable(null);
    self.groupEditItem = ko.observable({Id:-1,Name:""});
    self.helpers = new Helpers();
    self.uiPages = {
        overview: "overview",
        group: "groups",
        home: function () { return this.overview; }
    };
    self.apiEndpoints = {
        deleteGroups:"/api/groups/delete",
        getGroups: "/api/groups",
        editGroup: "/api/groups/edit"
    };
    self.clearGroupForm = function (data, event) {
        self.goToMenuOption(self.uiPages.group);
        self.groupEditItem({ Id: -1, Name: "" });
    };
    self.hideGroupForm = function (data, event) {
        self.helpers.goToMenuOption(self.uiPages.group);
        self.groupEditItem(null);
    };
    self.groupFormHidden = ko.computed(function () {
        return self.groupEditItem() == null;
    }, self);
    self.editGroupClick = function (data, event) {
        self.helpers.goToMenuOption(self.uiPages.group);
        self.groupEditItem(data);
    };
    self.getGroups = function () {
        var url = self.helpers.createRequestUrl(self.apiEndpoints.getGroups, null, false);
        $.getJSON(url, function (res) {
            self.groupsList(res);
            //console.log(res);
        }).fail(function (resp, status, error) {
            console.log("error - getGroups");
            //var errObj = self.processRequestFailure(resp, status, error);
            //self.assignErrorObject(errObj.errorCode, errObj.errorMessage, "getGroups");
            //self.goToMenuOption(self.uiPages.home());
        });
    };
    self.deleteGroup = function (groupId) {
        var url = self.helpers.createRequestUrl(self.apiEndpoints.deleteGroups,
            [{ key: "groupId", value: groupId }],
            false,
            false);
        $.ajax({
            url: url,
            type: 'DELETE',
            success: function (result) {
                console.log("deleted " + groupId);
                self.helpers.goToMenuOption(self.uiPages.home());
                self.clearGroupForm();
                // Do something with the result
            }
        });
        console.log("delete: " + groupId);
    };
    self.submitGroupEdit = function(group) {
        var url = self.helpers.createRequestUrl(self.apiEndpoints.editGroup, null, false);
        $.post(url, group, function () {
        }, "json")
        .done(function () {
            self.groupEditItem(null);
            self.helpers.goToMenuOption(self.uiPages.home());
        })
        .fail(function (resp, status, error) {
            self.helpers.goToMenuOption(self.uiPages.home());
        });
    };
    Sammy(function () {
        this.get("#overview", function () {
            self.clearGroupForm();
            self.getGroups();
        });
        this.post("#editgroup", function () {
            self.submitGroupEdit(self.groupEditItem());
            return false;
        });
        //default route (home page)
        this.get("", function () { this.app.runRoute("get", "#" + self.uiPages.home()) });
    }).run();
};
ko.applyBindings(new AdminVM());