// com.tts.initial.html5.avatar.ben
// HTML4 / HTML5: No differences
define(["jquery"], function (jq) {

	"use strict";

    var CoffeeShopBuilder = function( options ){
        var element = [
            "living_room",
            "room",
            "window",
            "chair_1",
            "chair_2",
            "sideboard",
            "books",
            "drawing_1",
            "drawing_2",
            "drawing_3",
            "plant_1",
            "plant_2",
            "plant_3",
            "lamp"
        ];

        function createCoffeeShopScene(){
            jq(options.shellElement)
                .append( createElement(options.elementId) );

            createHotspot( options.shellElement );
        }

        function createElement(elementId){
	        var testId = "sc-scene-livingroom-" + element[elementId];
            return createSvg( testId, element[elementId] + ".svg");
        }

        function createSvg( testId, fileName ) {
            var svgWrapper = createSvgWrapper( testId ),
                jqSvg = svgWrapper.html("<object type=\"image/svg+xml\"></object>")
                .children()
                .first()
                .attr({
                    "width": "100%",
                    "height": "100%"
                })
                .css({
	                "pointer-events" : "none" //ensure event bubbling for swipe
                });

            if( options.colors ){
                addColorChange( jqSvg.get(0), options.colors );
            }

            jqSvg.attr( "data", options.componentPath + "svg/" + fileName );

            return svgWrapper;
        }

        function createSvgWrapper( testId ){
            return jq(document.createElement("div"))
                .css({
                    "position": "absolute",
                    "left": 0,
                    "top": 0,
                    "width": "100%",
                    "height": "100%"
                })
                .addClass( testId );
        }

        /**
         * Hotspot element to enable ttkf-trigger and interaktion handling
         *
         * @param shellElement
         */
        function createHotspot( shellElement ) {

            var hotspot = jq( document.createElement("div") )
                .css({
                    "position" : "absolute",
                    "width" : "100%",
                    "height" : "100%",
                    "cursor" : "auto",
                    "opacity" : "0"
                });

            shellElement.append( hotspot );
        }

        function addColorChange( svg, colors ) {
            svg.addEventListener("load", function () {
                var svgObj = jq(svg.contentDocument);
                jq.each(colors, function (cssClassSelector, color) {
                    var svgPart = svgObj.find("[class=" + cssClassSelector + "]");
                    svgPart.attr("fill", color);
                });
            }, false);
        }

        return {
            build : createCoffeeShopScene
        };
    };

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

		// create an object shield so we are able to select and drag element
		function createAuthoringShield(shellElement) {
			shellElement.prepend(jq(document.createElement("div"))
				.css({
                    "width": "100%",
                    "height": "100%",
                    "position": "absolute"
				}));
        }
        
        function getCoreCss(){

            var isStandardPlayer = context.getPlayerName() === "standard";

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

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

            return coreStyles;
		}

		function createCoffeeShopScene( shellElement ){
            shellElement.css( getCoreCss() );

            new CoffeeShopBuilder({
                shellElement : shellElement,
                componentPath : context.getPath(),
                elementId: metadata.parameters.asNumber("element", 0),
                colors: {
                    "tts-scene-c1" : metadata.parameters.get("color_c1", "#bfe8f8"),
                    "tts-scene-c2" : metadata.parameters.get("color_c2", "#c1272d"),
                    "tts-scene-c3" : metadata.parameters.get("color_c3", "#96aa04"),
                    "tts-scene-c4" : metadata.parameters.get("color_c4", "#ed1c24"),
                    "tts-scene-c5" : metadata.parameters.get("color_c5", "#af7c47"),
                    "tts-scene-c6" : metadata.parameters.get("color_c6", "#ffffff")
                }
            }).build();

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

		// revealing public API
		return {
			onLoading: function () {
				createCoffeeShopScene( jq(context.getElement()) );
			}
		};
	};
});
