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

// Button Group
/// @type Color
$btngroup-background: $primary-color !default;
/// @type Color
$btngroup-color: #fff !default;
/// @type Number
$btngroup-radius: $button-radius !default;

$child-selectors: '> a, > label, > button';

%button-group {
  margin: 0;
  margin-bottom: 1rem;
  list-style-type: none;
  display: inline-flex;
  border-radius: $btngroup-radius;
  overflow: hidden;
  font-size: $button-font-size;

  > li {
    flex: 0 0 auto;

    // Links become buttons
    #{$child-selectors} {
      @extend %button;
      border-radius: 0;
      font-size: inherit;
      display: block;
      margin: 0;
    }
    > input + label {
      margin-left: 0;
    }
    // Add borders between items
    &:not(:last-child) {
      #{$child-selectors} {
        border-right: 1px solid scale-color($btngroup-background, $lightness: -25%);
      }
    }
  }
}

%button-group-segmented {
  border: 1px solid $primary-color;
  transition-property: background color;

  > li {
    // Hide the radio button
    > input[type="radio"] {
      position: absolute;
      left: -9999px;
    }
    // This is the button
    #{$child-selectors} {
      margin-right: 0;
      background: transparent;
    }
  }
}

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

  @if $expand {
    @include button-group-expand;
  }
}
@mixin button-group-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-group-expand($stretch: true) {
  display: if($stretch, flex, inline-flex);

  > li {
    flex: if($stretch, 1, 0 0 auto);

    #{$child-selectors} {
      @if $stretch { @include button-expand; }
    }
  }
}
@mixin button-group-style(
  $segmented: false,
  $background: $primary-color,
  $color: auto
) {

  @if not($segmented) {
    > li {
      #{$child-selectors} {
        @include button-style($background, auto, $color);
        border-color: scale-color($background, $lightness: -15%);
      }
      &.is-active {
        #{$child-selectors} {
          background: scale-color($background, $lightness: -15%);
        }
      }
    }
  }
  @else {
    @extend %button-group-segmented;
    $hover-color: rgba($background, 0.25);
    border-color: $background;

    > li {
      // This is the button
      #{$child-selectors} {
        border-color: $background;
        color: $background;

        // This is the button being hovered on
        &:hover {
          background: $hover-color;
          color: $background;
        }

        @if using(icon) {
          @each $iconclass in $icon-classes {
            .#{$iconclass} {
              @include color-icon($background);
            }
          }
        }
        @if using(loader) {
          @each $loaderclass in $loader-classes {
            .#{$loaderclass} {
              @include color-loaders($background);
            }
          }
        }
      }

      // This is the button when it's active
      &.is-active > a,
      > input:checked + label {
        &, &:hover {
          background: $background;
          color: isitlight($background);
        }

        @if using(icon) {
          @each $iconclass in $icon-classes {
            .#{$iconclass} {
              @include color-icon(isitlight($background));
            }
          }
        }
        @if using(loader) {
          @each $loaderclass in $loader-classes {
            .#{$loaderclass} {
              @include color-loaders(isitlight($background));
            }
          }
        }
      }
    }
  }
}

@mixin button-group(
  $segmented: false,
  $expand: false,
  $background: $primary-color,
  $color: #fff
) {
  @extend %button-group;
  @include button-group-expand($expand);
  @include button-group-style($segmented, $background, $color);
  border-radius: $btngroup-radius;
}

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

    // Colors
    &.secondary { @include button-group-style(false, $secondary-color); }
    &.success   { @include button-group-style(false, $success-color); }
    &.warning   { @include button-group-style(false, $warning-color); }
    &.alert     { @include button-group-style(false, $alert-color); }

    // Individual colors
    > li {
      &.secondary { #{$child-selectors} { @include button-style($secondary-color, auto, $btngroup: true); } }
      &.success   { #{$child-selectors} { @include button-style($success-color, auto, $btngroup: true); } }
      &.warning   { #{$child-selectors} { @include button-style($warning-color, auto, $btngroup: true); } }
      &.alert     { #{$child-selectors} { @include button-style($alert-color, auto, $btngroup: true); } }
    }

    // Segmented
    &.segmented { @include button-group-style(true);
      &.secondary { @include button-group-style(true, $secondary-color); }
      &.success   { @include button-group-style(true, $success-color); }
      &.warning   { @include button-group-style(true, $warning-color); }
      &.alert     { @include button-group-style(true, $alert-color); }
    }

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

    // Disabled
    li.disabled {
      #{$child-selectors} {
        @include button-disabled;
      }
    }

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

    @if using(icon) {
      > li > a {
        @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);
            }
          }
        }

        &.icon-left { @include button-group-icons(left); }
        &.icon-right  { @include button-group-icons(right); }
      }
    }

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

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

        &.icon-left { @include button-group-icons(left); }
        &.icon-right  { @include button-group-icons(right); }
      }
    }
  }
}
