//
// Copyright 2020 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// stylelint-disable selector-class-pattern --
// Selector '.mdc-*' should only be used in this project.

@use 'sass:math';
@use '../animation/functions' as animation-functions;
@use '../feature-targeting/feature-targeting';
@use '../rtl/rtl';
@use '../theme/gss';
@use '../theme/theme';
@use './variables';
@use './keyframes';

@mixin core-styles($query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);
  $feat-animation: feature-targeting.create-target($query, animation);

  @include color(variables.$color, $query);
  @include track-color(variables.$track-color, $query: $query);

  @include feature-targeting.targets($feat-animation) {
    @include keyframes.container-rotate-keyframes_;
    @include keyframes.spinner-layer-rotate-keyframes_;
    @include keyframes.color-1-fade-in-out-keyframes_;
    @include keyframes.color-2-fade-in-out-keyframes_;
    @include keyframes.color-3-fade-in-out-keyframes_;
    @include keyframes.color-4-fade-in-out-keyframes_;
    @include keyframes.left-spin-keyframes_;
    @include keyframes.right-spin-keyframes_;
  }

  .mdc-circular-progress {
    @include feature-targeting.targets($feat-structure) {
      display: inline-flex;
      position: relative;
      @include rtl.ignore-next-line();
      direction: ltr;
      line-height: 0;
    }

    @include feature-targeting.targets($feat-animation) {
      transition: animation-functions.exit-temporary(opacity, 250ms);
    }
  }

  .mdc-circular-progress__determinate-container,
  .mdc-circular-progress__indeterminate-circle-graphic,
  .mdc-circular-progress__indeterminate-container,
  .mdc-circular-progress__spinner-layer {
    @include feature-targeting.targets($feat-structure) {
      position: absolute;
      width: 100%;
      height: 100%;
    }
  }

  .mdc-circular-progress__determinate-container {
    @include feature-targeting.targets($feat-structure) {
      transform: rotate(-90deg);
    }
  }

  .mdc-circular-progress__indeterminate-container {
    @include feature-targeting.targets($feat-structure) {
      // Font size, letter spacing and nowrap are required in order to ignore
      // whitespaces between HTML elements.
      font-size: 0;
      letter-spacing: 0;
      white-space: nowrap;
      opacity: 0;
    }
  }

  .mdc-circular-progress__determinate-circle-graphic,
  .mdc-circular-progress__indeterminate-circle-graphic {
    @include feature-targeting.targets($feat-structure) {
      fill: transparent;
    }
  }

  .mdc-circular-progress__determinate-circle {
    @include feature-targeting.targets($feat-animation) {
      transition: animation-functions.enter(stroke-dashoffset, 500ms);
    }
  }

  .mdc-circular-progress__gap-patch {
    @include feature-targeting.targets($feat-structure) {
      position: absolute;
      top: 0;
      @include rtl.ignore-next-line();
      left: 47.5%;
      box-sizing: border-box;
      width: 5%;
      height: 100%;
      overflow: hidden;
    }

    .mdc-circular-progress__indeterminate-circle-graphic {
      @include feature-targeting.targets($feat-structure) {
        @include rtl.ignore-next-line();
        left: -900%;
        width: 2000%;
        transform: rotate(180deg);
      }
    }
  }

  .mdc-circular-progress__circle-clipper {
    @include feature-targeting.targets($feat-structure) {
      display: inline-flex;
      position: relative;
      width: 50%;
      height: 100%;
      overflow: hidden;
    }

    .mdc-circular-progress__indeterminate-circle-graphic {
      @include feature-targeting.targets($feat-structure) {
        width: 200%;
      }
    }
  }

  .mdc-circular-progress__circle-right {
    .mdc-circular-progress__indeterminate-circle-graphic {
      @include feature-targeting.targets($feat-structure) {
        @include rtl.ignore-next-line();
        left: -100%;
      }
    }
  }

  .mdc-circular-progress--indeterminate {
    .mdc-circular-progress__determinate-container {
      @include feature-targeting.targets($feat-structure) {
        opacity: 0;
      }
    }

    .mdc-circular-progress__indeterminate-container {
      @include feature-targeting.targets($feat-structure) {
        opacity: 1;
      }
    }

    @include indeterminate-active-animations_($query);
  }

  .mdc-circular-progress--closed {
    @include feature-targeting.targets($feat-structure) {
      opacity: 0;
    }
  }
}

// Public mixins

/// Customizes the stroke-color of the indicator. Applies to the
/// determinate variant, and also the indeterminate variant unless the
/// four-color mixin is applied.
/// @param {Color} $color - The desired stroke color.
/// @see {mixin} indeterminate-colors
@mixin color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-circular-progress__determinate-circle,
  .mdc-circular-progress__indeterminate-circle-graphic {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(stroke, $color);
    }
  }
}

/// Customizes the track color of the indicator. Applies to the
/// determinate variant only.
/// @param {Color} $color - The desired track color.
@mixin track-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-circular-progress__determinate-track {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(stroke, $color);
    }
  }
}

/// Applies four animated stroke-colors to the indeterminate indicator.
/// Applicable to the indeterminate variant only and overrides any single color
/// currently set.
/// @param {List} $colors - A list of four desired colors.
/// @see {mixin} color
@mixin indeterminate-colors($colors, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  @if length($colors) != 4 {
    @error "`mdc-circular-progress-colors` accepts exactly 4 colors";
  }

  @for $i from 1 through 4 {
    .mdc-circular-progress__color-#{$i}
      .mdc-circular-progress__indeterminate-circle-graphic {
      @include feature-targeting.targets($feat-color) {
        @include theme.property(stroke, nth($colors, $i));
      }
    }
  }
}

// Private mixins

/// Sets the animations for the indicator in indeterminate mode.
/// @access private
@mixin indeterminate-active-animations_($query: feature-targeting.all()) {
  $feat-animation: feature-targeting.create-target($query, animation);

  .mdc-circular-progress__indeterminate-container {
    @include feature-targeting.targets($feat-animation) {
      $duration: math.div(
        360deg * variables.$arc-time,
        variables.$arc-start-rotation-interval + (360 - variables.$arc-size)
      );

      // stylelint-disable-next-line no-unknown-animations -- allow animations
      animation: mdc-circular-progress-container-rotate $duration linear
        infinite;
    }
  }

  $spinner-rotate-animation: mdc-circular-progress-spinner-layer-rotate 4 *
    variables.$arc-time variables.$timing-function infinite both;

  .mdc-circular-progress__spinner-layer {
    @include feature-targeting.targets($feat-animation) {
      animation: $spinner-rotate-animation;
    }
  }

  @for $i from 1 through 4 {
    $color-fade-in-out-animation: mdc-circular-progress-color-#{$i}-fade-in-out
      4 *
      variables.$arc-time
      variables.$timing-function
      infinite
      both;

    .mdc-circular-progress__color-#{$i} {
      @include feature-targeting.targets($feat-animation) {
        animation: $spinner-rotate-animation, $color-fade-in-out-animation;
      }
    }
  }

  .mdc-circular-progress__circle-left
    .mdc-circular-progress__indeterminate-circle-graphic {
    @include feature-targeting.targets($feat-animation) {
      @include gss.annotate(
        (
          noflip: true,
        )
      );
      // stylelint-disable-next-line no-unknown-animations -- allow animations
      animation: mdc-circular-progress-left-spin variables.$arc-time
        variables.$timing-function infinite both;
    }
  }

  .mdc-circular-progress__circle-right
    .mdc-circular-progress__indeterminate-circle-graphic {
    @include feature-targeting.targets($feat-animation) {
      @include gss.annotate(
        (
          noflip: true,
        )
      );
      // stylelint-disable-next-line no-unknown-animations -- allow animations
      animation: mdc-circular-progress-right-spin variables.$arc-time
        variables.$timing-function infinite both;
    }
  }
}
