//
//  Tooltips
//  _____________________________________________

@mixin lib-tooltip(
    $_tooltip-position         : top,
    $_tooltip-selector-toggle  : ".tooltip-toggle",
    $_tooltip-selector-content : ".tooltip-content",
    $_tooltip-background       : $tooltip__background,
    $_tooltip-border-color     : $tooltip__border-color,
    $_tooltip-border-width     : $tooltip__border-width,
    $_tooltip-border-radius    : $tooltip__border-radius,
    $_tooltip-color            : $tooltip__color,
    $_tooltip-cursor           : $tooltip__cursor,
    $_tooltip-font-size        : $tooltip__font-size,
    $_tooltip-font-family      : $tooltip__font-family,
    $_tooltip-font-weight      : $tooltip__font-weight,
    $_tooltip-line-height      : $tooltip__line-height,
    $_tooltip-font-style       : $tooltip__font-style,
    $_tooltip-margin           : $tooltip__margin,
    $_tooltip-padding          : $tooltip__padding,
    $_tooltip-min-width        : $tooltip__min-width,
    $_tooltip-max-width        : $tooltip__max-width,
    $_tooltip-arrow-size       : $tooltip-arrow__size,
    $_tooltip-arrow-offset     : $tooltip-arrow__offset,
    $_tooltip-z-index          : $tooltip__z-index
) {
    position: relative;

    #{$_tooltip-selector-content} {
        @include lib-css(background, $_tooltip-background);
        @include lib-css(border-radius, $_tooltip-border-radius);
        @include lib-css(margin, $_tooltip-margin);
        @include lib-css(max-width, $_tooltip-max-width);
        @include lib-css(min-width, $_tooltip-min-width);
        @include lib-css(padding, $_tooltip-padding);
        @include lib-css(z-index, $_tooltip-z-index);
        display: none;
        position: absolute;
        text-align: left;

        @include lib-typography(
            $_color       : $_tooltip-color,
            $_font-size   : $_tooltip-font-size,
            $_font-family : $_tooltip-font-family,
            $_font-weight : $_tooltip-font-weight,
            $_line-height : $_tooltip-line-height,
            $_font-style  : $_tooltip-font-style
        );

        @include _lib-tooltip-border($_tooltip-border-width, $_tooltip-border-color);

        $_tooltip-arrow-after-margin: $_tooltip-arrow-size * -1;
        $_tooltip-arrow-before-margin: ($_tooltip-arrow-size + round($_tooltip-border-width * 1.41421356)) * -1;

        &:after,
        &:before {
            border: solid transparent;
            content: '';
            height: 0;
            position: absolute;
            width: 0;
        }

        &:after {
            @include lib-css(border-width, $_tooltip-arrow-size);
            border-color: transparent;
        }

        &:before {
            @include lib-css(border-width, $_tooltip-arrow-before-margin * -1);
            border-color: transparent;
        }

        @include _lib-tooltip-arrow-position(
            $_tooltip-position,
            $_tooltip-arrow-size,
            $_tooltip-background,
            $_tooltip-arrow-after-margin,
            $_tooltip-arrow-before-margin,
            $_tooltip-arrow-offset,
            $_tooltip-border-color
        );
    }

    & #{$_tooltip-selector-toggle} {
        @include lib-css(cursor, $_tooltip-cursor);
    }

    & #{$_tooltip-selector-toggle}:hover + #{$_tooltip-selector-content},
    & #{$_tooltip-selector-toggle}:focus + #{$_tooltip-selector-content},
    &:hover #{$_tooltip-selector-content} {
        display: block;
    }
}

@mixin _lib-tooltip-border(
    $_tooltip-border-width,
    $_tooltip-border-color
) {
    @if $_tooltip-border-width != 0 and $_tooltip-border-width != false and $_tooltip-border-color != false {
        @include lib-css(border, $_tooltip-border-width solid $_tooltip-border-color);
    }
}

@mixin _lib-tooltip-arrow-position(
    $_tooltip-position,
    $_tooltip-arrow-size,
    $_tooltip-background,
    $_tooltip-arrow-after-margin,
    $_tooltip-arrow-before-margin,
    $_tooltip-arrow-offset,
    $_tooltip-border-color
) {
    @if $_tooltip-position == top {
        @include lib-css(margin-bottom, $_tooltip-arrow-size);
        bottom: 100%;
        left: 0;

        &:after,
        &:before {
            top: 100%;
        }
        &:after {
            @include lib-css(border-top-color, $_tooltip-background);
            @include lib-css(left, $_tooltip-arrow-offset + $_tooltip-arrow-size);
            @include lib-css(margin-left, $_tooltip-arrow-after-margin);
        }

        &:before {
            @include lib-css(border-top-color, $_tooltip-border-color);
            @include lib-css(left, $_tooltip-arrow-offset + $_tooltip-arrow-size);
            @include lib-css(margin-left, $_tooltip-arrow-before-margin);
        }
    }

    @if $_tooltip-position == left {
        @include lib-css(margin-right, $_tooltip-arrow-size);
        right: 100%;
        top: 0;

        &:after,
        &:before {
            left: 100%;
        }

        &:after {
            @include lib-css(border-left-color, $_tooltip-background);
            @include lib-css(margin-top, $_tooltip-arrow-after-margin);
            @include lib-css(top, $_tooltip-arrow-offset + $_tooltip-arrow-size)
        }

        &:before {
            @include lib-css(border-left-color, $_tooltip-border-color);
            @include lib-css(margin-top, $_tooltip-arrow-before-margin);
            @include lib-css(top, $_tooltip-arrow-offset + $_tooltip-arrow-size);
        }
    }

    @if $_tooltip-position == right {
        @include lib-css(margin-left, $_tooltip-arrow-size);
        left: 100%;
        top: 0;

        &:after,
        &:before {
            right: 100%;
        }

        &:after {
            @include lib-css(border-right-color, $_tooltip-background);
            @include lib-css(margin-top, $_tooltip-arrow-after-margin);
            @include lib-css(top, $_tooltip-arrow-offset + $_tooltip-arrow-size);
        }

        &:before {
            @include lib-css(border-right-color, $_tooltip-border-color);
            @include lib-css(margin-top, $_tooltip-arrow-before-margin);
            @include lib-css(top, $_tooltip-arrow-offset + $_tooltip-arrow-size);
        }
    }

    @if $_tooltip-position == bottom {
        @include lib-css(margin-top, $_tooltip-arrow-size);
        left: 0;
        top: 100%;

        &:after,
        &:before {
            bottom: 100%;
        }

        &:after {
            @include lib-css(border-bottom-color, $_tooltip-background);
            @include lib-css(left, $_tooltip-arrow-offset + $_tooltip-arrow-size);
            @include lib-css(margin-left, $_tooltip-arrow-after-margin);
        }

        &:before {
            @include lib-css(border-bottom-color, $_tooltip-border-color);
            @include lib-css(left, $_tooltip-arrow-offset + $_tooltip-arrow-size);
            @include lib-css(margin-left, $_tooltip-arrow-before-margin);
        }
    }
}
