@import 'control-base';

$form-control-icon-size: rem-calc(16px);
$form-control-icon-position: rem-to-em(rem-calc(4px), $control-font-size) center;
$form-control-label-size: $ec-font-size-label;
$form-control-label-color: $ec-color-text-secondary-dark;
$form-control-height: $control-height;
$form-control-line-height: $control-line-height;
$form-control-spacing-y: rem-calc(16);

$checkbox-indicator-size: $form-control-icon-size;

@mixin form-control-input-base {
  &::selection {
    background-color: $ec-color-active;
    color: $ec-color-text-primary-light;
  }
  &::-webkit-input-placeholder { /* Chrome/Opera/Safari */
    color: $ec-color-text-hint-dark;
  }
  &::-moz-placeholder { /* Firefox 19+ */
    color: $ec-color-text-hint-dark;
    opacity: 1;
  }
  &:-ms-input-placeholder { /* IE 10+ */
    color: $ec-color-text-hint-dark;
  }
  &:-moz-placeholder { /* Firefox 18- */
    color: $ec-color-text-hint-dark;
    opacity: 1;
  }
}

@mixin form-control-input-group-invalid {
  .textbox-group-input ::ng-deep .control input.ng-invalid,
  .textbox-group-input ::ng-deep .control input.ng-valid {
    @include form-control-state(invalid, input);
  }

  &:not(.open) {
    .textbox-group-btn-right ::ng-deep button{
      @include control-invalid;
    }
  }
}

/// Calculate the background properties for an input icon
/// @param {string} $icon - The classname of the icon (matches the filename)
/// @param {number} $width [$form-control-icon-size] - The width of the icon, converted to ems
/// @param {number} $height [$form-control-icon-size] - The height of the icon, converted to ems
/// @param {string|number|list} $position [$form-control-icon-position] - The background-position of the icon
/// @param {number} $indent [$form-control-icon-size] - The amount to indent the text to allow room for the icon
@mixin form-control-input-icon(
  $icon,
  $width: $form-control-icon-size,
  $height: $form-control-icon-size,
  $position: $form-control-icon-position,
  $indent: $form-control-icon-size,
) {
  $size: rem-to-em($width, $control-font-size) rem-to-em($height, $control-font-size);
  @include icon-bg-image($icon, $position, $size);
  padding-left: rem-to-em(($indent + $control-padding-x), $control-font-size);
}

@mixin form-control-state($state, $selector) {
  $icon-position: $form-control-icon-position;

  @if $selector == textarea {
    $icon-position: rem-to-em(rem-calc(4px)) ($form-control-height - $form-control-icon-size) / 2;
  }

  /// Required
  @if $state == required {
    @include control-default;
    @include form-control-input-icon($required-icon, $position: $icon-position);

  /// Pending
  } @else if $state == pending {
    @include form-control-input-icon(pending-icon(), $position: $icon-position)

  /// Invalid
  } @else if $state == invalid {
    @include control-invalid;
    @include form-control-input-icon($invalid-icon, $position: $icon-position);

  /// Focus
  } @else if $state == focus {
    @include control-focus;
    position: relative;
    z-index: 1;

  /// Disabled
  } @else if $state == disabled {
    @include control-disabled('form', true);
  }
}

/// A form control label
@mixin form-control-label {
  color: $form-control-label-color;
  display: block;
  font-size: $form-control-label-size;
  line-height: 1;
  margin: ($form-control-label-size / 2) 0;
}

/// The default form control element in the control component
/// @param {string} $tag [input] - The HTML tag name of the input element (e.g. input, textarea)
@mixin form-control-input($selector: input) {
  @include control-default;
  @include form-control-input-base;
  background-image: none;
  background-clip: padding-box;
  border-width: $control-border-width;
  border-style: solid;
  border-radius: $control-border-radius;
  width: 100%;

  @if $selector == input {
    @include control-dimensions($height: $form-control-height);
  } @else {
    @include control-dimensions($height: auto);
  }

  transition: border-color ease .3s, box-shadow ease .3s;

  &:required {
    &.is-empty {
      @include form-control-state(required, $selector);
    }
  }

  // Use .ng-invalid because not all Angular validators trigger :invalid
  &.ng-invalid {
    &.ng-touched {
      @include form-control-state(invalid, $selector);
    }
  }

  &.is-pending {
    &.ng-valid,
    &.ng-invalid {
      @include form-control-state(pending, $selector);
    }
  }

  &:focus,
  &:focus.is-empty {
    @include form-control-state(focus, $selector);
  }

  &:disabled {
    @include form-control-state(disabled, $selector);
  }

  &.is-uppercase:not(.is-empty) {
    text-transform: uppercase;
  }
}

/// The base styles of a control component. These styles will be applied to :host
@mixin form-control-base() {
  color: $control-color;
  font-family: $control-font-family;
  font-size: $control-font-size;
  display: block;
  margin-bottom: $form-control-spacing-y;
  width: 100%;

  :host-context(.form-condensed) {
   margin-bottom: .5rem;
  }

  .control {
    width: 100%;
    display: flex;
    flex-direction: column;

    &.control-label-bottom {
      flex-direction: column-reverse;
    }

    &.control-label-left {
      flex-direction: row;
      label {
        margin-right: .25rem;
      }
    }

    &.control-label-right {
      flex-direction: row-reverse;
      label {
        margin-left: .25rem;
      }
    }

    &.control-label-left,
    &.control-label-right {
      align-items: center;

      label {
        flex: 1 1;
        margin-top: 0;
        margin-bottom: 0;
      }
      .control-input {
        flex: 2 2;
      }
    }

    &.is-readonly {
      input {
        @include form-control-read-only;
      }
    }

    &.invalid {
      @include form-control-input-group-invalid;
    }
  }

  .textbox-group {
    display: flex;
    position: relative;
  }

  textarea:focus,
  input:focus {
    outline: none;
  }
}

@mixin checked-icon($type: 'radio') {
  @if $type == 'radio' {
    background-image: radiobutton-icon($control-bg-active);
    background-color: $control-text-active;
  } @else {
    background-image: checkbox-icon($control-text-active);
    background-color: $control-bg-active;
  }
}


@mixin checkbox-radio {
  cursor: pointer;
  display: inline-flex;
  margin-bottom: 0;
  position: relative;
}

@mixin checkbox-radio-readonly {
  .input {
    opacity: 0;
  }

  .indicator {
    background-color: $control-bg-read-only;
    border: 0;
  }

  .label,
  .indicator {
    opacity: 1;
    color: $control-color;
  }
}

$checkbox-top-margin: ($form-control-height - $form-control-icon-size) / 2;

@mixin checkbox-radio-input(
  $type: 'radio',
  $indicator-selector: '.indicator',
  $label-selector: '.label'
) {
  margin-top: $checkbox-top-margin;
  opacity: 0;
  position: absolute;
  z-index: -1;

  &:checked {
    ~ #{$indicator-selector} {
      @include checked-icon($type);
    }

    &:disabled {
      ~ #{$indicator-selector} {
        @if $type == 'radio' {
          background-image: radiobutton-icon($control-color-disabled);
        } @else {
          background-image: checkbox-icon($control-color-disabled);
        }
      }
    }
  }

  &.indeterminate {
    @if $type == 'checkbox' {
      ~ #{$indicator-selector} {
        background-color: $control-bg-active;
        position: relative;

        &:after {
          content: '';
          position: absolute;
          background-color: $control-text-active;
          width: 10px;
          height: 4px;
          top: 5px;
          left: 2px;
        }
      }

      &:disabled {
        ~ #{$indicator-selector} {
          background-color: $control-bg-disabled;
  
          &:after {
            background-color: $control-color-disabled;
          }
        }
      }
    }
  }

  &:focus {
    ~ #{$indicator-selector} {
      border-color: $control-border-color-focused;
      box-shadow: $control-shadow-focused;
    }
  }

  &:disabled {
    ~ #{$indicator-selector} {
      background-color: $control-bg-disabled;
      opacity: $control-opacity-disabled;
    }

    ~ #{$label-selector} {
      color: $control-color-disabled;
      opacity: $control-opacity-disabled;
    }
  }
}

@mixin checkbox-radio-indicator($type: 'radio') {
  margin-top: $checkbox-top-margin;
  background-repeat: no-repeat;
  background-position: center;
  background-color: $ec-color-bg-content;
  background-size: .75rem;
  border: $control-border-width solid $control-border-color;
  display: block;
  flex: 0 0 auto;
  width: $checkbox-indicator-size;
  height: $checkbox-indicator-size;
  pointer-events: none;

  @if $type == 'radio' {
    border-radius: 50%;
  } @else {
    border-radius: $control-border-radius;
  }
}

@mixin checkbox-radio-label() {
  @include control-dimensions($height: $form-control-height, $borders: false);
  padding-left: 0;
  padding-right: 0;
  margin-left: .25rem;
  min-height: $form-control-height;
  height: auto;
}

@mixin toggle() {
  font-size: $control-font-size;
  background-color: $ec-border-color;
  border-radius: $control-border-radius;
  border: $control-border-width solid $control-border-color;
  min-height: 2em;
  position: relative;
  color: $ec-color-text-hint-dark;
  display: flex;

  input {
    position: absolute;
    z-index: -1;
    opacity: 0;

    &:checked {
      & + label {
        // TODO: get this sorted out
        // color: $ec-color-action;
        color: $blue-4
      }

      &:last-of-type {
        & ~ a {
          transform: translateX(100%);
        }
      }
    }

    &:focus ~ .toggle-focused {
      display: block;
    }
  }

  .toggle-focused {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    box-shadow: 0 0 0 rem-calc(2px) $control-border-color-focused;
    display: none;
  }

  label {
    align-items: center;
    cursor: pointer;
    display: flex;
    flex: 1 1 0%;
    justify-content: center;
    line-height: 1.1em;
    margin-bottom: 0;
    padding: rem-calc(6px) rem-calc(8px);
    position: relative;
    text-align: center;
    transition: color .3s ease;
    z-index: 2;

    ec-icon {
      color: inherit;
    }
  }

  a {
    border: rem-calc(3px) solid transparent;
    border-radius: $control-border-radius;
    display: block;
    height: 100%;
    left: 0;
    position: absolute;
    top: 0;
    transition: transform .25s ease;
    width: 50%;
    z-index: 1;

    .toggle-handle {
      background-color: $ec-color-bg-content;
      border-radius: $control-border-radius;
      height: 100%;
    }
  }

  &.is-disabled {
    opacity: $control-opacity-disabled;
    background-color: $control-bg-disabled;
    color: $control-color-disabled;

    label {
      color: inherit !important;
      cursor: default;
    }

    a.toggle-handle {
      background-color: $ec-color-text-hint-light;
    }
  }
}

@mixin toggle-options {
  @each $count in (3,4,5) {
    .toggle-options-#{$count} {
      a {
        width: percentage(1 / $count);
      }

      input:checked:last-of-type ~ a {
        transform: translateX(percentage($count - 1));
      }

      @while $count > 2 {
        $count: $count - 1;
        input:checked:nth-of-type(#{$count}) ~ a {
          transform: translateX(percentage($count - 1));
        }
      }
    }
  }
}

@mixin form-heading() {
  @include form-control-label;
  font-weight: bold;
}

// Override the presentation of children controls when the parent control is
// untouched
@mixin form-control-untouched {
  input {
    &.ng-invalid.ng-touched {
      background-image: none;
      padding-left: $control-padding-x;

      &:not(.is-empty) ~ .units-left {
        left: 0;
      }
    }
    &:required:not(:disabled).is-empty {
      @include form-control-state(required, input);

      &:focus {
        @include form-control-state(focus, input);
      }
    }
  }
}

// Shared styles for controls that include a popup when the popup is open (combobox, dateinput)
@mixin form-control-open() {
  .textbox-group {
    ec-textbox ::ng-deep input {
      border-color: $control-border-color;
      box-shadow: none;
    }

    ec-button ::ng-deep button {
      background-color: $control-bg-active;
      box-shadow: none;
      color: $control-text-active;
    }
  }
}

@mixin form-control-read-only {
  border-color: transparent;
  background-color: $control-bg-read-only;
  background-image: none;
  color: $control-color;
  opacity: 1;
  user-select: none;
  pointer-events: none;
  overflow: hidden;
  white-space: nowrap;
}
