/// class ProspectDirector.Modules.Processes.FeatureStatus
/// using ProspectDirector.Types.Core.Feature

ProspectDirector.Modules.Processes.FeatureStatus = ProspectDirector.Types.Core.Feature.extend({

    init: function (options) {

        this._super(options);

        this.init_list();

        this.get_addButton().on("click", $.proxy(this.addButton_onClick, this));
        this.get_removeButton().on("click", $.proxy(this.removeButton_onClick, this));
        this.get_editButton().on("click", $.proxy(this.editButton_onClick, this));

        this.enableButtons(false);

    },

    //---------------------------
    //--- PROPERTIES
    //---------------------------

    get_list: function () {
        return this.get_container().find("[data-role=list]");
    },

    get_listTemplate: function () {
        return this.get_container().find("[data-id=list-template]");
    },

    get_addButton: function () {
        return this.get_container().find("INPUT[type=button][data-command=add]");
    },

    get_removeButton: function () {
        return this.get_container().find("INPUT[type=button][data-command=remove]");
    },

    get_editButton: function () {
        return this.get_container().find("INPUT[type=button][data-command=edit]");
    },

    //---------------------------
    //--- INITIALIZERS
    //---------------------------

    init_list: function () {

        var list = null;

        list = this.get_list().kendoListView({
            dataSource: {
                data: this.options.data.statusList
            },
            selectable: true,
            change: $.proxy(this.list_onChange, this),
            template: kendo.template(this.get_listTemplate().html())
        }).data("kendoListView");

        this.get_list().on("dblclick", "DIV[data-uid]", $.proxy(this.editButton_onClick, this));

        this.get_list().disableTextSelection();

    },

    //---------------------------
    //--- EVENT HANDLERS
    //---------------------------

    list_onChange: function () {
        this.get_removeButton().enable();
        this.get_editButton().enable();
    },

    addButton_onClick: function (e) {

        ProspectDirector.Modules.Processes.ProcessStatusDialog.Dialog.show({
            callbacks: {
                ok: $.proxy(this.add_onSuccess, this)
            },
            params: {
                processId: this.options.data.processId
            }
        });

    },

    removeButton_onClick: function (e) {

        var list = this.get_list().data("kendoListView"),
            item = list.select(),
            data = null;

        if (item.length > 0) {

            data = list.dataSource.getByUid(item.data("uid"));

            if (!confirm("Delete the \"" + data.name + "\" status?")) {
                return;
            }

            PD.ajax({
                type: "POST",
                url: ProspectDirector.RUNTIME.resolveUrl("~/process/deletestatus"),
                data: {
                    id: data.id
                },
                success: $.proxy(function () {
                    this.remove_onSuccess(item);
                }, this)
            });

        }

    },

    remove_onSuccess: function (item) {

        var list = this.get_list().data("kendoListView"),
            data = list.dataSource.getByUid(item.data("uid")),
            next = this.getNext(item),
            nextUid = null;

        list.remove(item);

        if (next.length > 0) {

            nextUid = next.data().uid;

            next = this.get_list().find("[data-uid=" + nextUid + "]");

            list.select(next);

        }

    },

    editButton_onClick: function (e) {

        var list = this.get_list().data("kendoListView"),
            item = list.select(),
            data = list.dataSource.getByUid(item.data("uid"));

        ProspectDirector.Modules.Processes.ProcessStatusDialog.Dialog.show({
            callbacks: {
                ok: $.proxy(this.edit_onSuccess, this)
            },
            params: {
                processId: this.options.data.processId,
                id: data.id,
                icon: data.icon,
                name: data.name,
                customId: data.customId
            }
        });

    },

    add_onSuccess: function (sender, e) {

        var list = this.get_list().data("kendoListView"),
            item = null,
            data = null;

        list.dataSource.add(e);
        list.refresh();

        data = list.dataSource.get(e.id);

        if (data) {
            item = this.get_list().find("[data-uid=" + data.uid + "]");
            list.select(item);
        }

    },

    edit_onSuccess: function (sender, e) {

        var list = this.get_list().data("kendoListView"),
            item = list.select(),
            data = list.dataSource.getByUid(item.data("uid"));

        data.customId = e.customId;
        data.icon = e.icon;
        data.name = e.name;
        list.refresh();

        if (data) {
            data = list.dataSource.get(data.id);
            item = this.get_list().find("[data-uid=" + data.uid + "]");
            list.select(item);
        }

    },

    //---------------------------
    //--- METHODS
    //---------------------------

    getNext: function (node) {

        var next = $(null),
            list = this.get_list().data("kendoListView");

        if (node.length !== 0) {

            next = node.next();

            if (next.length === 0) {
                next = node.prev();
            }

        }

        this.enableButtons(next.length > 0);

        return next;

    },

    enableButtons: function (enabled) {

        if (enabled) {
            this.get_editButton().enable();
            this.get_removeButton().enable();
        }
        else {
            this.get_editButton().disable();
            this.get_removeButton().disable();
        }

    }

});