//
// 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 '../feature-targeting/feature-targeting';
@use '../ripple/ripple-theme';
@use '../theme/state';
@use '../theme/theme';

/// Default color for slider (track, thumb, ripple).
$color: primary;
$disabled-color: on-surface;

// Thumb variables.
$value-indicator-color: #000;
$value-indicator-opacity: 0.6;
$value-indicator-text-color: on-primary;

// Track variables.
$track-inactive-opacity: 0.24;
$tick-mark-active-color: on-primary;
$tick-mark-inactive-color: primary;
$tick-mark-opacity: 0.6;

/// Customizes active track color, using a Color or state Map.
/// - To set only the default color, provide a single Color.
/// - To set one or more state colors, provide a state Map with optional keys.
/// - Supported state Map keys: `default`, `disabled`.
///
/// @example
///   @include track-active-color(blue);
///   @include track-active-color((disabled: gray));
///
/// @param {Color | Map} $color-or-map - The label's color or a state Map
@mixin track-active-color($color-or-map, $query: feature-targeting.all()) {
  @include _set-track-active-color(
    state.get-default-state($color-or-map),
    $query
  );

  $_disabled-color: state.get-disabled-state($color-or-map);
  @if $_disabled-color {
    &.mdc-slider--disabled {
      @include _set-track-active-color($_disabled-color, $query);
    }
  }
}

@mixin _set-track-active-color($color, $query) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-slider__track--active_fill {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(border-color, $color);
    }
  }
}

/// Customizes inactive track color, using a Color or state Map.
/// - To set only the default color, provide a single Color.
/// - To set one or more state colors, provide a state Map with optional keys.
/// - Supported state Map keys: `default`, `disabled`.
///
/// @example
///   @include track-inactive-color(blue);
///   @include track-inactive-color((disabled: gray));
///
/// @param {Color | Map} $color-or-map - The label's color or a state Map
@mixin track-inactive-color(
  $color-or-map,
  $opacity: $track-inactive-opacity,
  $query: feature-targeting.all()
) {
  @include _set-track-inactive-color(
    state.get-default-state($color-or-map),
    $opacity,
    $query
  );

  $_disabled-color: state.get-disabled-state($color-or-map);
  @if $_disabled-color {
    &.mdc-slider--disabled {
      @include _set-track-inactive-color($_disabled-color, $opacity, $query);
    }
  }
}

@mixin _set-track-inactive-color($color, $opacity, $query) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-slider__track--inactive {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(background-color, $color);

      opacity: $opacity;
    }
  }
}

/// Customizes thumb color, using a Color or state Map.
/// - To set only the default color, provide a single Color.
/// - To set one or more state colors, provide a state Map with optional keys.
/// - Supported state Map keys: `default`, `disabled`.
///
/// @example
///   @include thumb-color(blue);
///   @include thumb-color((disabled: gray));
///
/// @param {Color | Map} $color-or-map - The label's color or a state Map
@mixin thumb-color($color-or-map, $query: feature-targeting.all()) {
  @include _set-thumb-color(state.get-default-state($color-or-map), $query);

  $_disabled-color: state.get-disabled-state($color-or-map);
  @if $_disabled-color {
    &.mdc-slider--disabled {
      @include _set-thumb-color($_disabled-color, $query);
    }
  }
}

@mixin _set-thumb-color($color, $query) {
  $feat-color: feature-targeting.create-target($query, color);

  @include feature-targeting.targets($feat-color) {
    .mdc-slider__thumb-knob {
      @include theme.property(background-color, $color);
      @include theme.property(border-color, $color);
    }

    .mdc-slider__thumb--top {
      .mdc-slider__thumb-knob,
      &.mdc-slider__thumb:hover .mdc-slider__thumb-knob,
      &.mdc-slider__thumb--focused .mdc-slider__thumb-knob {
        border-color: #fff;
      }
    }
  }
}

///
/// Customizes thumb ripple color.
/// @param {Color | String} $color Either a valid color value or a key from
///     `$theme-variables.property-values`.
///
@mixin thumb-ripple-color($color, $query: feature-targeting.all()) {
  .mdc-slider__thumb {
    @include ripple-theme.states($color: $color, $query: $query);
  }
}

/// Customizes thumb color when thumb is activated (hover, focused, or pressed
/// state).
///
/// @param {Color} $color - The thumb's color
@mixin thumb-activated-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-slider__thumb:hover,
  .mdc-slider__thumb--focused {
    @include _set-thumb-color($color, $query);
  }
}

/// Customizes color of active tick marks, using a Color or state Map.
/// - To set only the default color, provide a single Color.
/// - To set one or more state colors, provide a state Map with optional keys.
/// - Supported state Map keys: `default`, `disabled`.
///
/// @example
///   @include tick-mark-active-color(blue);
///   @include tick-mark-active-color((disabled: gray));
///
/// @param {Color | Map} $color-or-map - The label's color or a state Map
@mixin tick-mark-active-color(
  $color-or-map,
  $opacity: $tick-mark-opacity,
  $query: feature-targeting.all()
) {
  @include _set-tick-mark-active-color(
    state.get-default-state($color-or-map),
    $opacity,
    $query
  );

  $_disabled-color: state.get-disabled-state($color-or-map);
  @if $_disabled-color {
    &.mdc-slider--disabled {
      @include _set-tick-mark-active-color($_disabled-color, $opacity, $query);
    }
  }
}

@mixin _set-tick-mark-active-color($color, $opacity, $query) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-slider__tick-mark--active {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(background-color, $color);

      opacity: $opacity;
    }
  }
}

/// Customizes color of inactive tick marks, using a Color or state Map.
/// - To set only the default color, provide a single Color.
/// - To set one or more state colors, provide a state Map with optional keys.
/// - Supported state Map keys: `default`, `disabled`.
///
/// @example
///   @include tick-mark-inactive-color(blue);
///   @include tick-mark-inactive-color((disabled: gray));
///
/// @param {Color | Map} $color-or-map - The label's color or a state Map
@mixin tick-mark-inactive-color(
  $color-or-map,
  $opacity: $tick-mark-opacity,
  $query: feature-targeting.all()
) {
  @include _set-tick-mark-inactive-color(
    state.get-default-state($color-or-map),
    $opacity,
    $query
  );

  $_disabled-color: state.get-disabled-state($color-or-map);
  @if $_disabled-color {
    &.mdc-slider--disabled {
      @include _set-tick-mark-inactive-color(
        $_disabled-color,
        $opacity,
        $query
      );
    }
  }
}

@mixin _set-tick-mark-inactive-color($color, $opacity, $query) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-slider__tick-mark--inactive {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(background-color, $color);

      opacity: $opacity;
    }
  }
}

///
/// Customizes color and opacity of the value indicator.
/// @param {Color | String} $color Either a valid color value or a key from
///     `$theme-variables.property-values`.
/// @param {number} $opacity
///
@mixin value-indicator-color(
  $color,
  $opacity,
  $query: feature-targeting.all()
) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-slider__value-indicator {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(background-color, $color);

      opacity: $opacity;
    }
  }

  // Caret.
  .mdc-slider__value-indicator::before {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(border-top-color, $color);
    }
  }
}

///
/// Customizes color of the value indicator text.
/// @param {Color | String} $color Either a valid color value or a key from
///     `$theme-variables.property-values`.
///
@mixin value-indicator-text-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-slider__value-indicator {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(color, $color);
    }
  }
}
