// com.wacker.initial.html5.accordion
define([
    "./accordion.builder",
    "jquery"
    ], function (
        AccordionBuilder,
        jq
    ){

"use strict";

/**
* @constructor
*/
return function (context, metadata) {

    var shellElement,
        button,
        instance,
        isInAuthoringMode = false,
        buttonInitialized = false,
        maxElements = 7,
        fonts = [
            'Arial, Helvetica, sans-serif',
            '"Times New Roman", Times, serif',
            'Verdana, Geneva, sans-serif',
            '"Courier New", Courier, monospace',
            'Roboto, "Helvetica Neue", Helvetica, Arial, sans-serif',
            '"Droid Serif", "Times New Roman", Times, serif'
        ];

    function initializeButton(){
        if( buttonInitialized ){
            return;
        }
        buttonInitialized = true;

        var scParams = metadata.parameters,
            elementsArray = new Array();
        
        for(var i = 1; i <= maxElements; i++){
            if(!isEmptyOrSpaces(scParams.get( "title-" + i, "" )) && !isEmptyOrSpaces(scParams.get( "text-" + i, "" ))){
                elementsArray.push(loadElement(i));
            }
        }

        if(elementsArray.length == 0){
            var result = {};
            result.title = "Lorem ipsum ...";
            result.text = "Lorem ipsum ...";
            elementsArray.push(result);
        }

        button = new AccordionBuilder({
            width: scParams.asNumber("accordion-width", "300"),
            instance: instance,
            instanceId: metadata.getId(),
            elements : elementsArray,
            title : {
                fontSize : scParams.asNumber("title-font-size", "14"),
                fontFamily : getFont(scParams.asNumber("title-font-family", 0)),
                textColor : scParams.get("title-text-color", "#142534"),
                mouseoverTextColor : scParams.get("title-mouseover-text-color", "#142534"),
                textOrientation : scParams.get("title-text-orientation", "left"),
                paddingLeft : scParams.asNumber("title-padding-left", "15"),
                paddingRight : scParams.asNumber("title-padding-right", "15"),
                paddingTop : scParams.asNumber("title-padding-top", "15"),
                paddingBottom : scParams.asNumber("title-padding-bottom", "15"),
                backgroundColor : scParams.get("title-background-color", "#ebebeb"),
                mouseoverBackgroundColor : scParams.get("title-mouseover-background-color", "#e0e0e0"),
                backgroundTransparency : scParams.get( "title-background-transparency", "0"),
                lineHeight : scParams.get("font-size_title", "14")
            },
            text : {
                fontSize : scParams.asNumber("text-font-size", "14"),
                fontFamily : getFont(scParams.asNumber("text-font-family", 0)),
                textColor : scParams.get("text-text-color", "#142534"),
                textOrientation : scParams.get("text-text-orientation", "left"),
                paddingLeft : scParams.asNumber("text-padding-left", "15"),
                paddingRight : scParams.asNumber("text-padding-right", "15"),
                paddingTop : scParams.asNumber("text-padding-top", "15"),
                paddingBottom : scParams.asNumber("text-padding-bottom", "15"),
                backgroundColor : scParams.get("text-background-color", "#FFFFFF"),
                backgroundTransparency : scParams.get( "text-background-transparency", "0"),
                lineHeight : scParams.get("font-size_text", "14")
            },
            expandOnlyOne : scParams.asBool("expand-only-one"),
            showNumbering : scParams.asBool("show-numbering"),
            numberingColor : scParams.get("numbering-color", "#fd7326"),
            numberingMouseOverColor : scParams.get("numbering-mouseover-color", "#fd7326"),
            showIcons : scParams.asBool("show-icons"),
            icons : loadImage("icons", scParams.get("icon-image", null))
        }, context, metadata).build();

        shellElement.css({
            "width": scParams.asNumber("accordion-width", "300") + "px",
        });

        shellElement.append( button.getElement() );

        if (isInAuthoringMode) {
            createAuthoringShield();
        }
    }

    function loadImage(key, image) {

        var result = {};

            if (image) {
                result.fileNameURL = context.stage.getSharedPath() + image.fileName;
                result.width = image.width;
                result.height = image.height;
            }
            else {
                switch(key){
                    case "icons": 
                        result.fileNameURL = context.getPath() + "icons.png";
                        result.width = 35;
                        result.height = 40;
                        break;
                    default:
                        return null;
                } 
            }      

        return result;
    }

    function isEmptyOrSpaces(str){
        return (!str || str.length === 0 || /^\s*$/.test(str))
        //return str === null || str.match(/^ *$/) !== null;
    }

    function getFont( index ) {
        var fontIndex = (index > 0 && index < fonts.length) ? index-1: 0;
        return fonts[fontIndex];
    }

    function loadElement(number){
        var scParams = metadata.parameters,
            result = {};
        result.title = scParams.get( "title-" + number, "" );
        result.text = scParams.get( "text-" + number, "" );

        return result;
    }

    // create an object shield so we are able to select and drag element
    function createAuthoringShield() {
        shellElement.append(jq(document.createElement("div"))
            .css({
                "position": "absolute",
                "left": 0,
                "top": 0,
                "background-color": "transparent",
                "opacity": "0.4",
                "width": "100%",
                "height": "100%",
                "z-index": "9999"
            }));
    }

    // revealing public API
    return {
        // declare public members
        onLoading: function () {
            instance = this;

            context.page.event.bindOnEnter(this);
            context.page.event.bindOnExit(this);

            shellElement = jq(context.getElement());

            isInAuthoringMode = !!context.stage.isInAuthoringMode();

            initializeButton();
        },

        onEnterPage: function() {
            button.bindEvents();
        },

        onExitPage: function () {
            button.collapseAll();
            button.removeEvents();
        }
    };
};
});