// com.tts.initial.html5.textbutton
// HTML4 / HTML5: No differences
define( [
    "./accessibility.builder",
    "jquery"
    ], function (
        AccessibilityBuilder,
        jq
    ) {

    "use strict";

	var ButtonState = {
            Default : "btn_state_default",
            Hover : "btn_state_hover",
            Pressed : "btn_state_pressed"
        },
        isIOS = (/iphone|ipad|ipod/i.test( navigator.userAgent.toLowerCase() ) ),

        ButtonBuilder = function( options ){

            var backgroundColors = {},
                textColors = {};

            backgroundColors = {};
            backgroundColors[ ButtonState.Default ] = options.color.background;
            backgroundColors[ ButtonState.Hover ] = options.color.backgroundHover;
            backgroundColors[ ButtonState.Pressed ] = options.color.backgroundPressed;

            textColors = {};
            textColors[ ButtonState.Default ] = options.color.text;
            textColors[ ButtonState.Hover ] = options.color.textHover;
            textColors[ ButtonState.Pressed ] = options.color.textPressed;

            function createTextContent() {

                if (jq.trim(options.text.content) === "") {
                    return "";
                }

                var textContainer = "";
                if (options.textParams.verticalAlignment === "middle") {
                    textContainer = jq(document.createElement("div"))
                        .attr({
                            "id" : "textContent" + options.id
                        })
                        .css({
                            "width": "auto",
                            "padding-right": options.textParams.paddingRight + "px",
                            "padding-left": options.textParams.paddingLeft + "px",
                            "vertical-align": "middle",
                            "display": "table-cell"
                        });
                }
                else {
                    textContainer = jq(document.createElement("div"))
                        .attr({
                            "id" : "textContent" + options.id
                        })
                        .css({
                            "position": "absolute",
                            "padding": options.textParams.paddingTop + "px " + options.textParams.paddingRight + "px " + options.textParams.paddingBottom + "px " + options.textParams.paddingLeft + "px"
                        });

                    if (options.textParams.verticalAlignment === "bottom" || options.textParams.verticalAlignment === "top" ) {
                        textContainer.css(options.textParams.verticalAlignment, "0");
                    }

                    if( options.textParams.orientation === "right" ) {
                        textContainer.css(options.textParams.orientation, "0");
                    }
                }

                if(options.tooltip){
                    textContainer
                        .attr({
                            "title" : options.tooltip
                        })
                }

                textContainer.html(options.text.content);
                return textContainer;
            }

            function createButton() {

                var currentState = ButtonState.Default,
                    button = jq(document.createElement("div"))
                        .attr({
                            "class" : currentState
                        })
                        .css({
                        "width": options.width + "px",
                        "height": options.height + "px",
                        "text-align": options.textParams.orientation,
                        "font-family": options.textParams.fontFamily,
                        "color": options.color.text,
                        "background-color": options.color.background,
                        "-webkit-tap-highlight-color": "rgba(0,0,0,0)",
                        "font-size": Math.min( Math.abs(options.textParams.fontSize), 100) + "px",
                        "cursor": "pointer"
                    })
                    
                    .on("touchmove", function(e) {
                        e.preventDefault ? e.preventDefault() : true;
                    });

                if (options.textParams.verticalAlignment === "middle") {
                    button.css("display", "table");
                }

                if(options.tooltip){
                    button
                        .attr({
                            "title" : options.tooltip
                        })
                }

                button.append( createTextContent() );

                return {
                    getRootElement : function(){
                        return button;
                    },
                    getHotspotElement : function(){
                        return button;
                    },
                    hasState : function( buttonState ){
                        return buttonState === currentState;
                    },
                    changeState : function( buttonState){     
                        if( !buttonState ){
                            buttonState = currentState;
                        }
            
                        button.removeClass( currentState );
                        currentState = buttonState;
                        button.addClass( buttonState );
            
                        button.css({
                            "background-color": backgroundColors[buttonState],
                            "color": textColors[buttonState],
                            "cursor": "pointer"
                        });
                    }
                };
            }

            return {
                build : createButton
            };
        },
        
        EventUtil = (function() {

        function getFirstSupportedEvent(tagName, eventNames) {
            var result = "";
            jq.each(eventNames.split(" "), function (i, evtName) {
                if (isEventSupported(evtName, tagName)) {
                    result = evtName;
                    return false;
                }
            });
            return result;
        }

        function isEventSupported(eventName, tagName) {
            var elem = tagName ? document.createElement(tagName) : document,
                onEventName = "on" + eventName,
                isSupported;

            isSupported = !!(onEventName in elem);
            if (!isSupported && typeof elem.setAttribute !== "undefined") {
                elem.setAttribute(onEventName, "return;");
                isSupported = typeof elem[onEventName] === "function";
            }

            return isSupported;
        }

        return {
            /**
             * Attach the event handler <b>handler</b> for the first supported event specified by <b>eventNames</b>
             * to the element <b>jqObject</b>.
             * Example: attachFirstSupportedEvent( myObject, "pointerdown mousedown touchend", myHandler );
             *
             * @param element
             * @param {String} eventNames
             * @param handler
             * @return {String} the event name or ""
             */
            attachFirstSupportedEvent: function (element, eventNames, handler) {
                var jqObject = jq(element),
                    evtName = getFirstSupportedEvent(element.nodeName, eventNames);

                if (!evtName) {
                    return "";
                }

                jqObject.on(evtName, handler);
                return evtName;
            },

            /**
             * Remove attached event handler <b>handler</b> for the first supported event specified by <b>eventNames</b>
             * from the element <b>jqObject</b>.
             * Example: removeFirstSupportedEvent( myObject, "pointerdown mousedown touchend", myHandler );
             *
             * @param element
             * @param {String} eventNames
             * @param handler
             * @return {String} the event name or ""
             */
            removeFirstSupportedEvent: function (element, eventNames, handler) {
                var jqObject = jq(element),
                    evtName = getFirstSupportedEvent(element.nodeName, eventNames);

                if (!evtName) {
                    return "";
                }

                jqObject.off(evtName, handler);
                return evtName;
            },
            
            getPointerDownEvent: function( element ) {
                if( !isIOS ) {
                    return getFirstSupportedEvent( element.nodeName, "pointerdown mousedown touchstart" );
                }

                // iOS 9.3 unterstützt zwar click und mouseup, weshalb sich auf diese Events attached wird, aber sie werden
                //  aus unbekannten Gründen nicht gesendet.
                return "touchstart";
            },

            getPointerUpEvent: function( element ) {

                if( !isIOS ) {
                    return getFirstSupportedEvent( element.nodeName, "click pointerup mouseup touchend" );
                }

                // iOS 9.3 unterstützt zwar click und mouseup, weshalb sich auf diese Events attached wird, aber sie werden
                //  aus unbekannten Gründen nicht gesendet.
                return "touchend";
            }
        };
    })();

    /**
     * @constructor
     */
    return function (context, metadata) {
        var instance,
            shellElement,
            button,
            buttonInitialized = false,
            hasStageTriggerSupport = true,
            isInAuthoringMode = false,
            isInPreviewMode = false,
            pointerUpEventName,
            pointerDownEventName,
            pointerMoveEventName,
            pointerLeaveEventName,
            isStandardPlayer = context.getPlayerName() === "standard",
            accessibilityTooltip,
            accessibilityKey = metadata.parameters.get("accessibility-key", "KeyNone" ),
            accessibilityLocation = metadata.parameters.asNumber( "accessibility-location", 0 ),
            accessibilityPositionX = metadata.parameters.asNumber( "accessibility-position-x", -6 ),
            accessibilityPositionY = metadata.parameters.asNumber( "accessibility-position-y", -6 );

        function asRgbaValue(colorParameterName, defaultColor, transparencyParameterName) {

            var color = hexToRgb(metadata.parameters.get(colorParameterName, defaultColor)),
                alpha = asAlpha(metadata.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;
        }

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

        function lockNavigation() {
			if (isInAuthoringMode || !hasStageTriggerSupport) {
                return;
            }
            context.stage.trigger.lock(metadata.getId());
        }

        function unlockNavigation() {
			if (isInAuthoringMode || !hasStageTriggerSupport) {
                return;
            }
            context.stage.trigger.unlock(metadata.getId());
        }

        function onMouseOver() {
            button.changeState(ButtonState.Hover);
            fireTriggerOnMouseOver();
        }

        function onMouseLeave() {
            button.changeState(ButtonState.Default);
            fireTriggerOnMouseLeave();
        }

        function onMouseDown(e) {
            cancelEvent(e);
            lockNavigation();
            button.changeState(ButtonState.Pressed);
        }

        function onMouseUp(e) {
            cancelEvent(e);
            unlockNavigation();

            button.changeState(ButtonState.Hover);

            if(!isOnTarget(e)) {
                return;
            }

            button.changeState(ButtonState.Default);
            fireTriggerOnUp();
        }

        function onAccessibilityKeyPressed() {
            button.changeState(ButtonState.Hover);
            button.changeState(ButtonState.Default);
            fireTriggerOnUp();
        }

        function cancelEvent(e){
            if ( isStandardPlayer ) {
                e.preventDefault();
                e.stopPropagation();
            }
        }

        function isOnTarget( e ) {
            var elem = e.target,
                origEvent = e.originalEvent,
                changedTouch;

            if ( !origEvent ) {
                return true;
            } else {
                changedTouch = origEvent.changedTouches && origEvent.changedTouches.length > 0 ? origEvent.changedTouches[0] : undefined;
            }

            if ( changedTouch ) {
                elem = document.elementFromPoint( changedTouch.clientX, changedTouch.clientY );
            }

            return e.target === elem;
        }

        function fireTriggerOnMouseOver(){
            setTimeout(function(){
                context.page.event.trigger(instance, "tg_mouseover");
            }, 10);
        }

        function fireTriggerOnMouseLeave(){
            setTimeout(function(){
                context.page.event.trigger(instance, "tg_mouseleave");
            }, 10);
        }

        function fireTriggerOnUp(){
            setTimeout(function(){
                context.page.event.trigger(instance, "tg_pressed");
            }, 10);
        }

        function initPointerEventNames(element) {
            isInPreviewMode = !!context.stage.isInPreviewMode();
            if( !pointerUpEventName ) {
                pointerUpEventName = isInPreviewMode ? "mouseup" : EventUtil.getPointerUpEvent( element );
                pointerDownEventName = isInPreviewMode ? "mousedown" : EventUtil.getPointerDownEvent( element );
                pointerMoveEventName = isInPreviewMode ? "mousemove" : "pointermove mousemove";
                pointerLeaveEventName = isInPreviewMode ? "mouseend mouseout" : "pointerout mouseout";
            }
        }

        function bindEvents() {

            if (context.stage.isInAuthoringMode()) {
                return;
            }

            if (!button || !buttonInitialized) {
                return;
            }

            context.page.interaction.provokesFeedback( button.getHotspotElement(), false );
            context.page.interaction.provokesAutosolve( button.getHotspotElement(), false );

            removeEvents();
            
            EventUtil.attachFirstSupportedEvent( button.getHotspotElement(), pointerDownEventName, onMouseDown );
            EventUtil.attachFirstSupportedEvent( button.getHotspotElement(), pointerMoveEventName, onMouseOver );
            EventUtil.attachFirstSupportedEvent( button.getHotspotElement(), pointerLeaveEventName, onMouseLeave );
            EventUtil.attachFirstSupportedEvent( button.getHotspotElement(), pointerUpEventName, onMouseUp );
        }

        function removeEvents() {
            unlockNavigation();
            if (!button || !buttonInitialized) {
                return;
            }

            initPointerEventNames(button.getHotspotElement());
            EventUtil.removeFirstSupportedEvent( button.getHotspotElement(), pointerDownEventName, onMouseDown );
            EventUtil.removeFirstSupportedEvent( button.getHotspotElement(), pointerMoveEventName, onMouseOver );
            EventUtil.removeFirstSupportedEvent( button.getHotspotElement(), pointerLeaveEventName, onMouseLeave );
            EventUtil.removeFirstSupportedEvent( button.getHotspotElement(), pointerUpEventName, onMouseUp );
        }

        function getText( parameterName ) {
            var paramValue = jq.trim( metadata.parameters.get(parameterName,"") );
			return ( paramValue.length > 0 ) ? paramValue : null;
        }

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

            shellElement.css({
                "width": metadata.getWidth() + "px",
                "height": metadata.getHeight() + "px"
                });

            button = createButton();

            shellElement.append( button.getRootElement() );

            if ( accessibilityKey !== "KeyNone" ) {
                accessibilityTooltip = new AccessibilityBuilder( accessibilityKey, accessibilityLocation, accessibilityPositionX, accessibilityPositionY ).build();
                shellElement.append( accessibilityTooltip.getElement() );
            }

            context.page.interaction.provokesFeedback( button.getHotspotElement(), false );
            context.page.interaction.provokesAutosolve( button.getHotspotElement(), false );

            if (isInAuthoringMode) {
                createAuthoringShield( shellElement );
            }

            button.changeState( ButtonState.Default);
        }

        function createButton(){
            var scParams = metadata.parameters,
                button = new ButtonBuilder({
                    width: metadata.getWidth(),
                    height: metadata.getHeight(),
                    tooltip : getText("tooltip"),
                    instance : instance,
                    color : {
                        background: asRgbaValue("background-color", "#999999", "background-color-transparency"),
                        backgroundHover: asRgbaValue("background-color-hover", "#adadad", "background-color-hover-transparency"),
                        backgroundPressed: asRgbaValue("background-color-pressed", "#fd7326", "background-color-pressed-transparency"),
                        text : scParams.get("text-color", "#000000"),
                        textHover : scParams.get("text-color-hover", "#000000"),
                        textPressed : scParams.get("text-color-pressed", "#000000")
                    },
                    text : {
                        content : scParams.get("text", "Text")
                    },
                    textParams : {
                        fontFamily : getFontStyle( scParams.asNumber("font-family", "1") ),
                        fontSize : scParams.asNumber("font-size", "14"),
                        orientation : scParams.get("text_orientation", "center"),
                        verticalAlignment: scParams.get("text_vertical_align", "middle"),
                        paddingLeft : Math.abs(scParams.asNumber("padding-left", "10")),
                        paddingRight : Math.abs(scParams.asNumber("padding-right", "10")),
                        paddingTop : Math.abs(scParams.asNumber("padding-top", "10")),
                        paddingBottom : Math.abs(scParams.asNumber("padding-bottom", "10"))
                    }
                }).build();

            return button;
        }

        function getFontStyle( paramValue ) {
            switch( paramValue ) {
                case 2:
                    return "\"Times New Roman\", Times, serif";
                case 3:
                    return "Verdana, Geneva, sans-serif";
                case 4:
                    return "\"Courier New\", Courier, monospace";
                default:
                    return "Arial, Helvetica, sans-serif";
            }
        }

        // revealing public API
        return {
            onLoading: function () {
                instance = this;
                context.page.event.bindOnEnter(this);
                context.page.event.bindOnExit( this );
                if ( accessibilityKey !== "KeyNone" ) {
                    context.stage.accessibility.bindOnAccessibilityModeChanged( this );
                }

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

                shellElement = jq(context.getElement());

                initializeButton();
            },

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

            onExitPage: function() {
                removeEvents();
            },

            onAccessibilityModeChanged: function ( dataHTML5, dataHTML4 ) {
                var params = dataHTML4 || dataHTML5;

                if ( params.isActive ) {
                    accessibilityTooltip.show();
                } else {
                    accessibilityTooltip.hide();
                }
            },

            onAccessibilityKeyPressed: function ( dataHTML5, dataHTML4 ) {
                var params = dataHTML4 || dataHTML5;

                if ( params.keyCode == accessibilityTooltip.getKeyCode() ) {
                    onAccessibilityKeyPressed();
                    context.stage.accessibility.turnAccessibilityModeOff();
                }
            }
        };
    };
});
