// com.tts.initial.html5.document.properties
define(["jquery"], function (jq) {

    "use strict";

    function DocumentPropertiesTextBoxBuilder(metadata) {

        var parameters = metadata.parameters,
            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 getFont(index) {
            var fontIndex = (index > 0 && index < fonts.length) ? index - 1 : 0;
            return fonts[fontIndex];
        }

        function asRgbaValue(colorParameterName, defaultColor, transparencyParameterName) {

            var color = hexToRgb(parameters.get(colorParameterName, defaultColor)),
                alpha = asAlpha(parameters.asNumber(transparencyParameterName, 0));

            return "rgba(" + color.r + ", " + color.g + ", " + color.b + ", " + alpha + ")";
        }

        function hexToRgb(hex) {
            var parsingResult = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
            return parsingResult ? {
                r: parseInt(parsingResult[1], 16),
                g: parseInt(parsingResult[2], 16),
                b: parseInt(parsingResult[3], 16)
            } : {
                    r: 0,
                    g: 0,
                    b: 0
                };
        }

        function asAlpha(percentValue) {
            return (100 - percentValue) / 100;
        }

        function getProperty(value) {
            var result;

            switch (value) {
                case 1:
                    result = metadata.stage.getImprint();
                    break;
                case 2:
                    result = metadata.stage.getContactInformation();
                    break;
                default:
                    result = "No value avalaible!";
            }

            return result;
        }

        function createDocumentpropertiesTextbox() {

            var fontSize = parameters.get("font-size", "14"),
                borderSize = parameters.get("border-size", "1"),
                textbox,
                propertyHtml = getProperty(parameters.asNumber("property", 1));

            textbox = jq(document.createElement("div"))
                .css({
                    "width": "100%",
                    "background-color": asRgbaValue("background-color", "#cccccc", "background-transparency"),
                    "font-family": getFont(parameters.asNumber("font-family", 0)),
                    "font-size": fontSize + "px",
                    "line-height": fontSize + "px",
                    "padding-left": (parameters.asNumber("padding-left", "10") - parseInt(borderSize)) + "px",
                    "padding-right": (parameters.asNumber("padding-right", "10") - parseInt(borderSize)) + "px",
                    "padding-top": (parameters.asNumber("padding-top", "12") - parseInt(borderSize)) + "px",
                    "padding-bottom": (parameters.asNumber("padding-bottom", "6") - parseInt(borderSize)) + "px",
                    "text-align": parameters.get("text_orientation", "center"),
                    "box-sizing": "border-box",
                    "border": (borderSize > 0) ? borderSize + "px solid " + asRgbaValue("border-color", "#98a7b5", "border-transparency") : "0"
                })
                .html(propertyHtml);

            return textbox;
        }

        return {

            build: function () {

                var textboxDiv = createDocumentpropertiesTextbox();

                return {
                    getTextboxDiv: function () {
                        return textboxDiv;
                    },
                    setCss: function (params) {
                        textboxDiv.css(params);
                    }
                }
            }
        }
    }

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

        // declare private members
        var parameters = metadata.parameters,
            isStandardPlayer = context.getPlayerName() === "standard",
            textbox,
            textboxDiv,
            shellElement;

        function createComponent() {
            textbox = new DocumentPropertiesTextBoxBuilder(metadata).build();
            shellElement = jq(context.getElement());
            textboxDiv = textbox.getTextboxDiv();

            shellElement.css(getCoreCss())
                .append(textboxDiv);

            if (parameters.asNumber("auto-adjust-height", "1")) {
                shellElement.css({
                    "height": textbox.getTextboxDiv().outerHeight(true) + "px"
                })
            } else {
                textbox.setCss({
                    "height": "100%"
                })
            }

            if (context.stage.isInAuthoringMode()) {
                shellElement.prepend( createAuthoringShield() );
            }
        }

        function getCoreCss(){

            var coreStyles = {
				"cursor" : "default"
            };

            if( !isStandardPlayer ) {
                coreStyles["width"] = "100%";
                coreStyles["height"] = "100%";
            }

            return coreStyles;
		}

		function createAuthoringShield() {
			return jq( document.createElement("div") )
				.css({
					"width" : "100%",
					"height" : "100%",
					"position" : "absolute",
					"opacity" : "0.15",
					"background-image" : parameters.get("background-transparency", "0" ) === 100 ? "url(\"" + context.getPath() + "invis.png\")" : ""
				});
		}

		// revealing public API
		return {
            onLoading: function () {
                createComponent();
            }
        };
	};
});
