/*
 *  Usage:
 *
      <div class="sk-folding-cube">
        <div class="sk-cube1 sk-cube"></div>
        <div class="sk-cube2 sk-cube"></div>
        <div class="sk-cube4 sk-cube"></div>
        <div class="sk-cube3 sk-cube"></div>
      </div>
 *
 */
@import "../variables";

$spinkit-folding-cube-child-count: 4 !default;
$spinkit-folding-cube-width: $spinkit-size;
$spinkit-folding-cube-height: $spinkit-size;
$spinkit-folding-cube-margin: $spinkit-spinner-margin;
$spinkit-folding-cube-background-color: $spinkit-spinner-color;
$spinkit-folding-cube-animation-duration: 2.4s !default;
$spinkit-folding-cube-animation-delay-range: 1.2s !default;
$spinkit-folding-cube-css-export: true !default;

@keyframes sk-foldCubeAngle {
  0%, 10% {
    transform: perspective(140px) rotateX(-180deg);
    opacity: 0;
  } 25%, 75% {
    transform: perspective(140px) rotateX(0deg);
    opacity: 1;
  } 90%, 100% {
    transform: perspective(140px) rotateY(180deg);
    opacity: 0;
  }
}

@mixin sk-folding-cube-size(
  $width: $spinkit-folding-cube-width,
  $height: $spinkit-folding-cube-height
) {
  width: $width;
  height: $height;
}

@mixin sk-folding-cube-color(
  $color: $spinkit-folding-cube-background-color
) {
  .sk-cube:before {
    background-color: $color;
  }
}

@mixin sk-folding-cube-style(
  $width: $spinkit-folding-cube-width,
  $height: $spinkit-folding-cube-height,
  $color: $spinkit-folding-cube-background-color,
  $margin: $spinkit-folding-cube-margin
) {
  margin: $margin;

  @include sk-folding-cube-size(
    $width: $width,
    $height: $height
  );

  @include sk-folding-cube-color(
    $color: $color
  );
}

@mixin sk-folding-cube(
  $width: $spinkit-folding-cube-width,
  $height: $spinkit-folding-cube-height,
  $color: $spinkit-folding-cube-background-color,
  $margin: $spinkit-folding-cube-margin,
  $duration: $spinkit-folding-cube-animation-duration,
  $delay-range: $spinkit-folding-cube-animation-delay-range,
  $child-count: $spinkit-folding-cube-child-count
) {
  position: relative;
  transform: rotateZ(45deg);

  .sk-cube {
    float: left;
    width: 50%;
    height: 50%;
    position: relative;
    transform: scale(1.1);
  }

  .sk-cube:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    animation: sk-foldCubeAngle $duration infinite linear both;
    transform-origin: 100% 100%;
  }

  // Rotation / angle
  @for $i from 2 through $child-count {
    .sk-cube#{$i} {
      transform: scale(1.1) rotateZ(90deg * ($i - 1));
    }
  }

  @for $i from 2 through $child-count {
    .sk-cube#{$i}:before {
      animation-delay: $delay-range / $child-count * ($i - 1);
    }
  }

  @include sk-folding-cube-style(
    $width: $width,
    $height: $height,
    $color: $color,
    $margin: $margin
  );
}

@if ($spinkit-folding-cube-css-export) {
  .sk-folding-cube {
    @include sk-folding-cube;
  }
}
