/* global registerBackward, DataProviderFacade, TraverseUtility */

registerBackward('1.0.10', 'Theme', function (control) {
    'use strict';

    var keyCategoryProducts = '',
        keyProductOverview = '', keyProductOverviewDel = '', overviewCount = 0,
        keyShoppingCart = '';
    var prop;

    for (prop in control.controls) {
        if (control.controls.hasOwnProperty(prop)) {
            if (control.controls[prop].constructorName === 'ProductOverview') {
                overviewCount++;
            }
        }
    }

    for (prop in control.controls) {
        if (control.controls.hasOwnProperty(prop)) {
            switch (control.controls[prop].constructorName) {
                case 'CategoryProducts':
                    keyCategoryProducts = prop;
                    break;
                case 'ProductOverview':
                    keyProductOverview = prop;
                    if (control.controls[prop].dataId !== 63229 && overviewCount > 1) {
                        keyProductOverviewDel = prop;
                        delete control.controls[prop];
                    }
                    break;
                case 'ShoppingCart':
                    keyShoppingCart = prop;
                    break;
            }
        }
    }

    if (DataProviderFacade.getCmsInfo().cmsName === 'WordPress') {
        var getContentArea = function () {
            return {
                "items": {
                    "length": 0,
                    "constructorName": "ControlsCollection",
                    "packageData": {
                        "id": 0,
                        "name": "BaseCollections",
                        "version": "0.0.1"
                    }
                },
                "constructorName": "ContentArea",
                "packageData": {
                    "id": 0,
                    "name": "PositionControls",
                    "version": "0.0.1"
                }
            };
        };
        var fixPlaceholder = function (oldValue, newValue, collection){
            for (var index in collection) {
                if (collection.hasOwnProperty(index)) {
                    if (collection[index] === '/controls/' + oldValue) {
                        collection[index] = '/controls/' + newValue;
                    }
                }
            }
        };
        var categoryProductCollection = TraverseUtility.getCollectionsWithElement(control, "/controls/" + keyCategoryProducts)[0];
        control.controls.contentAreaProducts = getContentArea();
        fixPlaceholder(keyCategoryProducts, 'contentAreaProducts', categoryProductCollection);

        var overviewCollection = TraverseUtility.getCollectionsWithElement(control, "/controls/" + keyProductOverview)[0],
            oldOverviewCollection = TraverseUtility.getCollectionsWithElement(control, "/controls/" + keyProductOverviewDel)[0];
        control.controls.contentAreaProductOverview = getContentArea();

        if (control.controls[keyProductOverview].dataId === 63229) {
            delete control.controls.tabControl;
            delete control.controls.productVariations;
            delete control.controls.imageThumbnails;
            delete control.controls.overviewImage;
        } else {
            delete control.controls['bd-productvariations2'];
        }

        for (var index in oldOverviewCollection) {
            if (oldOverviewCollection.hasOwnProperty(index)) {
                if (oldOverviewCollection[index] === '/controls/' + keyProductOverviewDel) {
                    delete oldOverviewCollection[index];
                    oldOverviewCollection.length--;
                }
            }
        }

        fixPlaceholder(keyProductOverview, 'contentAreaProductOverview', overviewCollection);

        var shoppingCartCollection = TraverseUtility.getCollectionsWithElement(control, "/controls/" + keyShoppingCart)[0];
        control.controls.contentAreaShoppingCart = getContentArea();
        fixPlaceholder(keyShoppingCart, 'contentAreaShoppingCart', shoppingCartCollection);
    }

    if (control.controls[keyCategoryProducts]) {
        control.controls.productListContainer = {
            items: control.controls[keyCategoryProducts].productsGrid.item.layouts[1].items,
            constructorName: "LayoutContainer"
        };
        control.controls[keyCategoryProducts].productsGrid.item.layouts[1].items = {
            0: '/controls/productListContainer',
            length: 1,
            constructorName: "LayoutItems"
        };
        control.prototypes[keyCategoryProducts] = control.controls[keyCategoryProducts];
        delete control.controls[keyCategoryProducts];
    }

    if (control.controls[keyProductOverview]) {
        control.prototypes[keyProductOverview] = control.controls[keyProductOverview];
        delete control.controls[keyProductOverview];
    }

    if (control.controls[keyShoppingCart]) {
        control.prototypes[keyShoppingCart] = control.controls[keyShoppingCart];
        delete control.controls[keyShoppingCart];
    }

    if (DataProviderFacade.getCmsInfo().cmsName === 'PrestaShop') {
        var sortContainer = control.controls.sortContainer;
        for (var i = 0; i < sortContainer.items.length; i++) {
            var sorterColumn = sortContainer.items[i];
            if (sorterColumn.items[0] === '/controls/productsSorter') {
                sorterColumn.width = '9';
            } else if (sorterColumn.items[0] === '/controls/itemsPerPage') {
                sorterColumn.width = '8';
            } else if (sorterColumn.items[0] === '/controls/productCompare3') {
                sorterColumn.width = '4';
            }
        }
    }

    return control;
});