//
// Copyright IBM Corp. 2016, 2018
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//

//-----------------------------
// Toggle
//-----------------------------

@import '../../globals/scss/vars';
@import '../../globals/scss/helper-mixins';
@import '../../globals/scss/typography';
@import '../../globals/scss/vendor/@carbon/elements/scss/import-once/import-once';
@import '../form/form';

/// Toggle styles
/// @access private
/// @group toggle
@mixin toggle {
  .#{$prefix}--toggle {
    @include hidden;

    &:focus {
      outline: none;
    }
  }

  .#{$prefix}--toggle__label {
    @include type-style('label-01');

    position: relative;
    display: flex;
    align-items: center;
    margin: $carbon--spacing-03 0;
    cursor: pointer;
  }

  .#{$prefix}--toggle__appearance {
    position: relative;
    width: carbon--rem(48px);
    height: carbon--rem(24px);

    // Toggle background oval
    &::before {
      position: absolute;
      top: 0;
      display: block;
      box-sizing: border-box;
      width: carbon--rem(48px);
      height: carbon--rem(24px);
      background-color: $ui-04;
      border-radius: carbon--rem(15px);
      // Corresponds to the double-border for focused state (`0 0 0 1px $ui-02, 0 0 0 3px $focus`)
      box-shadow: 0 0 0 1px transparent, 0 0 0 3px transparent;
      cursor: pointer;
      transition: box-shadow $duration--fast-01 motion(exit, productive),
        background-color $duration--fast-01 motion(exit, productive);
      content: '';
      will-change: box-shadow;
    }

    // Toggle circle
    &::after {
      position: absolute;
      top: carbon--rem(3px);
      left: carbon--rem(3px);
      display: block;
      box-sizing: border-box;
      width: carbon--rem(18px);
      height: carbon--rem(18px);
      background-color: $icon-03;
      border-radius: 50%;
      cursor: pointer;
      transition: transform $duration--fast-01 motion(exit, productive);
      content: '';
    }
  }

  .#{$prefix}--toggle__check {
    position: absolute;
    top: carbon--rem(6px);
    left: carbon--rem(6px);
    z-index: 1;
    width: carbon--rem(6px);
    height: carbon--rem(5px);
    transform: scale(0.2);
    transition: $duration--fast-01 motion(exit, productive);
    fill: $icon-03;
  }

  .#{$prefix}--toggle__text--left,
  .#{$prefix}--toggle__text--right {
    @include type-style('body-short-01');

    position: relative;
    margin-left: $carbon--spacing-03;
  }

  .#{$prefix}--toggle__text--left {
    position: absolute;
    left: carbon--rem(48px);
  }

  .#{$prefix}--toggle:checked
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__text--left,
  .#{$prefix}--toggle:not(:checked)
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__text--right {
    visibility: hidden;
  }

  .#{$prefix}--toggle:checked
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__text--right,
  .#{$prefix}--toggle:not(:checked)
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__text--left {
    display: inline;
  }

  .#{$prefix}--toggle:checked
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__appearance {
    &::before {
      background-color: $support-02;
    }

    &::after {
      background-color: $icon-03;
      transform: translateX(carbon--rem(24px));
    }
  }

  //----------------------------------------------
  // Focus
  // ---------------------------------------------
  .#{$prefix}--toggle
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__appearance::before {
    // Corresponds to the double-border for focused state (`0 0 0 1px $ui-02, 0 0 0 3px $focus`)
    box-shadow: 0 0 0 1px transparent, 0 0 0 3px transparent;
  }

  .#{$prefix}--toggle:focus + .#{$prefix}--toggle__label,
  .#{$prefix}--toggle:active
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__appearance::before {
    box-shadow: 0 0 0 1px $ui-02, 0 0 0 3px $focus;
  }

  //----------------------------------------------
  // Disabled
  // ---------------------------------------------
  .#{$prefix}--toggle:disabled + .#{$prefix}--toggle__label {
    cursor: not-allowed;
  }

  .#{$prefix}--toggle:disabled
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__appearance {
    &::before {
      background-color: $disabled-01;
    }

    &::after {
      background-color: $disabled-02;
    }

    &::before,
    &::after {
      cursor: not-allowed;
      transition: $duration--fast-01 motion(exit, productive);
    }
  }

  .#{$prefix}--toggle:disabled
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__text--left,
  .#{$prefix}--toggle:disabled
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__text--right {
    color: $disabled;
  }

  .#{$prefix}--toggle:disabled:active
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__appearance:before {
    box-shadow: none;
  }

  .#{$prefix}--toggle:disabled
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__check {
    fill: $disabled-02;
  }

  //----------------------------------------------
  // Small toggle
  // ---------------------------------------------

  .#{$prefix}--toggle--small
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__appearance {
    width: carbon--rem(32px);
    height: carbon--rem(16px);

    &::before {
      top: 0;
      box-sizing: border-box;
      width: carbon--rem(32px);
      height: carbon--rem(16px);
      border-radius: 0.9375rem;
    }

    &::after {
      top: carbon--rem(3px);
      left: carbon--rem(3px);
      width: carbon--rem(10px);
      height: carbon--rem(10px);
    }
  }

  .#{$prefix}--toggle--small:checked
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__check {
    transform: scale(1) translateX(carbon--rem(16px));
    fill: $support-02;
  }

  .#{$prefix}--toggle--small
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__text--left {
    left: carbon--rem(32px);
  }

  .#{$prefix}--toggle--small:checked
    + .#{$prefix}--toggle__label
    .#{$prefix}--toggle__appearance {
    &::after {
      margin-left: 0;
      transform: translateX(rem(17px));
    }
  }

  // -----------------------------------------------------
  // new accessible toggle
  // TODO: deprecate styles above this line
  // -----------------------------------------------------

  .#{$prefix}--toggle-input {
    @include hidden;

    &:focus {
      outline: none;
    }
  }

  .#{$prefix}--toggle-input__label {
    @include type-style('label-01');

    display: flex;
    flex-direction: column;
    align-items: flex-start;
    color: $text-02;
    cursor: pointer;
  }

  .#{$prefix}--toggle__switch {
    position: relative;
    display: flex;
    align-items: center;
    width: carbon--rem(48px);
    height: carbon--rem(24px);
    cursor: pointer;

    // Toggle background oval
    &::before {
      position: absolute;
      top: 0;
      display: block;
      box-sizing: border-box;
      width: carbon--rem(48px);
      height: carbon--rem(24px);
      background-color: $ui-04;
      border-radius: carbon--rem(15px);
      // Corresponds to the double-border for focused state (`0 0 0 1px $ui-02, 0 0 0 3px $focus`)
      box-shadow: 0 0 0 1px transparent, 0 0 0 3px transparent;
      transition: box-shadow $duration--fast-01 motion(exit, productive),
        background-color $duration--fast-01 motion(exit, productive);
      content: '';
      will-change: box-shadow;
    }

    // Toggle circle
    &::after {
      position: absolute;
      top: carbon--rem(3px);
      left: carbon--rem(3px);
      display: block;
      box-sizing: border-box;
      width: carbon--rem(18px);
      height: carbon--rem(18px);
      background-color: $icon-03;
      border-radius: 50%;
      transition: transform $duration--fast-01 motion(exit, productive);
      content: '';
    }

    .#{$prefix}--toggle-input__label & {
      margin-top: $carbon--spacing-05;
    }
  }

  .#{$prefix}--toggle__text--off,
  .#{$prefix}--toggle__text--on {
    @include type-style('body-short-01');

    position: absolute;
    // top offset needed to vertically center absolutely positioned flex child in IE11
    top: 50%;
    margin-left: rem(56px);
    white-space: nowrap;
    transform: translateY(-50%);
    user-select: none;
  }

  //----------------------------------------------
  // Checked
  // ---------------------------------------------
  .#{$prefix}--toggle-input:checked
    + .#{$prefix}--toggle-input__label
    > .#{$prefix}--toggle__switch
    > .#{$prefix}--toggle__text--off,
  .#{$prefix}--toggle-input:not(:checked)
    + .#{$prefix}--toggle-input__label
    > .#{$prefix}--toggle__switch
    > .#{$prefix}--toggle__text--on {
    visibility: hidden;
  }

  .#{$prefix}--toggle-input:checked
    + .#{$prefix}--toggle-input__label
    > .#{$prefix}--toggle__switch {
    &::before {
      background-color: $support-02;
    }

    &::after {
      background-color: $icon-03;
      transform: translateX(carbon--rem(24px));
    }
  }

  //----------------------------------------------
  // Focus and active
  // ---------------------------------------------
  .#{$prefix}--toggle-input:focus
    + .#{$prefix}--toggle-input__label
    > .#{$prefix}--toggle__switch::before,
  .#{$prefix}--toggle-input:active
    + .#{$prefix}--toggle-input__label
    > .#{$prefix}--toggle__switch::before {
    box-shadow: 0 0 0 1px $ui-02, 0 0 0 3px $focus;
  }

  //----------------------------------------------
  // Disabled
  // ---------------------------------------------
  .#{$prefix}--toggle-input:disabled + .#{$prefix}--toggle-input__label {
    color: $disabled;
    cursor: not-allowed;
  }

  .#{$prefix}--toggle-input:disabled
    + .#{$prefix}--toggle-input__label
    > .#{$prefix}--toggle__switch {
    cursor: not-allowed;

    &::before {
      background-color: $disabled-01;
    }

    &::after {
      background-color: $disabled-02;
    }

    &::before,
    &::after {
      cursor: not-allowed;
      transition: $duration--fast-01 motion(exit, productive);
    }
  }

  .#{$prefix}--toggle-input:disabled:active
    + .#{$prefix}--toggle-input__label
    > .#{$prefix}--toggle__switch::before {
    box-shadow: none;
  }

  //----------------------------------------------
  // Small toggle
  // ---------------------------------------------
  .#{$prefix}--toggle-input--small + .#{$prefix}--toggle-input__label {
    > .#{$prefix}--toggle__switch {
      width: carbon--rem(32px);
      height: carbon--rem(16px);

      &::before {
        width: carbon--rem(32px);
        height: carbon--rem(16px);
        border-radius: 0.9375rem;
      }

      &::after {
        width: carbon--rem(10px);
        height: carbon--rem(10px);
      }
    }

    .#{$prefix}--toggle__text--off,
    .#{$prefix}--toggle__text--on {
      margin-left: carbon--rem(40px);
    }
  }

  .#{$prefix}--toggle-input--small:checked + .#{$prefix}--toggle-input__label {
    > .#{$prefix}--toggle__switch::after {
      transform: translateX(carbon--rem(17px));
    }

    .#{$prefix}--toggle__check {
      transform: scale(1) translateX(carbon--rem(16px));
      fill: $support-02;
    }
  }

  .#{$prefix}--toggle-input--small:disabled:checked
    + .#{$prefix}--toggle-input__label
    .#{$prefix}--toggle__check {
    fill: $disabled-02;
  }

  //----------------------------------------------
  // Skeleton
  // ---------------------------------------------

  .#{$prefix}--toggle__label.#{$prefix}--skeleton {
    flex-direction: column;
    align-items: flex-start;

    .#{$prefix}--toggle__label-text {
      margin-bottom: $carbon--spacing-03;
    }
  }
}

@include exports('toggle') {
  @include toggle;
}
