define([
    "jquery",
    "./button.state"
],
function(
    jq,
    ButtonState
){

    /**
     * @constructor
     */
    return function( options ){

        var buttonColors = {};
        buttonColors[ ButtonState.Default ] = options.colors.default;
        buttonColors[ ButtonState.Hover ] = options.colors.hover;
        buttonColors[ ButtonState.Pressed ] = options.colors.pressed;
        buttonColors[ ButtonState.Inactive ] = options.colors.inactive;

        function createSvgPath(){
            return  '<svg id="' + options.id + '" width="' + options.width + 'px" height="' + (options.height * 4) + 'px" viewBox="0 0 44 176" preserveAspectRatio="none" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="background-color: transparent;">' +
                            '<title>' + options.caption +'</title>' +
                 '<defs></defs>' +
                 '<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">' +
                 '<g>' +
                 '<g class="NavigationButton" transform="translate(0.000000, 0.000000)" fill="' + options.colors.default.bg + '">' +
                 '<rect x="0" y="0" width="44" height="44"></rect>' +
                 '<polygon class="NavigationButtonIcon" fill="' + options.colors.default.icon + '" fill-rule="nonzero" transform="translate(20.500000, 22.000000) rotate(-180.000000) translate(-20.500000, -22.000000) " points="13 12 28 21.9999994 13 32"></polygon>' +
                 '</g>' +
                 '</g>' +
                 '</g>' +
                 '</svg>';
        }

        function createButton(){

            var currentState = ButtonState.Default,
                svgButton = jq(document.createElement("div")),
                svg = svgButton.html( createSvgPath() )
                    .contents()
                    .addClass("sc-navigation")
                    .css({
                        "pointer-events" : "none"
                    })
                    .on("touchmove", function(e) {
                        e.preventDefault ? e.preventDefault() : true;
                    })
                    .get(0);

            var svgWrapper = jq(document.createElement("button"))
                .attr({
                    "title" : options.caption,
                    "class": currentState,
                    "role": "button",
                    "tab-index": "0"
                })
                .css({
                    "cursor": "pointer",
                    "width": options.width + "px",
                    "height": options.height + "px",
                    "overflow": "hidden",
                    "padding": "0",
                    "border": "none",
                    "background": "none"
                })

            if (options.ariaLabel || options.caption) {
                svgWrapper.attr({
                    "aria-label": options.ariaLabel ? options.ariaLabel : options.caption
                })
            }

            function changeState(buttonState) {

                if (currentState === buttonState) {
                    return;
                }

                var enabled = buttonState !== ButtonState.Inactive;

                var colors = buttonColors[buttonState],
                    jqSvg = jq(svg);

                svgWrapper.removeClass(currentState);
                currentState = buttonState;
                svgWrapper.addClass(buttonState);

                jqSvg.find("[class=NavigationButton]").attr("fill", colors.bg);
                jqSvg.find("[class=NavigationButtonIcon]").attr("fill", colors.icon);
                jqSvg.css({
                    "cursor": buttonState !== ButtonState.Inactive ? "pointer" : "default"
                });

                if (!enabled) {
                    svgWrapper.attr({
                        "aria-disabled": "true"
                    })
                } else {
                    svgWrapper.attr({
                        "aria-disabled": "false"
                    })
                }
            }

            function onBlur() {
                var buttonState = ButtonState.Default,
                    colors = buttonColors[buttonState],
                    jqSvg = jq(svg);

                svgWrapper.removeClass(currentState);
                currentState = buttonState;
                svgWrapper.addClass(buttonState);

                jqSvg.find("[class=NavigationButton]").attr("fill", colors.bg);
                jqSvg.find("[class=NavigationButtonIcon]").attr("fill", colors.icon);
                jqSvg.css({
                    "cursor": buttonState !== ButtonState.Inactive ? "pointer" : "default"
                });

                svgWrapper.off("focusout");
                svgWrapper.trigger("blur");
            }

            function onFocus() {
                var buttonState = ButtonState.Hover,
                    colors = buttonColors[buttonState],
                    jqSvg = jq(svg);

                svgWrapper.removeClass(currentState);
                currentState = buttonState;
                svgWrapper.addClass(buttonState);

                jqSvg.find("[class=NavigationButton]").attr("fill", colors.bg);
                jqSvg.find("[class=NavigationButtonIcon]").attr("fill", colors.icon);
                jqSvg.css({
                    "cursor": buttonState !== ButtonState.Inactive ? "pointer" : "default"
                });

                svgWrapper.trigger("focus");
                svgWrapper.on("focusout", onBlur);
            }

            svgWrapper.append(svg);

            return {
                getHotspotElement : function(){
                    return svgWrapper.get(0);
                },
                hasState : function( buttonState ){
                    return buttonState === currentState;
                },
                changeState : function( buttonState ){
                    changeState(buttonState);
                },
                focusButton: function () {
                    onFocus();
                },
                blurButton: function () {
                    onBlur();
                }
            };
        }

        return {
            build : createButton
        };
    };
});
