@use 'sass:math';
@import '../animation/variables.scss';

$name: 'ivue-switch';

:root {
  // color
  --#{$name}-bg: #858585;

  // emboss
  --#{$name}-disabled-emboss-thumb-color: #fafafa;
  --#{$name}-disabled-emboss-track-color: rgba(0, 0, 0, 0.12);
  --#{$name}-disabled-emboss-track-checked-color: rgba(0, 0, 0, 0.38);

  // disabled
  --#{$name}-disabled-bg: #f8f9fd;
}

$default-width: 44px;
$default-after-width: 18px;

$emboss-default-width: 38px;
$emboss-default-height: 22px;
$emboss-large-width: 60px;
$emboss-large-height: 28px;
$emboss-small-width: 28px;
$emboss-small-height: 16px;
$emboss-margin: 7px;

@mixin transition() {
  transition: left 0.2s ease-in-out, width 0.2s ease-in-out;
}

.#{$name} {
  position: relative;
  display: inline-block;
  width: $default-width;
  height: math.div($default-width, 2);
  border-radius: math.div($default-width, 2);
  vertical-align: middle;
  border: 1px solid var(--#{$name}-bg);
  background: var(--#{$name}-bg);
  cursor: pointer;
  user-select: none;
  outline: none;
  transition: var(--ivue-all-transition) 0s;
  -webkit-tap-highlight-color: transparent;

  &:focus {
    outline: 0;

    &:hover {
      box-shadow: none;
    }
  }

  &:after {
    content: '';
    position: absolute;
    top: 1px;
    left: 1px;
    width: $default-after-width;
    height: $default-after-width;
    border-radius: $default-after-width;
    background: var(--ivue-white-color);
    cursor: pointer;

    @include transition;
  }

  &:active:not(.ivue-switch-disabled) {
    &:after {
      width: calc($default-after-width + 8px);
    }
  }

  &:before {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    display: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background-color: transparent;
    z-index: 1;
    border: 1px solid currentColor;
    border-color: transparent transparent transparent currentColor;
    animation: switch-loading 1s linear;
    animation-iteration-count: infinite;
  }

  // input
  &-input {
    position: absolute;
    width: 0;
    height: 0;
    opacity: 0;
    margin: 0;
  }

  // 未激活
  &-false {
    border-color: currentColor;
    background: currentColor;
  }

  // 内部文字样式
  &-inner {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 23px;
    display: flex;
    align-items: center;
    color: var(--ivue-white-color);
    font-size: var(--ivue-font-small-size);

    i {
      position: relative;
      width: 12px;
      min-width: auto;
      height: 12px;
      text-align: center;
    }
  }

  // 激活
  &-checked {
    border-color: currentColor;
    background: currentColor;

    .#{$name}-inner {
      left: 7px;
    }

    &:after {
      left: calc(math.div($default-width, 2) + 1px);
    }

    &:before {
      left: 24px;
    }

    &:active:not(.ivue-switch-disabled):after {
      left: calc(
        math.div($default-width, 2) - math.div($default-after-width, 2) + 2px
      );
    }
  }

  // 禁用
  &-disabled {
    cursor: var(--ivue-cursor-disabled);
    border-color: var(--ivue-border-color) !important;
    background-color: var(--#{$name}-disabled-bg) !important;

    &:after {
      background: var(--ivue-disabled-color);
      cursor: var(--ivue-cursor-disabled);
    }

    .#{$name}-inner {
      color: var(--ivue-disabled-color);
    }
  }

  // 小
  &-small {
    width: 28px;
    height: 16px;
    line-height: 14px;

    &:after {
      width: 12px;
      height: 12px;
      border-radius: 12px;
    }

    &:active:not(.ivue-switch-disabled) {
      &.#{$name}-checked:after {
        left: 11px;
      }

      &:after {
        width: 14px;
      }
    }

    &:before {
      width: 10px;
      height: 10px;
    }

    &.#{$name}-checked:after {
      left: 13px;
    }

    &.#{$name}-checked:before {
      left: 14px;
      width: 10px;
      height: 10px;
    }
  }

  // 大
  &-large {
    width: 56px;

    &.#{$name}-checked:active:not(.ivue-switch-disabled) {
      &:after {
        left: 23px;
      }
    }

    &:active:not(.ivue-switch-disabled):after {
      width: 30px;
    }

    &.#{$name}-checked:after {
      left: 35px;
    }

    &.#{$name}-checked:before {
      left: 36px;
    }
  }

  // loading
  &-loading {
    opacity: 0.6;

    &:before {
      display: block;
    }

    &.#{$name}-checked {
      &:active:not(.ivue-switch-disabled):after {
        left: calc(math.div($default-width, 2) + 1px);
      }
    }

    &:active:not(.ivue-switch-disabled):after {
      width: $default-after-width;
    }

    // 大型
    &.#{$name}-large {
      &.#{$name}-checked:after {
        left: 35px !important;
      }

      &:active:not(.ivue-switch-disabled):after {
        width: 18px;
      }
    }

    // 小型
    &.#{$name}-small {
      &.#{$name}-checked {
        &:active:not(.ivue-switch-disabled):after {
          width: 12px !important;
          left: 13px !important;
        }
      }

      &:active:not(.ivue-switch-disabled):after {
        width: 12px !important;
      }
    }
  }

  // 扁平化
  &-emboss {
    position: relative;
    display: inline-flex;
    width: $emboss-default-width;
    height: $emboss-default-height;
    flex: 0 0 auto;

    // 外层
    &--wrapper {
      position: relative;
      display: inline-flex;
      align-items: center;
      vertical-align: middle;
      user-select: none;
      outline: none;
      cursor: pointer;

      .#{$name}-inner {
        position: relative;
        margin-left: 8px;
        left: 0;
        color: var(--ivue-text-color);
      }
    }

    // loading
    &--loading {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      margin: auto;
      width: $emboss-default-height - 4;
      height: $emboss-default-height - 4;
      border-radius: 50%;
      border: 1px solid currentColor;
      border-color: transparent transparent transparent currentColor;
      animation: switch-loading 1s linear;
      animation-iteration-count: infinite;
      background-color: transparent;
    }

    // 跟踪
    &--track {
      position: absolute;
      left: 1px;
      right: 2px;
      top: calc(50% - 5.5px);
      width: $emboss-default-width - 2;
      height: math.div($emboss-default-height, 2);
      border-radius: 8px;
      opacity: 0.6;
      color: var(--#{$name}-bg);
      background-color: currentColor;
      transition: $ivue-transition-default;
      transition-property: color, background-color;

      // 激活
      &__checked {
        color: currentColor;
      }
    }

    // 指示器
    &--thumb {
      position: relative;
      width: $emboss-default-height;
      height: $emboss-default-height;
      color: var(--ivue-white-color);
      border-radius: 50%;
      box-shadow: 0px 2px 4px -1px rgba(0, 0, 0, 0.2),
        0px 4px 5px 0px rgba(0, 0, 0, 0.14),
        0px 1px 10px 0px rgba(0, 0, 0, 0.12);
      pointer-events: none;
      background-color: currentColor;
      transition: $ivue-transition-default;
      transition-property: color, transform;

      // 激活
      &__checked {
        color: currentColor;
        transform: translate(
          $emboss-default-height - (math.div($emboss-default-height, 4)),
          0
        );
      }
    }

    // ripple
    &--ripple {
      position: absolute;
      top: calc(50% - 25px);
      left: -14px;
      width: $emboss-default-width - 2;
      height: $emboss-default-width - 2;
      margin: $emboss-margin;
      border-radius: 50%;
      transition: $ivue-transition-default;

      // 激活
      &__checked {
        transform: translate(
          $emboss-default-height - (math.div($emboss-default-height, 4)),
          0
        );
      }
    }

    // 禁用
    &--disabled {
      cursor: var(--ivue-cursor-disabled);

      .#{$name}-emboss--thumb {
        color: var(--#{$name}-disabled-emboss-thumb-color) !important;
      }

      .#{$name}-emboss--track {
        caret-color: var(--#{$name}-disabled-emboss-track-color) !important;
        color: var(--#{$name}-disabled-emboss-track-color) !important;

        &__checked {
          caret-color: var(
            --#{$name}-disabled-emboss-track-checked-color
          ) !important;
          color: var(--#{$name}-disabled-emboss-track-checked-color) !important;
        }
      }
    }

    // 小
    &--small {
      width: $emboss-small-width;
      height: $emboss-small-height;

      .#{$name}-emboss--loading {
        width: $emboss-small-height - 4;
        height: $emboss-small-height - 4;
      }

      .#{$name}-emboss--track {
        top: calc(50% - 4px);
        left: 1px;
        width: $emboss-small-width - 2;
        height: math.div($emboss-small-height, 2);
      }

      .#{$name}-emboss--thumb {
        width: $emboss-small-height;
        height: $emboss-small-height;

        &__checked {
          transform: translate(
            $emboss-small-height - (math.div($emboss-small-height, 4)),
            0
          );
        }
      }

      .#{$name}-emboss--ripple {
        top: calc(50% - 20px);
        left: -((math.div($emboss-small-height, 2)) +
              (math.div($emboss-small-height, 4)));
        width: $emboss-small-width - 2;
        height: $emboss-small-width - 2;

        &__checked {
          transform: translate(
            $emboss-small-height - (math.div($emboss-small-height, 4)),
            0
          );
        }
      }
    }

    // 大
    &--large {
      width: $emboss-large-width;
      height: $emboss-large-height;

      .#{$name}-emboss--loading {
        width: $emboss-large-height - 4;
        height: $emboss-large-height - 4;
      }

      .#{$name}-emboss--track {
        top: calc(50% - 7px);
        left: 3px;
        width: $emboss-large-width - 10;
        height: math.div($emboss-large-height, 2);
      }

      .#{$name}-emboss--thumb {
        width: $emboss-large-height;
        height: $emboss-large-height;

        &__checked {
          transform: translate(
            $emboss-large-height + (math.div($emboss-large-height, 4)) - 9,
            0
          );
        }
      }

      .#{$name}-emboss--ripple {
        top: calc(50% - 32px);
        left: -(
            $emboss-large-width - $emboss-large-height -
              (math.div($emboss-large-height, 4)) - 2
          ) + 4;
        width: $emboss-large-width - 10;
        height: $emboss-large-width - 10;

        &__checked {
          transform: translate(
            $emboss-large-height + (math.div($emboss-large-height, 4)) - 9,
            0
          );
        }
      }
    }
  }
}

@keyframes switch-loading {
  0% {
    transform: rotate(0);
  }

  100% {
    transform: rotate(360deg);
  }
}
