//
// Copyright 2017 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.
//

@use "sass:map";
@use "sass:math";
@use "sass:meta";
@use "@material/base/mixins" as base-mixins;
@use "@material/feature-targeting/functions" as feature-targeting-functions;
@use "@material/feature-targeting/mixins" as feature-targeting-mixins;
@use "@material/theme/mixins" as theme-mixins;
@use "@material/theme/variables" as theme-variables;
@use "./functions";
@use "./variables";

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

  @for $z-value from 0 through 24 {
    .mdc-elevation--z#{$z-value} {
      @include elevation($z-value, $query: $query);
    }
  }

  .mdc-elevation-transition {
    @include feature-targeting-mixins.targets($feat-animation) {
      transition: functions.transition-value();
    }

    @include feature-targeting-mixins.targets($feat-structure) {
      will-change: variables.$property;
    }
  }
}

///
/// Called once per application to set up the global default elevation styles.
///
@mixin overlay-common($query: feature-targeting-functions.all()) {
  $feat-animation: feature-targeting-functions.create-target($query, animation);
  $feat-structure: feature-targeting-functions.create-target($query, structure);

  @include overlay-selector_ {
    @include feature-targeting-mixins.targets($feat-structure) {
      @include base-mixins.emit-once('mdc-elevation/common/structure') {
        position: absolute;
        border-radius: inherit;
        opacity: 0;
        pointer-events: none;
      }
    }

    @include feature-targeting-mixins.targets($feat-animation) {
      @include base-mixins.emit-once('mdc-elevation/common/animation') {
        transition: functions.overlay-transition-value();
      }
    }

    @include base-mixins.emit-once('mdc-elevation/common/color') {
      @include overlay-fill-color(variables.$overlay-color, $query: $query);
    }
  }
}

///
/// Sets the shadow of the element.
///
/// @param {String} $box-shadow - The shadow to apply to the element.
///
@mixin shadow($box-shadow, $query: feature-targeting-functions.all()) {
  $feat-color: feature-targeting-functions.create-target($query, color);

  @include feature-targeting-mixins.targets($feat-color) {
    /* @alternate */
    box-shadow: $box-shadow;
  }
}

///
/// Sets the elevation overlay surface required positioning.
///
@mixin overlay-surface-position($query: feature-targeting-functions.all()) {
  $feat-structure: feature-targeting-functions.create-target($query, structure);

  @include feature-targeting-mixins.targets($feat-structure) {
    /* @alternate */
    position: relative;
  }
}

///
/// Sets the dimensions of the elevation overlay, including positioning and sizing.
///
/// @param {Number} $width - The width of the elevation overlay
/// @param {Number} [$height] - The height of the elevation overlay
/// @param {Boolean} [$has-content-sizing] - Set to false if the container has no content sizing
///
@mixin overlay-dimensions(
  $width,
  $height: $width,
  $has-content-sizing: true,
  $query: feature-targeting-functions.all()
) {
  $feat-structure: feature-targeting-functions.create-target($query, structure);

  @include overlay-selector_ {
    @include feature-targeting-mixins.targets($feat-structure) {
      width: $width;
      height: $height;

      @if $has-content-sizing {
        top: 0;
        /* @noflip */
        left: 0;
      } @else {
        top: 50%;
        /* @noflip */
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }
  }
}

///
/// Sets the elevation overlay fill color.
/// Expected to be called directly on the elevation overlay element.
///
/// @param {Color} $color - The color of the elevation overlay.
///
@mixin overlay-fill-color($color, $query: feature-targeting-functions.all()) {
  $feat-color: feature-targeting-functions.create-target($query, color);

  @include feature-targeting-mixins.targets($feat-color) {
    @include theme-mixins.prop(background-color, $color);
  }
}

///
/// Sets the elevation overlay opacity.
/// Expected to be called from a parent element.
///
/// @param {Number} $opacity - The opacity of the elevation overlay.
///
@mixin overlay-opacity($opacity, $query: feature-targeting-functions.all()) {
  $feat-color: feature-targeting-functions.create-target($query, color);

  @include overlay-selector_ {
    @include feature-targeting-mixins.targets($feat-color) {
      opacity: $opacity;
    }
  }
}

// Applies the correct CSS rules to an element to give it the elevation specified by $z-value.
// The $z-value must be between 0 and 24.
// If $color has an alpha channel, it will be ignored and overridden. To increase the opacity of the shadow, use
// $opacity-boost.
@mixin elevation(
  $z-value,
  $color: variables.$baseline-color,
  $opacity-boost: 0,
  $query: feature-targeting-functions.all()
) {
  @if meta.type-of($z-value) != number or not math.is-unitless($z-value) {
    @error "$z-value must be a unitless number, but received '#{$z-value}'";
  }

  @if $z-value < 0 or $z-value > 24 {
    @error "$z-value must be between 0 and 24, but received '#{$z-value}'";
  }

  $feat-color: feature-targeting-functions.create-target($query, color);

  $color: theme-variables.prop-value($color);

  $umbra-z-value: map.get(variables.$umbra-map, $z-value);
  $penumbra-z-value: map.get(variables.$penumbra-map, $z-value);
  $ambient-z-value: map.get(variables.$ambient-map, $z-value);

  $umbra-color: rgba($color, variables.$umbra-opacity + $opacity-boost);
  $penumbra-color: rgba($color, variables.$penumbra-opacity + $opacity-boost);
  $ambient-color: rgba($color, variables.$ambient-opacity + $opacity-boost);

  $box-shadow: (
    #{'#{$umbra-z-value} #{$umbra-color}'},
    #{'#{$penumbra-z-value} #{$penumbra-color}'},
    #{$ambient-z-value} $ambient-color
  );

  @include shadow($box-shadow, $query: $query);
}

// Private

///
/// Sets the elevation overlay transition value.
///
/// @param {String} $duration - The duration of the transition.
/// @param {String} $easing - The easing function for the transition.
/// @return {String}
/// @access private
///
@mixin overlay-selector_ {
  .mdc-elevation-overlay {
    @content;
  }
}
