// Drawing mixins

// Function to convert px values to em
@function to_em($input, $base: 16px) {
  // multiplied and divided by 1000 to make up for round() shortcoming
  $em_value: ($input / $base) * 1.091 * 1000;
  @return round($em_value) / 1000 * 1em;
}

// generic drawing of more complex things
@function draw_widget_edge($c:$borders_edge) {
// outer highlight "used" on most widgets
  @return 0 1px $c;
}

// provide font size in rem, with px fallback
@mixin fontsize($size: 24, $base: 16) {
  font-size: round($size) + pt;
  //font-size: ($size / $base) * 1rem;
}

@mixin draw_shadows($shadow1, $shadow2:none, $shadow3:none, $shadow4:none) {
//
// Helper function to stack up to 4 box-shadows;
//
  @if $shadow4!=none { box-shadow: $shadow1, $shadow2, $shadow3, $shadow4; }
  @else if $shadow3!=none { box-shadow: $shadow1, $shadow2, $shadow3; }
  @else if $shadow2!=none { box-shadow: $shadow1, $shadow2; }
  @else { box-shadow: $shadow1; }
}

// entries

@mixin entry($t, $fc:$selected_bg_color, $edge: $borders_edge) {
//
// 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
//

  @if $t==normal {
    background-color: $base_color;
    border: 1px solid $borders_color !important;
    box-shadow: none !important;
  }

  @if $t==focus {
    background-color: $base_color;
    border: 1px solid $selected_borders_color !important;
    box-shadow: none !important;
  }

  @if $t==hover {
    background-color: $base_color;
    border: 1px solid $selected_borders_color !important;
    box-shadow: none !important;
  }

  @if $t==insensitive {
    color: $insensitive_fg_color;
    border: 1px solid $insensitive_bg_color !important;
    box-shadow: none !important;
  }
}

// buttons

@function draw_border_color ($c) {
  //
  // colored buttons want the border form the base color
  //
  @return if($variant == 'light', darken($c, 18%), darken($c, 6%));
}

@function draw_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 rgba(255,255,255,$lbg/($lbg*1.3)); }
  @else { @return rgba(0,0,0,1-$lbg*0.8); }
}

@function draw_button_hilight_color($c) {
//
// calculate the right top highlight color for buttons
//
// $c: base color;
//
  @if lightness($c)>90% { @return white; }
  @else if lightness($c)>80% { @return rgba(255,255,255, 0.7); }
  @else if lightness($c)>50% { @return rgba(255,255,255, 0.5); }
  @else if lightness($c)>40% { @return rgba(255,255,255, 0.3); }
  @else { @return rgba(255,255,255, 0.1); }
}

@mixin draw_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: draw_text_shadow_color($tc, $bg);

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

$button_bg_color: if($variant == 'light', $base_color, lighten($base_color, 3%));

@mixin button($t, $c:$button_bg_color, $tc:$fg_color, $edge: $borders_edge, $shadow: $shadow_color) {
//
// 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
// $shadow: set to none to not draw the drop shadow or specify a color to not
//          use the default one
//
// 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: draw_button_hilight_color($c);
  $button_edge: if($edge == none, none, draw_widget_edge($edge));
  $blank_edge: if($edge == none, none, draw_widget_edge(transparentize($edge,1)));
  $button_shadow: if($shadow == none, none, 0 1px 1px 0 $shadow);

  // normal button
  @if $t==normal {
    color: $tc;
    background-color: $c;
    border-color: $borders_color;
    // @include draw_shadows($button_shadow);
    box-shadow: none;
    text-shadow: none;
    icon-shadow: none;
  }

  // focused button
  @if $t==focus {
    color: $tc;
    text-shadow: none;
    icon-shadow: none;
    box-shadow: inset 0 0 0 2px transparentize($selected_bg_color, 0.65) !important;
    //border-color: $selected_bg_color;
  }

  // hover button
  @else if $t==hover {
    color: $tc;
    background-color: mix($tc, $c, if($variant == 'light', 8%, 5%));
    border-color: $borders_color;
    box-shadow: none;
    text-shadow: none;
    icon-shadow: none;
  }

  // active button
  @else if $t==active {
    color: $tc;
    background-color: mix($tc, $c, if($variant == 'light', 12%, 10%));
    border-color: $borders_color;
    text-shadow: none;
    icon-shadow: none;
    box-shadow: none;
  }

  // checked button
  @else if $t==checked {
    color: $tc;
    background-color: mix($tc, $c, if($variant == 'light', 8%, 5%));
    border-color: $borders_color;
    text-shadow: none;
    icon-shadow: none;
    box-shadow: none;
  }

  // insensitive button
  @else if $t==insensitive {
    color: $insensitive_fg_color;
    border-color: $insensitive_borders_color;
    background-color: $insensitive_bg_color;
    box-shadow: none;
    text-shadow: none;
    icon-shadow: none;
  }

  // normal button
  @if $t==flat {
    color: $tc;
    background-color: transparent;
    border-color: transparent;
    // box-shadow: 0 1px 1px 0 rgba(0,0,0,0.1);
    text-shadow: none;
    icon-shadow: none;
  }

  // focused button
  @if $t==flat-focus {
    color: $tc;
    text-shadow: none;
    icon-shadow: none;
    box-shadow: inset 0 0 0 2px transparentize($selected_bg_color, 0.4);
    //border-color: $selected_bg_color;
  }

  // hover button
  @else if $t==flat-hover {
    color: $tc;
    background-color: rgba($tc, 0.08);
    border-color: transparent;
    text-shadow: none;
    icon-shadow: none;
  }

  // active button
  @else if $t==flat-active {
    color: $tc;
    background-color: rgba($tc, 0.15);
    border-color: transparent;
    text-shadow: none;
    icon-shadow: none;
    box-shadow: none;
  }
  
  // active button
  @else if $t==flat-checked {
    color: $tc;
    background-color: rgba($tc, 0.12);
    border-color: transparent;
    text-shadow: none;
    icon-shadow: none;
    box-shadow: none;
  }

  // insensitive button
  @else if $t==flat-insensitive {
    color: $insensitive_fg_color;
    border-color: transparent;
    background-color: rgba($tc, 0.01);
    box-shadow: none;
    text-shadow: none;
    icon-shadow: none;
  }

  // reset
  @else if $t==undecorated {
    border-color: transparent;
    background-color: transparent;
    background-image: none;
    @include draw_shadows(inset 0 1px rgba(255,255,255,0),$blank_edge);
    text-shadow: none;
    icon-shadow: none;
  }
}

// overview icons
@mixin overview-icon($color) {
  .overview-icon {
    @extend %icon_tile;
    color: $color;
    // background-color: transparentize($color, .9);
  }

  &:hover,
  &:selected {
    .overview-icon {
      background-color: transparentize($color, .8);
    }
  }

  &:focus {
    .overview-icon {
      background-color: transparentize($color, .7);
      // border-color: $selected_bg_color;
    }
  }

  &:drop {
    .overview-icon {
      border: 2px solid $selected_bg_color; //already 2px transparent so no jumping
      background-color: transparentize($selected_bg_color, .8);
    }
  }

  &:active,
  &:checked {
    .overview-icon {
      background-color: transparentize($color, .75);
    }
  }
}

// styling for elements within popovers that look like notifications
@mixin card($flat: false) {
  border-radius: $base_border_radius;
  margin: $base_margin;

  @if $flat {
    @include button(undecorated);
    color: $fg_color;
    box-shadow: none !important;
  } @else {
    @include button(normal);
    box-shadow: 0 1px 1px 0 rgba(black, 0.05);
  }

  &:hover { @include button(hover); }
  &:active { @include button(active); }
  &:focus { @include button(focus); }
}

// Helper mixin for button-like elements with an icon
@mixin tile_button($fg:$system_fg_color, $bg:$system_bg_color, $raised: false, $system: true) {
  //
  // $fg: foreground color
  // $bg: background color
  //
  // $raised: uses raised style, true or false
  // $system: uses system styles, true or false
  //

  @extend %tile;

  @if $raised {
    @include button(normal, $tc:$fg, $c: mix($fg, $bg, if($variant == 'light', 4%, 3%)));
    &:focus { @include button(focus, $tc:$fg, $c:$bg);}
    &:hover { @include button(hover, $tc:$fg, $c:$bg);}
    &:active { @include button(active, $tc:$fg, $c:$bg);}
    &:highlighted,&:selected,
    &:checked { @include button(checked, $tc:$fg, $c:$bg);}
    &:insensitive { @include button(insensitive, $tc:$fg, $c:$bg);}
  } @else {
    @include button(flat, $tc:$fg, $c:$bg);
    &:focus { @include button(flat-focus, $tc:$fg, $c:$bg);}
    &:hover { @include button(flat-hover, $tc:$fg, $c:$bg);}
    &:active { @include button(flat-active, $tc:$fg, $c:$bg);}
    &:highlighted,&:selected,
    &:checked { @include button(flat-checked, $tc:$fg, $c:$bg);}
    &:insensitive { @include button(flat-insensitive, $tc:$fg, $c:$bg);}
  }

  &:drop {
    background-color: transparentize($selected_bg_color, .8);
    box-shadow: inset 0 0 0 2px transparentize($selected_bg_color, .2);
  }
}
