////
/// @group button
////

// Button
/// @type Number | List
$button-padding: 0.85em 1em !default;
/// @type Number | List
$button-margin: 0 $global-padding $global-padding 0 !default;
/// @type Keyword
$button-style: solid !default;
/// @type Color
$button-background: $primary-color !default;
/// @type Color
$button-background-hover: scale-color($button-background, $lightness: -15%) !default;
/// @type Color
$button-color: auto !default;
/// @type Number
$button-radius: 0 !default;
/// @type Map
$button-sizes: (
  tiny: 0.7,
  small: 0.8,
  medium: 1,
  large: 1.3,
) !default;
/// @type Number
$button-font-size: 0.9rem !default;
/// @type Number
$button-opacity-disabled: 0.5 !default;
/// @type Boolean
$button-tag-selector: false !default;
/// @type Number
$button-icon-spacing: 0.5em !default;
///

%button {
  display: inline-block;
  border: 0;
  text-align: center;
  line-height: 1;
  cursor: pointer;
  -webkit-appearance: none;
  -webkit-font-smoothing: antialiased;
  transition: background 0.25s ease-out;
  vertical-align: middle;

  padding: $button-padding;
  margin: $button-margin;
  font-size: $button-font-size;
  border-radius: $button-radius;

  // Dropdown arrow
  // TODO: Change to class and mixin because now the toggle is 'fa-open' which is too generic
  // &[data-popup-toggle] {
  //   position: relative;
  //   padding-right: 2em; // Placeholder

  //   &::after {
  //     @include css-triangle(6px, black, top);
  //     position: absolute;
  //     right: 0.7em;
  //     top: 50%;
  //     margin-top: -3px;
  //   }
  // }
}

@mixin button-size($size: medium, $expand: false) {
  $size: $button-font-size * map-get($button-sizes, $size);
  font-size: $size;

  @if $expand {
    @include button-expand;
  }

  @if using(icon) {
    @each $iconclass in $icon-classes {
      .#{$iconclass} {
        vertical-align: middle;
        margin-top: -2px; // The icons are oddly misaligned

        @include size-icon(1em, 1em);
      }
    }
  }

  @if using(loader) {
    @each $loaderclass in $loader-classes {
      .#{$loaderclass} {
        vertical-align: middle;

        @include size-loaders(1em, 1em);
      }
    }
  }

  // Icon positioning
  &, &.icon-left { @include button-icons(left); }
  &.icon-right  { @include button-icons(right); }
}

@mixin button-icons(
  $position: left
) {
  @if $position == left {
    > img {
      margin: 0 $button-icon-spacing 0 0;
      float: left;
    }

    @if using(icon) {
      @each $iconclass in $icon-classes {
        > .#{$iconclass} {
          margin: 0 $button-icon-spacing 0 0;
          float: left;
        }
      }
    }
    @if using(loader) {
      @each $loaderclass in $loader-classes {
        > .#{$loaderclass} {
          margin: 0 $button-icon-spacing 0 0;
          float: left;
        }
      }
    }
  }
  @if $position == right {
    > img {
      margin: 0 0 0 $button-icon-spacing;
      float: right;
    }

    @if using(icon) {
      @each $iconclass in $icon-classes {
        > .#{$iconclass} {
           margin: 0 0 0 $button-icon-spacing;
           float: right;
        }
      }
    }
    @if using(loader) {
      @each $loaderclass in $loader-classes {
        > .#{$loaderclass} {
          margin: 0 0 0 $button-icon-spacing;
          float: right;
        }
      }
    }
  }
}

@mixin button-expand($expand: true) {
  @if $expand {
    display: block;
    width: 100%;
    margin-left: 0;
    margin-right: 0;
  }
  @else {
    display: inline-block;
    width: auto;
    margin: $button-margin;
  }
}

@mixin button-style(
  $background: $button-background,
  $background-hover: $button-background-hover,
  $color: $button-color,
  $style: $button-style,
  $radius: $button-radius,
  $btngroup: false
){
  @if $style == hollow {
    border: 1px solid $background;
    padding-top: calc(#{get-side($button-padding, top)} - 1px);
    padding-right: calc(#{get-side($button-padding, right)} - 1px);
    padding-bottom: calc(#{get-side($button-padding, bottom)} - 1px);
    padding-left: calc(#{get-side($button-padding, left)} - 1px);
    background: transparent;
    color: $background;

    &:hover, &:focus {
      border-color: scale-color($background, $lightness: 25%);
      background: transparent;
      color: scale-color($background, $lightness: 25%);
    }
  }
  // Solid is the default
  @else {
    @if $color == auto {
      $color: isitlight($background);
    }

    background: $background;
    color: $color;

    &:hover, &:focus {
      @if $background-hover == auto {
        background: scale-color($background, $lightness: -15%);
      }
      @else {
        background: $background-hover;
      }
      color: $color;
    }
  }

  @if $btngroup {
    border-color: $background;
    &:hover, &:focus {
      border-color: scale-color($background, $lightness: -25%);
    }
  }

  @if using(icon) {
    @if $style == hollow {
      @each $iconclass in $icon-classes {
        .#{$iconclass} {
          @include color-icon($background);
        }
      }
      &:hover {
        @each $iconclass in $icon-classes {
          .#{$iconclass} {
            @include color-icon(scale-color($background, $lightness: 25%));
          }
        }
      }
    }
    @else {
      @each $iconclass in $icon-classes {
        .#{$iconclass} {
          @include color-icon($color);
        }
      }
    }
  }

  @if using(loader) {
    @if $style == hollow {
      @each $loaderclass in $loader-classes {
        .#{$loaderclass} {
          @include color-loaders($background);
        }
      }
      &:hover {
        @each $loaderclass in $loader-classes {
          .#{$loaderclass} {
            @include color-loaders(scale-color($background, $lightness: 25%));
          }
        }
      }
    }
    @else {
      @each $loaderclass in $loader-classes {
        .#{$loaderclass} {
          @include color-loaders($color);
        }
      }
    }
  }
}

@mixin button-disabled() {
  opacity: $button-opacity-disabled;
  cursor: default;
  pointer-events: none;
}

@mixin button(
  $size: medium,
  $expand: false,
  $background: $button-background,
  $background-hover: $button-background-hover,
  $color: $button-color,
  $style: $button-style,
  $radius: $button-radius
) {
  @extend %button;
  @include button-size($size);
  @include button-expand($expand);
  @include button-style($background, $background-hover, $color, $style);
}

@include exports(button) {
  .button {
    @include button;

    &.tiny   { @include button-size(tiny); }
    &.small  { @include button-size(small); }
    &.large  { @include button-size(large); }
    &.expand { @include button-expand; }

    &.secondary { @include button-style($secondary-color, auto) }
    &.success   { @include button-style($success-color, auto) }
    &.warning   { @include button-style($warning-color, auto) }
    &.alert     { @include button-style($alert-color, auto) }
    &.info      { @include button-style($info-color, auto) }
    &.dark      { @include button-style($dark-color, auto) }

    @if $button-style != hollow {
      &.hollow { @include button-style($style: hollow);
        &.secondary { @include button-style($secondary-color, $style: hollow); }
        &.success   { @include button-style($success-color, $style: hollow); }
        &.warning   { @include button-style($warning-color, $style: hollow); }
        &.alert     { @include button-style($alert-color, $style: hollow); }
        &.info      { @include button-style($info-color, $style: hollow); }
        &.dark      { @include button-style($dark-color, $style: hollow); }
      }
    }

    &.disabled, &:disabled { @include button-disabled; }
  }

  @if $button-tag-selector {
    button {
      @extend .button;
    }
  }
}
