// Drawing mixins

// generic drawing of more complex things

@function _widget_edge($c:$borders_edge) {
// outer highlight "used" on most widgets
  @if $c == none { @return none; }
  @else { @return 0 1px $c; }
}

@mixin _shadows($list...) {
//
// Helper mixin to stack up to box-shadows;
//
  $shadows: null;

  @each $shadow in $list {
    @if $shadow!=none { $shadows: $shadows, $shadow; }
  }

  box-shadow: $shadows;
}

$focus_border_color: gtkalpha($accent_color, 0.5);

// entries
@function entry_focus_border($fc:$focus_border_color) {
  @return $fc;
}

@function entry_focus_shadow($fc:$focus_border_color) { @return inset 0 0 0 1px $fc; }

@mixin entry($t, $fc:$focus_border_color, $edge: none) {
//
// Entries drawing function
//
// $t: entry type
// $fc: focus color
// $edge: set to none to not draw the bottom edge or specify a color to not
//        use the default one
//
// possible $t values:
// normal, focus, insensitive, backdrop, backdrop-insensitive, osd, osd-focus, osd-backdrop;
//

  $_blank_edge: if($edge == none, none, 0 1px transparentize($edge, 1));
  $_entry_edge: if($edge == none, none, _widget_edge($edge));

  $entry_normal: if($variant == 'light', gtkmix(currentColor, $bg_color, 8%), gtkmix(currentColor, $bg_color, 9.7%));
  $entry_disabled: gtkmix(currentColor, $bg_color, 10%);
  $entry_backdrop: $entry_disabled;
  $entry_backdrop_disabled: $entry_disabled;

  @if $t==normal {
    color: $text_color;
    border-color: transparent;
    background-color: $entry_normal;
    @include _shadows(entry_focus_shadow(gtkalpha($fc, 0)), $_entry_edge);
    // for the transition to work the number of shadows in different states needs to match, hence the transparent shadow here.
  }
  @if $t==focus {
    @include _shadows(entry_focus_shadow($fc), $_entry_edge);
    border-color: entry_focus_border($fc);
    transition: 300ms ease-in-out;
    transition-property: border, box-shadow;
  }
  @if $t==insensitive {
    color: $insensitive_fg_color;
    border-color: transparent;
    background-color: $entry_disabled;
  }
  @if $t==backdrop {
    color: $backdrop_text_color;
    border-color: transparent;
    background-color: $entry_backdrop;
  }
  @if $t==backdrop-insensitive {
    color: $backdrop_insensitive_color;
    border-color: transparent;
    background-color: $entry_backdrop_disabled;
  }
  @if $t==osd {
    color: $osd_text_color;
    border-color: $osd_borders_color;
    background-color: transparentize(opacify($osd_borders_color, 1), 0.5);
    background-clip: padding-box;
    box-shadow: none;
    text-shadow: 0 1px black;
    -gtk-icon-shadow: 0 1px black;
  }
  @if $t==osd-focus {
    color: $osd_text_color;
    border-color: $selected_bg_color;
    background-color: transparentize(opacify($osd_borders_color, 1), 0.5);
    background-clip: padding-box;
    box-shadow: entry_focus_shadow($fc);
    text-shadow: 0 1px black;
    -gtk-icon-shadow: 0 1px black;
  }
  @if $t==osd-insensitive {
    color: $osd_insensitive_fg_color;
    border-color: $osd_borders_color;
    background-color: $osd_insensitive_bg_color;
    background-clip: padding-box;
    box-shadow: none;
    text-shadow: none;
    -gtk-icon-shadow: none;
  }
  @if $t==osd-backdrop {
    color: $osd_text_color;
    border-color: $osd_borders_color;
    background-color: transparentize(opacify($osd_borders_color, 1), 0.5);
    background-clip: padding-box;
    box-shadow: none;
    text-shadow: none;
    -gtk-icon-shadow: none;
  }
}

// buttons

@function _border_color($c, $darker: false) {
  @if $darker == true { @return gtkshade($c, 0.8); }
  @else { @return gtkshade($c, 0.9); }
}

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

  $button_normal: if($variant == 'light', gtkmix(currentColor, $c, 10%), gtkmix(currentColor, $c, 9.7%));
  $button_hover: if($variant == 'light', gtkmix(currentColor, $c, 14.7%), gtkmix(currentColor, $c, 15%));
  $button_active: if($variant == 'light', gtkmix(currentColor, $c, 30%), gtkmix(currentColor, $c, 29.7%));
  $button_active_hover: if($variant == 'light', gtkmix(currentColor, $c, 35%), gtkmix(currentColor, $c, 34.7%));
  $button_disabled: gtkmix(currentColor, $c, 10%);
  $button_disabled_active: gtkmix(currentColor, $c, 30%);
  $button_backdrop: $button_disabled;
  $button_backdrop_active: $button_disabled_active;

  @if $t==normal {
  //
  // normal button
  //
    color: $tc;
    outline-color: $focus_border_color;
    background-color: $button_normal;
  }

  @else if $t==hover {
  //
  // hovered button
  //
    color: $tc;
    background-color: $button_hover;
    box-shadow: none;
  }

  @if $t==normal-alt {
  //
  // normal button alternative look
  //
    @include button(normal);
  }

  @else if $t==hover-alt {
  //
  // hovered button alternative look
  //
    @include button(hover);
  }

  @else if $t==active {
  //
  // pushed button
  //
    color: $tc;
    background-color: $button_active;
    box-shadow: none;

    text-shadow: none;
    -gtk-icon-shadow: none;
    &:hover { background-color: $button_active_hover; }
  }

  @else if $t==insensitive {
  //
  // insensitive button
  //
    $_bg: if($c != $bg_color, gtkmix($c, $base_color, 85%), $insensitive_bg_color);

    color: if($tc != $fg_color, gtkmix($tc, $_bg, 50%), $insensitive_fg_color);
    background-color: $button_disabled; //gtkalpha(currentColor, .09);
    text-shadow: none;
    -gtk-icon-shadow: none;
    box-shadow: none;
  }

  @else if $t==insensitive-active {
  //
  // insensitive pushed button
  //
    $_bg: if($variant == 'light', gtkshade(gtkmix($c, $base_color, 85%), 0.92), gtkshade(gtkmix($c, $base_color, 85%), 0.94));
    $_bc: if($c != $bg_color, _border_color($c), $insensitive_borders_color);

    color: if($c != $bg_color, gtkmix($tc, $_bg, 60%), $insensitive_fg_color);
    background-color: $button_disabled_active; //gtkalpha(currentColor, .28);
    box-shadow: none;
  }

  @else if $t==backdrop {
  //
  // backdrop button
  //
    $_bg: if($c != $bg_color, $c, $backdrop_bg_color);
    $_bc: if($variant == 'light', $c, _border_color($c));

    color: if($tc != $fg_color, gtkmix($tc, $_bg, 80%), $backdrop_fg_color);
    background-color: $button_backdrop;
    text-shadow: none;
    -gtk-icon-shadow: none;
  }

  @else if $t==backdrop-active {
  //
  // backdrop pushed button
  //
    $_bg: if($variant == 'light', gtkshade(gtkmix($c, $base_color, 85%), 0.92), gtkshade(gtkmix($c, $base_color, 85%), 0.96));
    $_bc: if($variant == 'light', $_bg ,_border_color($c));

    color: if($tc != $fg_color, gtkmix($tc, $_bg, 80%), $backdrop_fg_color);
    background-color: $button_backdrop_active;
  }

  @else if $t==backdrop-insensitive {
  //
  // backdrop insensitive button
  //
    $_bg: if($c != $bg_color, gtkmix($c, $base_color, 85%), $insensitive_bg_color);
    $_bc: if($variant == 'light', $_bg,_border_color($c));

    // FIXME figure out the correct colors here
    color: if($c != $bg_color, gtkmix($tc, $_bg, 35%), $backdrop_insensitive_color);
    background-color: $button_disabled;
    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', gtkshade(gtkmix($c, $base_color, 85%), 0.92), gtkshade(gtkmix($c, $base_color, 85%), 0.94));
    $_bc: if($c != $bg_color, _border_color($c), $insensitive_borders_color);

    color: if($c != $bg_color, gtkmix($tc, $_bg, 60%), $insensitive_fg_color);
    background-color: $button_disabled_active;
    //@include _shadows(inset 0 1px transparentize(white, 1), $_blank_edge);
  }

  @else if $t==osd {
  //
  // normal osd button
  //
    $_bg: if($c != $bg_color, gtkalpha($c, 0.5), $osd_bg_color);

    color: $osd_fg_color;
    border-color: transparent; //$osd_borders_color;
    background-color: transparentize(black, .35);
    background-clip: padding-box;
    outline-color: transparentize($osd_fg_color, 0.7);
  }

  @else if $t==osd-hover {
  //
  // active osd button
  //
    $_bg: if($c != $bg_color, gtkalpha($c, 0.7), lighten($osd_bg_color, 12%));

    color: white;
    border-color: transparent; //$osd_borders_color;
    background-color: gtkalpha(gtkmix(black, currentColor, 85%), .65);
    background-clip: padding-box;
    outline-color: transparentize($osd_fg_color, 0.7);
  }

  @else if $t==osd-active {
  //
  // active osd button
  //
    $_bg: if($c != $bg_color, $c, $osd_borders_color);

    color: white;
    border-color: transparent; //$osd_borders_color;
    background-color: gtkalpha(gtkmix(black, currentColor, 75%), .65);
    background-clip: padding-box;

    outline-color: transparentize($osd_fg_color, 0.7);
  }

  @else if $t==osd-insensitive {
  //
  // insensitive osd button
  //
    color: $osd_insensitive_fg_color;
    border-color: transparent; //$osd_borders_color;
    background-color: transparent;
    background-image: image($osd_insensitive_bg_color);
    background-clip: padding-box;
    box-shadow: none;
    text-shadow: none;
    -gtk-icon-shadow: none;
  }

  @else if $t==osd-backdrop {
  //
  // backdrop osd button
  //
    $_bg: if($c != $bg_color, gtkalpha($c, 0.5), $osd_bg_color);

    color: $osd_fg_color;
    border-color: transparent; //$osd_borders_color;
    background-color: transparent;
    background-image: image($_bg);
    background-clip: padding-box;
    box-shadow: none;
    text-shadow: none;
    -gtk-icon-shadow: none;
  }

  @else if $t==undecorated {
  //
  // reset
  //
    border-color: transparent;
    background-color: transparent;
    background-image: none;

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

    text-shadow: none;
    -gtk-icon-shadow: none;
  }
}

@mixin headerbar_fill($c:$headerbar_bg_color, $hc:$top_hilight, $ov: none) {
//
// headerbar fill
//
// $c:  base color
// $hc: top highlight color
// $ov: a background layer for background shorthand (hence no commas!)
//
  $gradient: linear-gradient(to top, $c, $c);

  //@if $variant == 'dark' { $gradient: linear-gradient(to top, lighten($c, 4%), lighten($c, 6%)); }

  @if $ov != none { background: $c $ov, $gradient; }
  @else { background: $c $gradient; }

  //box-shadow: inset 0 1px $hc; // top highlight
}

@mixin overshoot($p, $t:normal, $c:$fg_color) {
//
// overshoot
//
// $p: position
// $t: type
// $c: base color
//
// possible $p values:
// top, bottom, right, left
//
// possible $t values:
// normal, backdrop
//

  $_small_gradient_length: 5%;
  $_big_gradient_length: 100%;

  $_position: center top;
  $_small_gradient_size: 100% $_small_gradient_length;
  $_big_gradient_size: 100% $_big_gradient_length;

  @if $p==bottom {
    $_position: center bottom;
    $_linear_gradient_direction: to top;
  }

  @else if $p==right {
    $_position: right center;
    $_small_gradient_size: $_small_gradient_length 100%;
    $_big_gradient_size: $_big_gradient_length 100%;
  }

  @else if $p==left {
    $_position: left center;
    $_small_gradient_size: $_small_gradient_length 100%;
    $_big_gradient_size: $_big_gradient_length 100%;
  }

  $_small_gradient_color: $c;
  $_big_gradient_color: $c;

  @if $c==$fg_color {
    $_small_gradient_color: gtkshade($borders_color, 0.9);
    $_big_gradient_color: $fg_color;

    @if $t==backdrop { $_small_gradient_color: $backdrop_borders_color; }
  }

  $_small_gradient: -gtk-gradient(radial,
                                  $_position, 0,
                                  $_position, 0.5,
                                  to($_small_gradient_color),
                                  to(gtkalpha($_small_gradient_color, 0)));

  $_big_gradient: -gtk-gradient(radial,
                                $_position, 0,
                                $_position, 0.6,
                                from(gtkalpha($_big_gradient_color, 0.07)),
                                to(gtkalpha($_big_gradient_color, 0)));

  @if $t==normal {
    background-image: $_small_gradient, $_big_gradient;
    background-size: $_small_gradient_size, $_big_gradient_size;
  }

  @else if $t==backdrop {
    background-image: $_small_gradient;
    background-size: $_small_gradient_size;
  }

  background-repeat: no-repeat;
  background-position: $_position;

  background-color: transparent; // reset some properties to be sure to not inherit them somehow
  border: none;                  //
  box-shadow: none;              //
}

// ***************************
// * Check and Radio buttons *
// ***************************

 @mixin check($t, $c:$bg_color, $tc:$fg_color, $checked: false) {
  // Check/Radio drawing function
  //
  // $t:        check/radio type,
  // $c:        base button color for colored* types
  // $tc:       optional text color for colored* types
  // $checked:  bool to chose between checked/unchecked
  //
  // possible $t values:
  // normal, hover, active, insensitive, backdrop, backdrop-insensitive

  $_border_color: if($c==$checkradio_bg_color, $c, $trough_color);

  @if $t==normal  {
    background-clip: if($checked, border-box, padding-box);
    background-image: image($c);
    border-color: $_border_color;
    box-shadow: none;
    color: $tc;
  }

  @if $t==hover {
    &:not(:checked):not(:indeterminate) { border-color: $trough_hover_color; }
  }

  @if $t==active {
    box-shadow: none;
  }

  @if $t==insensitive {
    // FIXME: Background color looks bad when using gtkalpha
    box-shadow: none;
    background-image: image($c);
    color: gtkalpha($tc, $disabled_opacity);
    border-color: $_border_color;
  }
}
