// Drawing mixins

// generic drawing of more complex things

// entries
@function entry_focus_border($fc:$selected_bg_color) {
  @if $variant == 'light' { @return $fc; }
  @else { @return if($fc==$selected_bg_color, $selected_borders_color, darken($fc, 35%)); }
}

@mixin entry($t, $fc:$selected_bg_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, osd, osd-focus;
//

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

  @if $t==normal {
    color: $text_color;
    border-color: $borders_color;
    background-color: $base_color;
    background-image: linear-gradient(to bottom, darken($base_color, 8%), $base_color);
    @if $variant == 'light' {
      box-shadow: inset 0 1px transparentize(black, 0.9),
                  inset 1px 0 transparentize(black, 0.96),
                  inset -1px 0 transparentize(black, 0.96),
                  inset 0 -1px transparentize(black, 0.98),
                  0 1px transparentize(white, 0.6);
    }
    @else {
      box-shadow: inset 0 1px transparentize(black, 0.9),
                  inset 1px 0 transparentize(black, 0.96),
                  inset -1px 0 transparentize(black, 0.96),
                  inset 0 -1px transparentize(black, 0.98),
                  0 1px transparentize(white, 0.9);
    }
    // for the transition to work the number of shadows in different states needs to match, hence the transparent shadow here.
  }
  @if $t==focus {
    border-color: $selected_bg_color;
  }
  @if $t==insensitive {
    color: $insensitive_fg_color;
    border-color: $insensitive_borders_color;
    background-color: $bg_color;
    background-image: none;
  }
  @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;
    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;
  }
}

// buttons

@function _border_color($c, $darker: false) {
  @if $darker == true { @return darken($c, 20%); }
  @else { @return darken($c, 15%); }
}

@function _text_shadow_color ($tc: $fg_color, $bg: $bg_color) {
//
// calculate the color of text shadows
//
// $tc is the text color
// $bg is the background color
//
  $_lbg: lightness($bg)/100%;
  @if lightness($tc)<50% { @return transparentize(white, 1-$_lbg/($_lbg*1.3)); }
  @else { @return transparentize(black, $_lbg*0.8); }
}

@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 functions 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, $backimage: null) {
//
// Button drawing function
//
// $t:    button type,
// $c:    base button color for colored* types
// $tc:   optional text color for colored* types
// $backimage: additional background-image behind the default one
//             (for the button.circular hack)
//
// possible $t values:
// normal, hover, active, insensitive, insensitive-active,
// osd, osd-hover, osd-active, osd-insensitive, undecorated
//
  @if $t==normal {
  //
  // normal button
  //
    color: $tc;
    border-color: _border_color($c);
    border-bottom-color: if($c != $bg_color, _border_color($c, true), $borders_color);
    background-image: if($variant == 'light', linear-gradient(to bottom, lighten($c, 16%), darken($c, 2%)),
                                              linear-gradient(to bottom, lighten($c, 10%), darken($c, 2%))),
                      $backimage;
    @if $variant == 'light' {
      box-shadow: inset 0 1px transparentize(white, 0),
                  inset 1px 0 transparentize(white, 0.6),
                  inset -1px 0 transparentize(white, 0.6),
                  inset 0 -1px transparentize(white, 0.8),
                  1px 0 transparentize(black, 0.95),
                  -1px 0 transparentize(black, 0.95),
                  0 1px transparentize(black, 0.9);
    }
    @else {
      box-shadow: inset 0 1px transparentize(white, 0.9),
                  inset 1px 0 transparentize(white, 0.95),
                  inset -1px 0 transparentize(white, 0.95),
                  inset 0 -1px transparentize(white, 0.98),
                  1px 0 transparentize(black, 0.95),
                  -1px 0 transparentize(black, 0.95),
                  0 1px transparentize(black, 0.9);
    }                  
    @include _button_text_shadow($tc, $c);
  }

  @else if $t==hover {
  //
  // hovered button
  //
    color: $tc;
    border-color: _border_color($c);
    border-bottom-color: if($c != $bg_color, _border_color($c, true), $borders_color);
    @if $variant == 'light' {
      background-image: linear-gradient(to bottom, lighten($c, 20%), lighten($c, 2%)),
                        $backimage;
      @include _button_text_shadow($tc, lighten($c, 6%));
    }
    @else {
      background-image: linear-gradient(to bottom, lighten($c, 14%), lighten($c, 2%)),
                        $backimage;
      @include _button_text_shadow($tc,lighten($c, 6%));
    }
  }

  @else if $t==active {
  //
  // pushed button
  //
    color: $tc;
    border-color: if($c != $bg_color, _border_color($c), $borders_color);
    @if $variant == 'light' {
      background-image: linear-gradient(to bottom, darken($c, 22%), darken($c, 15%) 30%, darken($c, 15%));

    }
    @else {
      background-image: linear-gradient(to bottom, darken($c, 14%), darken($c, 7%) 30%, darken($c, 7%));
    }

    box-shadow: inset 0 1px transparentize(black, 0.9),
                inset 1px 0 transparentize(black, 0.92),
                inset -1px 0 transparentize(black, 0.92),
                inset 0 -1px transparentize(black, 0.94);
    text-shadow: none;
    -gtk-icon-shadow: none;
  }

  @else if $t==insensitive {
  //
  // insensitive button
  //
    $_bg: if($c != $bg_color, mix($c, $base_color, 85%), linear-gradient(to bottom, $bg_color, darken($bg_color, 5%)));

    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;

    @if variant == 'light' {
      box-shadow: inset 0 1px transparentize(white, 0.7),
                  inset 1px 0 transparentize(white, 0.85),
                  inset -1px 0 transparentize(white, 0.85),
                  inset 0 -1px transparentize(white, 0.94);
    }
    @else {
      box-shadow: inset 0 1px transparentize(white, 0.95),
                  inset 1px 0 transparentize(white, 0.98),
                  inset -1px 0 transparentize(white, 0.98);
    }
  }

  @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: linear-gradient(to bottom, darken($bg_color, 22%), darken($bg_color, 10%));

    box-shadow: inset 0 1px transparentize(black, 0.9),
                inset 1px 0 transparentize(black, 0.92),
                inset -1px 0 transparentize(black, 0.92),
                inset 0 -1px transparentize(black, 0.94);
  }

  @else if $t==normal-colored {
  //
  // normal colored button
  //
    color: $tc;
    border-color: _border_color($c);
    border-bottom-color: if($c != $bg_color, _border_color($c, true), $borders_color);
    background-image: linear-gradient(to bottom, lighten($c, 8%), darken($c, 4%));
    box-shadow: inset 0 1px lighten($c, 15%),
                inset 1px 0 lighten($c, 7%),
                inset -1px 0 lighten($c, 7%),
                1px 0 transparentize(black, 0.95),
                -1px 0 transparentize(black, 0.95),
                0 1px transparentize(black, 0.9);
    text-shadow: none;                  
  }

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

    color: $osd_fg_color;
    border-color: $osd_borders_color;
    background-color: transparent;
    background-image: image($_bg);
    background-clip: padding-box;
    box-shadow: inset 0 1px transparentize(white, 0.9);
    text-shadow: 0 1px black;
    -gtk-icon-shadow: 0 1px black;
  }

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

    color: white;
    border-color: $osd_borders_color;
    background-color: transparent;
    background-image: image($_bg);
    background-clip: padding-box;
    box-shadow: inset 0 1px transparentize(white, 0.9);
    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);

    color: white;
    border-color: $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==osd-insensitive {
  //
  // insensitive osd button
  //
    color: $osd_insensitive_fg_color;
    border-color: $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==undecorated {
  //
  // reset
  //
    border-color: transparent;
    background-color: transparent;
    background-image: none;
    box-shadow: none;
    text-shadow: none;
    -gtk-icon-shadow: none;
  }
}

@mixin headerbar_fill($c:$headerbar_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 bottom, lighten($c, 8%), darken($c, 12%));

  @if $variant == 'dark' { $gradient: linear-gradient(to bottom, lighten($c, 3%), darken($c, 3%)); }

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

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

@mixin overshoot($p) {
//
// overshoot
//
// $p: position
//
// possible $p values:
// top, bottom, right, left
//

  $_gradient_size: 100% 60%;
  $_position: center top;

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

  @else if $p==right {
    $_position: right center;
    $_gradient_size: 60% 100%;
  }

  @else if $p==left {
    $_position: left center;
    $_gradient_size: 60% 100%;
  }

  $_gradient: -gtk-gradient(radial,
                            $_position, 0,
                            $_position, 0.6,
                            from(transparentize($selected_bg_color, 0.8)),
                            to(transparentize($selected_bg_color, 1)));

  background-image: $_gradient;
  background-size: $_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

  @if $t==normal  {
    background-clip: if($checked, border-box, padding-box);
    background-image: linear-gradient(to bottom, darken($c, 8%) 15%, $c);
    border-color: if($variant == 'light', darken($borders_color, 10%), $borders_color);
    box-shadow: 0 1px if($variant == 'light', transparentize(white, 0.5), transparentize(white, 0.9));
    color: $tc;
  }

  @if $t==hover {
    background-image: linear-gradient(to bottom, darken($c, 12%) 15%, darken($c, 4%));
  }

  @if $t==active {
    box-shadow: inset 0 1px 1px 0px if($variant == 'light', rgba(0, 0, 0, 0.2), black);
  }

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

    @if $variant == 'light' {
      background-image: linear-gradient(to bottom, darken($insensitive_bg_color, 8%) 15%, $insensitive_bg_color);
      border-color: $insensitive_borders_color;
    }
    @else {
      background-image: linear-gradient(to bottom, $insensitive_bg_color 15%, lighten($insensitive_bg_color, 3%));
      border-color: lighten($insensitive_borders_color, 12%);
    }
    color: $insensitive_fg_color;
  }
}

