//
// Mixins and funcs for buttons
//

@function _button_hilight_color($c)
{
    //
    // Calculate the right top hilight color for buttons
    //
    // $c: base color;
    //
    @if      lightness($c) > 95% { @return #FFFFFF; }
    @else if lightness($c) > 90% { @return transparentize(#FFFFFF, 0.2); }
    @else if lightness($c) > 80% { @return transparentize(#FFFFFF, 0.5); }
    @else if lightness($c) > 50% { @return transparentize(#FFFFFF, 0.8); }
    @else if lightness($c) > 40% { @return transparentize(#FFFFFF, 0.9); }
    @else { @return transparentize(#FFFFFF, 0.98); }
}

@mixin _button_text_shadow($tc:$fg_color, $bg:$bg_color)
{
    //
    // Helper function for the text emboss effect
    //
    // $tc is the optional text color, not the shadow color
    //
    // TODO: This function needs a way to deal with special cases
    //

    $_shadow: _text_shadow_color($tc, $bg);

    @if lightness($tc) < 50%
    {
        text-shadow:      0 1px $_shadow;
        -gtk-icon-shadow: 0 1px $_shadow;
    }
    @else
    {
        text-shadow:      0 -1px $_shadow;
        -gtk-icon-shadow: 0 -1px $_shadow;
    }
}

@mixin button($t, $c:$bg_color, $tc:$fg_color, $edge: none, $backimage: null)
{
    //
    // Button drawing function
    //
    // $t:         button type
    // $c:         base button color for colored* types
    // $tc:        optional text color for colored* types
    // $edge:      set to none to not draw the bottom edge or specify a color to not
    //             use the default one
    // $backimage: additional background-image behind the default one
    //             (for the button.circular hack)
    //
    // Possible $t values:
    // normal, hover, active, insensitive, insensitive-active,
    // backdrop, backdrop-active, backdrop-insensitive, backdrop-insensitive-active,
    // osd, osd-hover, osd-active, osd-insensitive, osd-backdrop, undecorated
    //
    $_hilight_color: _button_hilight_color($c);
    $_button_edge:   if($edge == none, none, _widget_edge($edge));
    $_blank_edge:    if($edge == none, none, _widget_edge(transparentize($edge, 1)));
    $_button_shadow: 0 1px 2px transparentize($shadow_color, 0.03);

    @if $t == normal
    {
        //
        // Normal Button
        //
        background-image:    if($variant == 'light', linear-gradient(to top, darken($c, 4%) 2px, $c),
                                                     linear-gradient(to top, darken($c, 1%) 2px, $c)),
                                 $backimage;
        border-color:        if($c != $bg_color, _border_color($c), $borders_color);
        border-bottom-color: if($c != $bg_color, _border_color($c, true), $alt_borders_color);
        color:               $tc;
        outline-color:       transparentize($tc, 0.7);
        
        @include _button_text_shadow($tc, $c);
        @include _shadows(inset 0 1px $_hilight_color, $_button_edge, $_button_shadow);
    }
    @else if $t == hover
    {
        //
        // Hovered Button
        //
        border-color:        if($c != $bg_color, _border_color($c),       $borders_color);
        border-bottom-color: if($c != $bg_color, _border_color($c, true), $alt_borders_color);
        color:               $tc;
        outline-color:       transparentize($tc, 0.7);

        @if $variant == 'light'
        {
            background-image: linear-gradient(to top, $c, lighten($c, 1%) 1px),
                              $backimage;

            @include _button_text_shadow($tc, lighten($c, 6%));
            @include _shadows(inset 0 1px _button_hilight_color(lighten($c, 6%)), $_button_edge, $_button_shadow);
        }
        @else
        {
            background-image: linear-gradient(to top, darken($c, 1%), lighten($c, 1%), 1px),
                              $backimage;
            
            @include _button_text_shadow($tc, lighten($c, 6%));
            @include _shadows(inset 0 1px _button_hilight_color(darken($c, 2%)), $_button_edge, $_button_shadow);
        }
    }
    @else if $t == normal-alt
    {
        //
        // Normal Button Alternative Look
        //
        border-color:  if($c != $bg_color, _border_color($c, true), $alt_borders_color);
        color:         $tc;
        outline-color: transparentize($tc, 0.7);

        @include _button_text_shadow($tc, $c);

        @if $variant == 'light'
        {
            background-image: linear-gradient(to bottom, lighten($c, 5%) 20%, $c 90%);

            @include _shadows(inset 0 1px _button_hilight_color(lighten($c, 6%)),
                             $_button_edge, $_button_shadow);
        }
        @else
        {
            background-image: linear-gradient(to bottom, darken($c, 3%) 20%, darken($c, 6%) 90%);

            @include _shadows(inset 0 1px $_hilight_color,
                             $_button_edge, $_button_shadow);
        }
    }
    @else if $t == hover-alt
    {
        //
        // Hovered Button Alternative Look
        //
        border-color:  if($c != $bg_color, _border_color($c, true), $alt_borders_color);
        color:         $tc;
        outline-color: transparentize($tc, 0.7);

        @if $variant == 'light'
        {
            background-image: linear-gradient(to bottom, lighten($c, 9%) 10%, lighten($c, 4%) 90%);

            @include _shadows(inset 0 1px _button_hilight_color(lighten($c, 6%)),
                             $_button_edge, $_button_shadow);
        }
        @else
        {
            background-image: linear-gradient(inset 0 1px $_hilight_color,
                                             $_button_edge, $_button_shadow);
            
            @include _shadows(inset 0 1px $_hilight_color,
                             $_button_edge, $_button_shadow);
        }
    }
    @else if $t == active
    {
        //
        // Pushed Button
        //
        border-color:     if($c != $bg_color, _border_color($c), $borders_color);
        background-image: if($variant == 'light', image(darken($c, 14%)), image(darken($c, 9%)));
        color:            $tc;
        outline-color:    transparentize($tc, 0.7);
        text-shadow:      none;
        -gtk-icon-shadow: none;

        @include _shadows(inset 0 1px transparentize($_hilight_color, 1), $_button_edge);
    }
    @else if $t == insensitive
    {
        //
        // Insensitive Button
        //
        $_bg: if($c != $bg_color, mix($c, $base_color, 85%), $insensitive_bg_color);

        color:            if($tc != $fg_color, mix($tc, $_bg, 50%), $insensitive_fg_color);
        border-color:     if($c != $bg_color, _border_color($c), $insensitive_borders_color);
        background-image: image($_bg);
        text-shadow:      none;
        -gtk-icon-shadow: none;

        // White with 0 alpha to avoid an ugly transition, since no color means
        // black with 0 alpha
        //
        @include _shadows(inset 0 1px transparentize(#FFFFFF, 1), $_button_edge);
    }
    @else if $t == insensitive-active
    {
        //
        // Insensitive Pushed Button
        //
        $_bg: if($variant == 'light', darken(mix($c, $base_color, 85%), 8%), darken(mix($c, $base_color, 85%), 6%));
        $_bc: if($c != $bg_color, _border_color($c), $insensitive_borders_color);

        color:            if($c != $bg_color, mix($tc, $_bg, 60%), $insensitive_fg_color);
        border-color:     $_bc;
        background-image: image($_bg);

        // White with 0 alpha to avoid an ugly transition, since no color means
        // black with 0 alpha
        //
        @include _shadows(inset 0 1px transparentize(#FFFFFF, 1), $_button_edge);
    }
    @else if $t == backdrop
    {
        //
        // Backdrop Button
        //
        $_bg: if($c != $bg_color, $c, $backdrop_bg_color);
        $_bc: if($variant == 'light', $c, _border_color($c));

        background-image: image($_bg);
        border-color:     if($c != $bg_color, $_bc, $backdrop_borders_color);
        color:            if($tc != $fg_color, mix($tc, $_bg, 80%), $backdrop_fg_color);
        text-shadow:      none;
        -gtk-icon-shadow: none;

        @include _shadows(inset 0 1px transparentize(#FFFFFF, 1), $_blank_edge);
    }
    @else if $t == backdrop-active
    {
        //
        // Backdrop Pushed Button
        //
        $_bg: if($variant == 'light', darken(mix($c, $base_color, 85%), 8%), darken(mix($c, $base_color, 85%), 4%));
        $_bc: if($variant == 'light', $_bg, _border_color($c));

        background-image: image($_bg);
        border-color:     if($c  != $bg_color, $_bc,                $backdrop_borders_color);
        color:            if($tc != $fg_color, mix($tc, $_bg, 80%), $backdrop_fg_color);

        @include _shadows(inset 0 1px transparentize(white, 1), $_blank_edge);
    }
    @else if $t == backdrop-insensitive
    {
        //
        // Backdrop Insensitive Button
        //
        $_bg: if($c != $bg_color, mix($c, $base_color, 85%), $insensitive_bg_color);
        $_bc: if($variant == 'light', $_bg, _border_color($c));

        background-image: image($_bg);
        border-color:     if($c != $bg_color, $_bc,                $backdrop_borders_color);
        color:            if($c != $bg_color, mix($tc, $_bg, 35%), $backdrop_insensitive_color);
        text-shadow:      none;
        -gtk-icon-shadow: none;

        // White with 0 alpha to avoid an ugly transition, since no color means
        // black with 0 alpha
        //
        @include _shadows(inset 0 1px transparentize(white, 1), $_blank_edge);
    }
    @else if $t == backdrop-insensitive-active
    {
        //
        // Backdrop Insensitive Pushed Button
        //
        $_bg: if($variant == 'light', darken(mix($c, $base_color, 85%), 8%), darken(mix($c, $base_color, 85%), 4%));
        $_bc: if($variant == 'light', $_bg,                                  _border_color($c));

        background-image: image($_bg);
        border-color:     if($c != $bg_color, $_bc,                $backdrop_borders_color);
        color:            if($c != $bg_color, mix($tc, $_bg, 35%), $backdrop_insensitive_color);

        @include _shadows(inset 0 1px transparentize(white, 1), $_blank_edge);
    }
    @else if $t == osd
    {
        //
        // Normal OSD Button
        //
        $_bg: if($c != $bg_color, transparentize($c, 0.5), $osd_bg_color);

        background-clip:  padding-box;
        background-color: transparent;
        background-image: image($_bg);
        border-color:     $osd_borders_color;
        box-shadow:       inset 0 1px transparentize(white, 0.9);
        color:            $osd_fg_color;
        outline-color:    transparentize($osd_fg_color, 0.7);
        text-shadow:      0 1px black;
        -gtk-icon-shadow: 0 1px black;
    }
    @else if $t == osd-hover
    {
        //
        // Hovered OSD Button
        //
        $_bg: if($c != $bg_color, transparentize($c, 0.3), lighten($osd_bg_color, 12%));

        background-clip:  padding-box;
        background-color: transparent;
        background-image: image($_bg);
        border-color:     $osd_borders_color;
        box-shadow:       inset 0 1px transparentize(white, 0.9);
        color:            white;
        outline-color:    transparentize($osd_fg_color, 0.7);
        text-shadow:      0 1px black;
        -gtk-icon-shadow: 0 1px black;
    }
    @else if $t == osd-active
    {
        //
        // Active OSD Button
        //
        $_bg: if($c != $bg_color, $c, $osd_borders_color);

        background-clip:  padding-box;
        background-color: transparent;
        background-image: image($_bg);
        border-color:     $osd_borders_color;
        box-shadow:       none;
        color:            white;
        outline-color:    transparentize($osd_fg_color, 0.7);
        text-shadow:      none;
        -gtk-icon-shadow: none;
    }
    @else if $t == osd-insensitive
    {
        //
        // Insensitive OSD Button
        //
        background-clip:  padding-box;
        background-color: transparent;
        background-image: image($osd_insensitive_bg_color);
        border-color:     $osd_borders_color;
        box-shadow:       none;
        color:            $osd_insensitive_fg_color;
        text-shadow:      none;
        -gtk-icon-shadow: none;
    }
    @else if $t == osd-backdrop
    {
        //
        // Backdrop OSD Button
        //
        $_bg: if($c != $bg_color, transparentize($c, 0.5), $osd_bg_color);

        background-clip:  padding-box;
        background-color: transparent;
        background-image: image($_bg);
        border-color:     $osd_borders_color;
        box-shadow:       none;
        color:            $osd_fg_color;
        text-shadow:      none;
        -gtk-icon-shadow: none;
    }
    @else if $t == undecorated
    {
        //
        // Reset
        //
        background-color: transparent;
        background-image: none;
        border-color: transparent;
        text-shadow: none;
        -gtk-icon-shadow: none;

        @include _shadows(inset 0 1px transparentize(white, 1), $_blank_edge);
    }
}
