/*
 * Copyright (c) 2015,  nwillc@gmail.com
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

var myPresentation = {

    init: function (config) {
        console.log("init");
        myPresentation.config = {
            categories: $('#browseCategories'),
            snippetCategories: $('#snippetCategories'),
            category: $('#category'),
            titles: $('#titles'),
            title: $('#title'),
            body: $('#body'),
            bodyInput: $('#bodyInput'),
            searchDialog: $('#searchCategoryDialog'),
            query: $('#query')
        };
        $.extend(myPresentation.config, config);
        myPresentation.bind();
        myPresentation.loadCategories();
    },

    bind: function () {
        console.log("bind");
        $('#tabs').tabs();
        $(myPresentation.config.searchDialog).dialog();
        $(myPresentation.config.searchDialog).dialog('close');
        $(myPresentation.config.categories).change(myPresentation.loadAllTitles);
        $(myPresentation.config.titles).change(myPresentation.loadBody);
        $('#saveSnippetButton').click(myPresentation.saveSnippet);
        $('#logoutButton').click(myPresentation.logout);
        $('#deleteButton').click(myPresentation.deleteSnippet);
        $('#saveCategoryButton').click(myPresentation.saveCategory);
        $('#deleteCategoryButton').click(myPresentation.deleteCategory);
        $('#searchButton').click(myPresentation.openSearch);
        $('#performSearch').click(myPresentation.performSearch);
    },

    loadCategories: function () {
        console.log("loadCategories");
        $.get("v1/categories", function (data) {
            var list = JSON.parse(data);
            $(myPresentation.config.categories).empty();
            $(list).sort(function (a, b) {
                return a.name.toLowerCase() > b.name.toLowerCase();
            }).each(function () {
                myPresentation.config.categories.append($("<option></option>").attr("value", this.key).text(this.name));
            });
            window.setTimeout(function () {
                $(myPresentation.config.categories).change();
            }, 1);
            $(myPresentation.config.snippetCategories).empty();
            $(list).sort(function (a, b) {
                return a.name.toLowerCase() > b.name.toLowerCase();
            }).each(function () {
                myPresentation.config.snippetCategories.append($("<option></option>").attr("value", this.key).text(this.name));
            });
        });
    },

    loadAllTitles: function () {
        console.log("loadAllTitles");
        var category = $(myPresentation.config.categories).val();
        console.log("Selected Category: " + category);
        $.get("v1/snippets/category/" + category, function (data) {
            myPresentation.loadTitles(JSON.parse(data));
        })
    },

    loadTitles: function (list) {
        console.log("loadTitles");
        $('option', myPresentation.config.titles).remove();
        $(myPresentation.config.body).val('');
        $(list).sort(function (a, b) {
            return a.title.toLowerCase() > b.title.toLowerCase();
        }).each(function () {
            myPresentation.config.titles.append($("<option></option>").attr("value", this.key).text(this.title));
        })
    },

    loadBody: function () {
        console.log("loadBody");
        var category = $(myPresentation.config.categories).val();
        var title = $(myPresentation.config.titles).val();
        console.log("Selected Category: " + category + " Title: " + title);
        $.get("v1/snippets/" + title, function (data, status) {
            console.log("Status: " + status + " Data: " + data);
            var found = JSON.parse(data);
            $(myPresentation.config.body).val(found.body);
        })
    },

    saveCategory: function () {
        console.log("saveCategory");
        $.post("v1/categories", JSON.stringify({
            name: $(myPresentation.config.category).val()
        }), function () {
            myPresentation.loadCategories();
            $('#category').val('');
        }).fail(function () {
            alert("Failed saving category");
        });
    },

    saveSnippet: function () {
        console.log("Save Snippet");
        $.post('v1/snippets', JSON.stringify({
            category: $(myPresentation.config.snippetCategories).val(),
            title: $(myPresentation.config.title).val(),
            body: $(myPresentation.config.bodyInput).val()
        }), function () {
            myPresentation.loadCategories();
        }).fail(function () {
            alert("Failed saving snippet.")
        });
        $(myPresentation.config.title).val('');
        $(myPresentation.config.bodyInput).val('');
    },

    deleteSnippet: function () {
        var selected = $(titles).find("option:selected");
        console.log("Delete Snippet: " + $(myPresentation.config.categories).val() + ':'
            + $(selected).val());
        $.ajax({
            url: 'v1/snippets/' + $(selected).val(),
            type: 'DELETE',
            success: function () {
                console.log('success');
                myPresentation.loadCategories();
            }
        });
    },

    deleteCategory: function () {
        console.log("Delete Category: " + $(myPresentation.config.categories).val());
        $.ajax({
            url: 'v1/categories/' + $(myPresentation.config.categories).val(),
            type: 'DELETE',
            success: function () {
                console.log('success');
                myPresentation.loadCategories();
            }
        });
    },

    openSearch: function () {
        $(myPresentation.config.searchDialog).dialog('open');
    },

    performSearch: function () {
        console.log("loadAllTitles");
        var category = $(myPresentation.config.categories).val();
        console.log("Selected Category: " + category);
        $.post("v1/snippets/category/" + category, JSON.stringify({
            query: $(query).val()
        }), function (data) {
            myPresentation.loadTitles(JSON.parse(data));
            $(myPresentation.config.searchDialog).dialog('close');
        }).fail(function () {
            alert("Failed searching snippets.")
        });
    },

    logout: function () {
        console.log("logout");
        myPersona.logout();
        $.ajax({
            url: 'v1/auth',
            type: 'DELETE',
            success: function () {
                window.location.replace("/login.html");
            }
        });
    }

};

$(document).ready(function () {
    myPresentation.init();
});

@@ -1527,16 +1527,30 @@
 .dialog(
+%7B width: 500 %7D
 );%0A     

Set initial search dialog size.