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(22.000000, 22.000000) rotate(-180.000000) translate(-22.000000, -22.000000) " points="29.183622 12 21.9993364 19.1833322 14.8177052 12 12.0013272 14.8146772 19.1856128 22.0006635 12 29.1853228 14.8150508 32 21.9993364 24.8140137 29.1862765 32 32 29.1866499 32 29.1839958 24.8143872 22.0006635 31.9973455 14.8160042"></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({
                    "width": options.width + "px",
                    "height": options.height + "px",
                    "cursor": "pointer",
                    "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 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"
                });
            }

            function onBlur() {
                var buttonState = ButtonState.Default;

                changeState(buttonState);

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

            function onFocus() {
                var buttonState = ButtonState.Hover;

                changeState(buttonState);

                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
        };
   };
});
