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

@import '../variables';

// returns the lower grid boundary or null if the smallest grid is selected
@function mdc-layout-grid-breakpoint-min($size) {
  @if not map-has-key($ray-layout-grid-columns, $size) {
    @error "Invalid style specified! Choose one of #{map-keys($ray-layout-grid-columns)}";
  }
  $min: map-get($ray-layout-grid-breakpoints, $size);

  @return if($min > 0, $min, null);
}

// returns the upper grid boundary or null if the largest grid is selected
@function mdc-layout-grid-breakpoint-max($size) {
  @if not map-has-key($ray-layout-grid-columns, $size) {
    @error "Invalid style specified! Choose one of #{map-keys($ray-layout-grid-columns)}";
  }
  $names: map-keys($ray-layout-grid-columns);
  $n: index($names, $size);
  $prev: if($n > 1, nth($names, $n - 1), null);

  @return if($prev, (mdc-layout-grid-breakpoint-min($prev) - 1px), null);
}

// Private mixins, meant for internal use.
@mixin mdc-layout-grid-media-query_($size) {
  @if not map-has-key($ray-layout-grid-columns, $size) {
    @error "Invalid style specified! Choose one of #{map-keys($ray-layout-grid-columns)}";
  }

  $min: mdc-layout-grid-breakpoint-min($size);
  $max: mdc-layout-grid-breakpoint-max($size);

  @if $min == null and $max != null {
    // Phone
    @media (max-width: $max) {
      @content;
    }
  } @else if $min != null and $max != null {
    // Tablet
    @media (min-width: $min) and (max-width: $max) {
      @content;
    }
  } @else if $min != null and $max == null {
    // Desktop
    @media (min-width: $min) {
      @content;
    }
  } @else {
    // Fallback - no breakpoints defined
    @content;
  }
}

@mixin mdc-layout-grid-cell-span_($size, $span, $gutter) {
  @if not map-has-key($ray-layout-grid-columns, $size) {
    @error "Invalid style specified! Choose one of #{map-keys($ray-layout-grid-columns)}";
  }

  $percent: percentage($span / map-get($ray-layout-grid-columns, $size));

  @if $percent > 100% {
    $percent: 100%;
  }

  width: calc(#{$percent} - #{$gutter});
  width: calc(#{$percent} - var(--ray-layout-grid-gutter-#{$size}, #{$gutter}));

  margin: 0 $gutter / 2;

  margin-left: calc(var(--ray-layout-grid-gutter-#{$size}, #{$gutter}) / 2);
  margin-right: calc(var(--ray-layout-grid-gutter-#{$size}, #{$gutter}) / 2);
}

@mixin mdc-layout-grid-cell-push_($size, $span, $gutter) {
  @if not map-has-key($ray-layout-grid-columns, $size) {
    @error "Invalid style specified! Choose one of #{map-keys($ray-layout-grid-columns)}";
  }

  $percent: percentage($span / map-get($ray-layout-grid-columns, $size));

  @if $percent > 100% {
    $percent: 100%;
  }

  $margin: $gutter / 2;

  margin-left: calc(#{$margin} + #{$percent}) !important;
}

// Public mixins, meant for developer usage.
@mixin mdc-layout-grid($size, $margin, $max-width: null) {
  @if not map-has-key($ray-layout-grid-columns, $size) {
    @error "Invalid style specified! Choose one of #{map-keys($ray-layout-grid-columns)}";
  }

  box-sizing: border-box;

  margin: 0 auto;
  padding: 0 $margin;

  padding-left: var(--ray-layout-grid-margin-#{$size}, #{$margin});
  padding-right: var(--ray-layout-grid-margin-#{$size}, #{$margin});

  width: 100%;

  @if $max-width {
    max-width: $max-width;
  }
}

@mixin mdc-layout-grid-inner($size, $margin, $gutter) {
  @if not map-has-key($ray-layout-grid-columns, $size) {
    @error "Invalid style specified! Choose one of #{map-keys($ray-layout-grid-columns)}";
  }

  display: flex;
  flex-flow: row wrap;
  align-items: stretch;

  margin: 0 (-$gutter / 2);

  margin-left: calc(
    var(--ray-layout-grid-gutter-#{$size}, #{$gutter}) / 2 * -1
  );
  margin-right: calc(
    var(--ray-layout-grid-gutter-#{$size}, #{$gutter}) / 2 * -1
  );
}

@mixin mdc-layout-grid-cell($size, $default-span, $gutter) {
  @if not map-has-key($ray-layout-grid-columns, $size) {
    @error "Invalid style specified! Choose one of #{map-keys($ray-layout-grid-columns)}";
  }

  @include mdc-layout-grid-cell-span_($size, $default-span, $gutter);

  box-sizing: border-box;
}

@mixin mdc-layout-grid-cell-order($order) {
  order: $order;
}

@mixin mdc-layout-grid-cell-align($position) {
  @if $position == 'top' {
    align-self: flex-start;
  }

  @if $position == 'middle' {
    align-self: center;
  }

  @if $position == 'bottom' {
    align-self: flex-end;
  }

  @if $position == 'stretch' {
    align-self: stretch;
  }
}
