@import '../variables';
@import './shadows';
@import './state';

@mixin FormNote {
  font-size: $font-size-xs;
  display: block;
  color: $gray-600;
  margin: 0.375rem 0 0.25rem;
  line-height: 1.4;
}

@mixin FormActions {
  margin-top: $form-actions-spacer;
  justify-content: flex-end;
  display: flex;
}

@mixin FormAlertCaret($color) {
  position: absolute;
  overflow: hidden;
  height: 0.5rem;
  width: 1rem;

  bottom: auto;
  right: auto;
  left: 0.5rem;
  top: -0.5rem;

  &::before {
    background: mix($color, $white, 10%);
    content: '';

    pointer-events: none;
    height: 1rem;
    width: 1rem;

    border: $border mix($color, $white, 40%);
    transform: rotate(45deg);
    position: absolute;
    display: block;

    border-top-left-radius: 3px;
    bottom: auto;
    right: auto;
    left: auto;
    top: 0.25rem;
  }
}

@mixin FormAlert {
  max-width: $form-control-max-width;
  border-radius: $border-radius;
  padding: $form-alert-padding;
  font-size: $font-size-xs;
  font-weight: $font-weight-medium;
  line-height: 1.35;
  display: inline-block;
  margin: $spacer-1 0 0;
  position: relative;
  min-width: 3rem;
  z-index: 10;

  background-color: mix($gray-400, $white, 10%);
  border: $border mix($gray-400, $white, 40%);

  &Caret {
    @include FormAlertCaret($gray-400);
  }

  @each $state, $color in $states {
    @include state($state) {
      background-color: $color;

      &Caret {
        @include FormAlertCaret($color);
      }
    }
  }
}

@mixin FormCheckbox {
  padding-left: 3rem;
  position: relative;
  display: block;

  &Switch {
    -webkit-appearance: none;
    appearance: none;
    background: $gray-300;
    border-radius: 20px;
    position: absolute;
    cursor: pointer;
    height: 20px;
    width: 36px;
    outline: 0;
    margin: 0;
    left: 0;
    top: 0.25rem;

    &::after {
      position: absolute;
      width: calc((100% - 4px) / 2);
      height: calc(100% - 4px);
      border-radius: 50%;
      background: $white;
      display: block;
      content: '';
      left: 2px;
      top: 2px;

      transition: 250ms;
    }

    &:checked {
      background: #5b5;
    }

    &:checked::after {
      transform: translateX(100%);
    }
  }

  &Label {
    font-weight: bold;
    font-size: 13px;
  }

  &Note {
    @include FormNote;
  }
}

@mixin FormControl {
  background-color: $form-control-background-color;
  border-radius: $form-control-border-radius;
  position: relative;
  line-height: 1;
  outline: 0;
  margin: 0;

  font-size: $form-control-font-size;
  padding: $form-control-padding-base;
  border: 1px solid;
  border-color: $form-control-border-color;

  appearance: none;
  display: block;
  max-width: 100%;
  width: $form-control-max-width;

  box-shadow: map_get($shadows, control);

  @include focus {
    background-color: $white;
    border-color: $blue;
    @include focus-shadow;
  }
}

@mixin FormGroup {
  position: relative;
  line-height: 1;
  margin: $form-group-spacer 0;
}

@mixin FormLabel {
  font-weight: $font-weight-semibold;
  margin-bottom: 0.5rem;
  white-space: nowrap;
  align-items: center;
  font-size: 0.875rem;
  display: flex;
}

@mixin FormPasswordStrengthIndicator {
  position: absolute;
  bottom: 0.25rem;
  width: 0.675rem;
  right: 0.5rem;
  top: 0.25rem;

  justify-content: space-around;
  flex-flow: column;
  display: flex;

  .Indicator {
    background-color: $gray-200;
    display: block;
    height: 3px;
    width: 100%;
    border-radius: $form-control-border-radius;
    transition: 150ms;

    @each $state, $color in $states {
      @include state($state) {
        background-color: $color;
      }
    }
  }
}

@mixin FormRadio {
  padding-left: 20px;

  input[type='radio'] {
    vertical-align: middle;
    margin: 2px 0 0 -20px;
    float: left;
  }

  label {
    font-weight: bold;
    font-size: 13px;
  }

  .note {
    @include FormNote;
  }
}

@mixin FormSelect {
  height: 2.25rem;
  background: $white url($form-select-arrow) no-repeat right 8px center;
  background-size: 10px 12px;
  -webkit-appearance: none;
  appearance: none;
}

@mixin FormTextarea {
  @include FormControl;
}
