/// class PD.Shared.View.ViewService

PD.Shared.View.ViewService = Class.extend({

    //===================================
    // FIELDS
    //===================================

    _viewGrid: null,
    _viewStateId: null,

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (viewStateId, viewGrid) {
        this._viewStateId = viewStateId;
        this._viewGrid = viewGrid;
    },

    //===================================
    // PUBLIC METHODS
    //===================================

    save: function () {

        PD.ajax({
            type: "POST",
            url: PD.Path.resolve("~/view/save?") + $.param({
                viewStateId: this._viewStateId
            }),
            success: onSuccess,
            error: onError
        });

    },

    saveAs: function () {

        ProspectDirector.Types.Core.Dialogs.ViewSaveAs.show({
            params: {
                viewStateId: this._viewStateId
            }
        });

    },

    undo: function () {

        var that = this;

        PD.ajax({
            type: "POST",
            url: PD.Path.resolve("~/view/undo?") + $.param({
                viewStateId: this._viewStateId
            }),
            success: function () {
                that._viewGrid.build();
            }
        });

    },

    reset: function () {

        var that = this;

        PD.ajax({
            type: "POST",
            url: PD.Path.resolve("~/view/reset?") + $.param({
                viewStateId: this._viewStateId
            }),
            success: function () {
                PDJS.Shared.MessageStack.instance.success("View reset successfully.")
                that._viewGrid.build();
            }
        });

    },

    refresh: function () {
        this._viewGrid.refresh();
    },

    showAnchorDialog: function () {

        var that = this;

        ProspectDirector.Types.Core.Dialogs.ViewAnchor.show({
            params: {
                viewStateId: this._viewStateId
            },
            callbacks: {
                ok: function () {
                    that._viewGrid.build();
                }
            }
        });

    },

    showDesignFiltersDialog: function () {

        var that = this;

        ProspectDirector.Modules.Views.ViewDesignFiltersDialog.Dialog.show({
            params: {
                viewStateId: this._viewStateId
            },
            callbacks: {
                ok: function (sender, e) {
                    that._viewGrid.build();
                }
            }
        });

    },

    showDesignColumnsDialog: function () {

        var that = this;

        ProspectDirector.Modules.Views.ViewDesignColumnsDialog.Dialog.show({
            params: {
                viewStateId: this._viewStateId
            },
            callbacks: {
                ok: function (sender, e) {
                    that._viewGrid.build();
                }
            }
        });

    },

    showBoundsFiltersDialog: function () {

        var that = this;

        ProspectDirector.Modules.Views.ViewBoundsFiltersDialog.Dialog.show({
            params: {
                viewStateId: this._viewStateId
            },
            callbacks: {
                ok: function (sender, e) {
                    that._viewGrid.build();
                }
            }
        });

    },

    showBoundsColumnsDialog: function () {

        var that = this;

        ProspectDirector.Modules.Views.ViewBoundsColumnsDialog.Dialog.show({
            params: {
                viewStateId: this._viewStateId
            },
            callbacks: {
                ok: function (sender, e) {
                    that._viewGrid.build();
                }
            }
        });

    },

    showSearchableColumnsDialog: function () {

        var that = this;

        ProspectDirector.Modules.Views.ViewBoundsSearchablesDialog.Dialog.show({
            params: {
                viewStateId: this._viewStateId
            },
            callbacks: {
                ok: function (sender, e) {
                    that._viewGrid.build();
                }
            }
        });

    },

    exportFormatted: function () {

        PDJS.Shared.Downloader.download({
            url: `~/view/export`,
            params: {
                viewStateId: this._viewStateId,
                format: true,
                contextualFilters: JSON.stringify(this._viewGrid.getContextualFilters())
            }
        });
        
    },

    exportUnformatted: function () {

        PDJS.Shared.Downloader.download({
            url: `~/view/export`,
            params: {
                viewStateId: this._viewStateId,
                format: false,
                contextualFilters: JSON.stringify(this._viewGrid.getContextualFilters())
            }
        });

    },

    properties: function () {

        ProspectDirector.Types.Core.Dialogs.ViewProperties.show({
            params: {
                viewStateId: this._viewStateId
            }
        });

    },

    share: function () {

        window.location = "mailto:?subject=Invitation to share a view&body=I would like to share a view with you. Click the link below or copy and paste it into your browser to access the view.%0d%0d" + window.location.href + "\n\n";

    }

});