/* global editor, Utils, UIProvider, ErrorUtility*/
/**
 *
 * @param {ProviderData} data
 * @constructor
 */
var UIPIconFontPage = function (data) {
    'use strict';

    UIPIconFontPage.superclass.constructor.call(this, data);

    this.iconTable = {
        '1Common': 'TypeCategoryIcons/icon-common.png',
        '2Objects': 'TypeCategoryIcons/icon-objects.png',
        '3eCommerceIcons': 'TypeCategoryIcons/icon-ecommerce.png',
        '4DirectionalIcons': 'TypeCategoryIcons/icon-directional.png',
        '5Bullets': 'TypeCategoryIcons/icon-bullets.png',
        '6BrandIcons': 'TypeCategoryIcons/icon-brand.png',
        '7Application': 'TypeCategoryIcons/icon-application.png'
    };

    var updateItems = function () {
        this.getItems(function (error, result) {
            ErrorUtility.logError(error);
            this.items(result);
        }.bind(this));
    }.bind(this);

    if (window.iconFontPageController) {
        $(window.iconFontPageController).on('categoriesChanged', updateItems);
    }

    /**
     *
     * @param {Object} value Structure with gallery item properties
     * @param {String} value.class Class name of the group.
     */
    this.click = function (value) {
        if (!editor.documentBody) {
            return;
        }

        var nodes = editor.documentBody.find('.' + value.class);
        if (!nodes || !nodes.length) {
            return;
        }
        var offsetTop = nodes[0].offsetTop;
        editor.documentContentWindow.scrollTop(offsetTop);
    };

    this.dispose = function () {
        UIPIconFontPage.superclass.dispose.call(this);

        if (window.iconFontPageController) {
            $(window.iconFontPageController).off('categoriesChanged', updateItems);
        }
    };
};

Utils.extend(UIPIconFontPage, UIProvider);

UIPIconFontPage.prototype.getItems = function (callback) {
    'use strict';
    var items = [];
    if (window.iconFontPageController)
        for (var i = 0; i < window.iconFontPageController.categories.length; i++) {
            var item = {
                caption: window.iconFontPageController.categories[i],
                class: window.iconFontPageController.categories[i],
                icon: this.iconTable[window.iconFontPageController.categories[i]],
                group: ''
            };
            items.push(item);
        }
    callback(null, items);
};