@import "../../../sass/webpack_deps";

$square-size: 220px;
$gutter: 30px;
$fluid-max: 427px;
$fluid-min: ($fluid-max + 1);
$desktop-size: 1170px;
$max-card-width: 1395px; // When all 9 cards are up

// -----------------------------------------------------------------------------
// Helper functions
// -----------------------------------------------------------------------------

// column size should be half of the blueprint-size
// 2 and 4 cards should show up in a single row
@function calculate-columns($blueprint-size, $card-count) {
  @if $card-count == 2 or $card-count == 4 {
    @return $card-count;
  } @else {
    @return $blueprint-size / 2;
  }
}

// figure out odd or even based on card numbers .. (1 to 9)
@function determine-parity($card-count) {
  @if $card-count % 2 == 0 {
    @return "even";
  } @else {
    @return "odd";
  }
}

// calculate first cards size depending on odd/even
@function calculate-first-square-size($parity) {
  @if $parity == "odd" {
    @return $square-size * 2;
  } @else {
    @return $square-size;
  }
}

// calculates the size of the grid
// odd numbered grid size equals card numbers plus 3
// (big card == 4x small card)
@function calculate-blueprint-size($parity, $card-count) {
  @if $parity == "odd" {
    @return $card-count + 3;
  } @else {
    @return $card-count;
  }
}

// -----------------------------------------------------------------------------
// Helper mixins
// -----------------------------------------------------------------------------

// outputs a media query dynamicaly
// for each whole quotient of blueprint
@mixin generate-dynamic-media-queries($square-size, $blueprint-size, $fluid-max, $card-count) {
  $factor: $blueprint-size;
  $i: $blueprint-size - 1;

  @while $i > 1 {
    @if round($blueprint-size / $i) == $blueprint-size / $i {
      $mq-max-width: ($factor * $square-size) + 60px + 15px;

      @media screen and (min-width: $fluid-max) and (max-width: $mq-max-width) {
        $factor: $i;

        .interests__cards.interests__cards--#{$card-count} {
          width: $i * $square-size;
        }
      }
    }

    $i: $i - 1;
  }
}

// outputs distinct styles for all card number permutations 1-9
@mixin generate-style-permutations($card-count, $column-count, $square-size, $first-square-dimension, $g, $parity, $fluid-max) {
  .interests__cards--#{$card-count} {
    width: ($column-count * $square-size);

    .interests__card {
      &:first-child {
        height: $first-square-dimension;
        width: $first-square-dimension;

        .interests__card--inner {
          height: ($first-square-dimension - $g);
          width: ($first-square-dimension - $g);

          @if $parity == "odd" {
            .interests__card__link {
              bottom: $gutter;
              font-size: 3.6rem;
              left: $gutter;
              line-height: 1.5;
              right: $gutter;
              text-align: left;
              text-transform: capitalize;
            }
          }
        }
      }
    }

    @media screen and (max-width: $fluid-max) {
      .interests__card {
        height: auto;
        margin-bottom: .2rem;
        width: 50%;

        &:first-child {
          @if $parity == "odd" {
            height: auto;
            width: 100%;

            .interests__card--inner {
              height: 100%;
              width: 100%;
            }
          } @else {
            height: auto;
            width: 50%;

            .interests__card--inner {
              height: 100%;
              margin-right: .1rem;
              width: calc(100% - 1px);
            }
          }
        }

        .interests__card--inner {
          height: 100%;
          padding-bottom: 100%;
          width: 100%;
        }

        &:nth-child(#{ if($parity == "odd", "even", "odd")}) {
          .interests__card--inner {
            margin-right: .1rem;
            width: calc(100% - 1px);
          }
        }
      }
    }
  }
}

// -----------------------------------------------------------------------------
// BEM component styles
// -----------------------------------------------------------------------------

.interests {
  margin-bottom: $gutter;

  &__heading {
    @include h1();
  }
}

.interests__cards {
  margin: 0 auto;
  overflow: hidden;

  &--9 {
    @media (min-width: $max-card-width) {
      margin-left: -1.5rem;
    }
  }

  .interests__card {
    float: left;
    height: $square-size;
    overflow: hidden;
    width: $square-size;

    @media (min-width: $fluid-min) {
      transition: transform $animation-speed;

      &:hover {
        transform: translateY(-.5rem);
      }
    }

    .interests__card--inner {
      background-color: $feature-copy;
      background-position: 0;
      background-size: cover;
      height: $square-size - $gutter;
      position: relative;
      width: $square-size - $gutter;

      &::before {
        background: linear-gradient(to top, rgba($color-black, .4) 0%, rgba($color-black, 0) 32%);
        bottom: 0;
        content: "";
        display: block;
        left: 0;
        position: absolute;
        right: 0;
        top: 0;
      }

      .interests__card__bottom {
        border-top: .1rem solid rgba($color-white, .6);
        bottom: 1rem;
        display: table;
        height: 4rem;
        left: 0;
        margin: 0 auto;
        padding-top: 1rem;
        position: absolute;
        right: 0;
        text-align: center;
        width: 90%;
        z-index: z("default");
      }

      .interests__card__link {
        color: $color-white;
        display: table-cell;
        font-size: 1.2rem;
        font-weight: 600;
        line-height: (15 / 12);
        text-align: center;
        text-transform: uppercase;
        vertical-align: middle;
      }
    }
  }
}

// main loop to control dynamic style creation
@for $card-count from 1 through 9 {
  $parity: determine-parity($card-count);
  $first-square-dimension: calculate-first-square-size($parity);
  $blueprint-size: calculate-blueprint-size($parity, $card-count);
  $column-count: calculate-columns($blueprint-size, $card-count);

  @include generate-style-permutations(
    $card-count,
    $column-count,
    $square-size,
    $first-square-dimension,
    $gutter,
    $parity,
    $fluid-max
  );

  @include generate-dynamic-media-queries(
    $square-size,
    $blueprint-size,
    $fluid-max,
    $card-count
  );
}

// -----------------------------------------------------------------------------
// Fluid styles
// -----------------------------------------------------------------------------

@media screen and (max-width: $fluid-max) {
  .interests .interests__cards {
    font-size: 0;
    margin: 0 auto;
    text-align: center;
    width: calc(100% - 2px);

    .interests__card {
      display: inline-block;
      float: none;
    }
  }
}

@media (min-width: $fluid-min) {
  .interests__card--inner {
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
}
