function AdminVM() {
    var self = this;
    self.groupsList = ko.observable(null);
    self.groupEditItem = ko.observable(null);
    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 () {
        self.helpers.goToMenuOption(self.uiPages.group);
        self.groupEditItem(null);
    };
    self.hideGroupForm = function () {
        self.groupEditItem(null);
    };
    self.newGroupForm = function () {
        self.groupEditItem({ Id: -1, Name: "" });
        self.helpers.goToMenuOption(self.uiPages.group);
    };
    self.groupFormHidden = ko.computed(function () {
        return self.groupEditItem() == null;
    }, self);
    self.editGroupClick = function (data) {
        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);
        }).fail(function (resp, status, error) {
            console.log("error - getGroups");
            var errorObj = self.helpers.processRequestFailure(resp, status, error);
        });
    };
    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 () {
                console.log("deleted " + groupId);
                self.hideGroupForm();
                self.helpers.goToMenuOption(self.uiPages.home());
            }
        });
        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());
                var errorObj = self.helpers.processRequestFailure(resp, status, error);
            });
    };
    Sammy(function () {
        this.disable_push_state = true;
        this.get("#overview", function () {
            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());