// @import 'utils';

// ** Break Points *************************************************************
$small-phone-width:  320px !default;
$small-phone-height: 586px !default;
$phone-width:        360px !default;
$phone-height:       640px !default;
$high-phone-width:   414px !default;
$high-phone-height:  896px !default;

$tablet-width:        768px !default;
$tablet-height:      1024px !default;
$high-tablet-width:  1024px !default;
$high-tablet-height: 1366px !default;

$laptop-width:      1280px !default;
$laptop-height:      720px !default;
$high-laptop-width: 1600px !default;
$high-laptop-height: 900px !default;

$desktop-width:       1920px !default;
$desktop-height:      1080px !default;
$high-desktop-width:  3840px !default;
$high-desktop-height: 2160px !default;

$breakpoints: (        // px
  small-phone:  $small-phone-width,
  phone:        $phone-width,
  high-phone:   $high-phone-width,
  tablet:       $tablet-width,
  high-tablet:  $high-tablet-width,
  laptop:       $laptop-width,
  high-laptop:  $high-laptop-width,
  desktop:      $desktop-width,
  high-desktop: $high-desktop-width
) !default;

$breakpoints-height: ( // px
  small-phone:  $small-phone-height,
  phone:        $phone-height,
  high-phone:   $high-phone-height,
  tablet:       $tablet-height,
  high-tablet:  $high-tablet-height,
  laptop:       $laptop-height,
  high-laptop:  $high-laptop-height,
  desktop:      $desktop-height,
  high-desktop: $high-desktop-height
) !default;

$screen-sizes: (       // inch
  small-phone:  4,
  phone:        4.7,
  high-phone:   5.8,
  tablet:       9.7,
  high-tablet:  12.9,
  laptop:       13,
  high-laptop:  14,
  desktop:      24,
  high-desktop: 32
) !default;

$screen-distances: (   // 10cm
  small-phone:  3,
  phone:        3,
  high-phone:   3,
  tablet:       4,
  high-tablet:  4,
  laptop:       5,
  high-laptop:  5,
  desktop:      6,
  high-desktop: 6
) !default;

// Alias
$small-phone:  $small-phone-width   !default;
$phone:        $phone-width        !default;
$high-phone:   $high-phone-width   !default;
$tablet:       $tablet-width       !default;
$high-tablet:  $high-tablet-width  !default;
$laptop:       $laptop-width       !default;
$high-laptop:  $high-laptop-width  !default;
$desktop:      $desktop-width      !default;
$high-desktop: $high-desktop-width !default;

// == Validation ===============================================================
$CACHED-BREAKPOINTS:        $breakpoints        !default;
$CACHED-BREAKPOINTS-HEIGHT: $breakpoints-height !default;
$CACHED-SCREEN-SIZES:       $screen-sizes       !default;
$CACHED-SCREEN-DISTANCES:   $screen-distances   !default;

@function check-breakpoints($criteria-name, $others-name, $criteria-keys, $others-keys) {
  $validate-breakpoints: true;

  $criteria-length: length($criteria-keys);
  $others-length:   length($others-keys);
  @each $i in range($others-length) {
    $other-name:   nth($others-name, $i);
    $other-keys:   nth($others-keys, $i);
    $other-length: length($other-keys);

    // Length Check
    $validate-breakpoints: $validate-breakpoints and $criteria-length == $other-length;
    @if not $validate-breakpoints {
      @error "#{$criteria-name}'s length doesn't same #{$other-name}'s length";
    }

    // Key Check
    $validate-breakpoints: $validate-breakpoints and $criteria-keys == $other-keys;
    @if not $validate-breakpoints {
      @error "#{$criteria-name}'s keys doesn't same #{$other-name}'s keys";
    }
  }
  @return $validate-breakpoints;
}

// -- Interface --
@function validate-breakpoints() {
  $validate-breakpoints: true;

  // Check Cache
  @if not ($CACHED-BREAKPOINTS  == $breakpoints  and $CACHED-BREAKPOINTS-HEIGHT == $breakpoints-height and
           $CACHED-SCREEN-SIZES == $screen-sizes and $CACHED-SCREEN-DISTANCES   == $screen-distances) {
    $break-keys: map($breakpoints $breakpoints-height $screen-sizes $screen-distances,
                     map-keys list-sort);
    $breakpoints-keys:        nth($break-keys, 1);
    $breakpoints-height-keys: nth($break-keys, 2);
    $screen-sizes-keys:       nth($break-keys, 3);
    $screen-distances-keys:   nth($break-keys, 4);

    // Check Validate
    $others-name: "$breakpoints-height"    "$screen-sizes"    "$screen-distances";
    $others-keys: $breakpoints-height-keys $screen-sizes-keys $screen-distances-keys;
    $validate-breakpoints: check-breakpoints("$breakpoints", $others-name, $breakpoints-keys, $others-keys);

    // Cache Update
    $CACHED-BREAKPOINTS:        $breakpoints        !global;
    $CACHED-BREAKPOINTS-HEIGHT: $breakpoints-height !global;
    $CACHED-SCREEN-SIZES:       $screen-sizes       !global;
    $CACHED-SCREEN-DISTANCES:   $screen-distances   !global;
  }

  @return $validate-breakpoints;
}
