/*globals define*/
/*jslint nomen: true*/

define('de_epages/shop/linkpicker/additionalpagesview', [
    'jquery',
    'backbone',
    'ep',
    '$tmpl!de_epages/shop/linkpicker/additionalpagesview',
    'ep/ui/input',
    'ep/ajax'
    /**
     * Additional pages view
     * @param  {Function}       $           jQuery
     * @param  {Function}       Backbone    Backbone
     * @param  {Function}       ep          Used for getting the config object
     * @param  {Function}       template    Template for rendering the additional pages view
     * @return {Backbone view}              Additional pages view
     */
], function ($, Backbone, ep, template) {
    'use strict';

    return Backbone.View.extend({

        /**
         * Tag used for $el
         * @type {String}
         */
        tagName: 'div',

        /**
         * Template used for rendering
         * @type {String}
         */
        template: template,

        /**
         * Data used in the template rendering process
         * @type {Object}
         */
        data: {},

        /**
         * events the view listens to
         * @type {Object}
         */
        events: {
            'change input': '_publishLinkData'
        },

        /**
         * Renders the additional pages view
         * @return {Backbone view}
         */
        render: function (_currentOption) {
            this.$el.html(this.template({
                data: this.data,
                currentOption: _currentOption
            })).find('input').uiInput();

            return this;
        },

        /**
         * Clears the selected radio buttons
         */
        clearLinkData: function () {
            this.$('input:radio').prop("checked", false);
            this.data = {};
        },

        /**
         * Checks the radio button based on the passed url
         * @param {String} _url [value of the radio button that should be checked]
         */
        setCheckedRadio: function (_url) {
            var relativeURL = _url.replace(/.*\?/g,'?');
            this.$('input:radio[value="' + relativeURL + '"]').prop("checked", true);
        },

        /**
         * Triggers the 'linkpicked' event and publishes name and link url
         * @param   {Event} _event [Event that was fired]
         * @private
         */
        _publishLinkData: function (_event) {
            var target = $(_event.target);
            this.trigger('linkpicked', target.data('ep-linkpicker-additionalpages-alias'), target.val());
        },

        /**
         * Fetches all available pages via AJAX call and fires the 'datafetched' event when ready
         * @private
         */
        getAdditionalPages: function () {

            ep.ajax({
                type: 'post',
                dataType: 'json',
                data: {
                    ViewAction: 'JSONAdvancedLinks',
                    ObjectID: ep.config.siteId
                }
            }).done($.proxy(function (data) {

                this.data = data;
                this.trigger('datafetched', true);
            }, this));
        }
    });
});