@import "./grid";

%ui-flex {
  display: -ms-flexbox;
  display: -moz-box;
  display: flex;
  flex-flow: row wrap;

  &--inline {
    display: -ms-inline-flexbox;
    display: -moz-inline-box;
    display: inline-flex;
  }

  &--vertical {
    flex-flow: column wrap;
  }
}

.ui-flex {
  @extend %ui-flex;
  align-items: stretch;
  justify-content: stretch;

  &--inline {
    @extend %ui-flex--inline;
  }

  &--vertical {
    @extend %ui-flex--vertical;
  }
}

.ui-container {
  --gutter: #{$gutter};
  @include clearfix();
  display: block;

  &--fluid {
    max-width: none;
    padding: var(--gutter);
  }

  &:not(&--fluid) {
    margin-left: auto;
    margin-right: auto;
  }
}

.ui-row {
  @extend %ui-flex;
  margin: calc(-0.5 * var(--gutter));

  &+& {
    margin-top: calc(0.5 * var(--gutter));
  }

  &--reverse {
    flex-direction: row-reverse;
  }

  &--vertical {
    @extend %ui-flex--vertical;

    &--reverse {
      flex-direction: column-reverse;
    }
  }

  &--auto>.ui-col {
    @extend .ui-col--auto;
  }

  &--nowrap {
    flex-wrap: nowrap;
  }

  // valign
  &--stretch {
    align-items: stretch;
  }

  &--top {
    align-items: flex-start;
  }

  &--bottom {
    align-items: flex-end;
  }

  &--middle {
    align-items: center;
  }

  // halign
  &--start {
    justify-content: flex-start;
  }

  &--end {
    justify-content: flex-end;
  }

  &--center {
    justify-content: center;
  }

  &--spaced {
    justify-content: space-between;
  }

  &--even {
    justify-content: space-evenly;
  }

  &--around {
    justify-content: space-around;
  }
}

.ui-col {
  flex: 0 0 calc(100% - var(--gutter));
  margin: calc(0.5 * var(--gutter));

  &--auto {
    flex: 0 0 auto;
  }

  &--fill {
    flex: 1 1 1rem;
  }

  &--auto-fill {
    flex: 1 0 auto;
  }

  &--stretch {
    align-self: stretch;
  }

  &--top {
    align-self: flex-start;
  }

  &--bottom {
    align-self: flex-end;
  }

  &--middle {
    align-self: center;
  }

  &.content-stretch> :first-child {
    height: calc(100%);
  }

  @for $i from 1 through 12 {
    &--#{$i} {
      flex: 0 0 calc(#{($i/12) * 100%} - var(--gutter));
    }
  }
}

.ui-gutter {
  --gutter: #{$gutter};

  &--0 {
    --gutter: #{$gutter * 0};
  }

  &--xs {
    --gutter: #{$gutter * 0.25};
  }

  &--sm {
    --gutter: #{$gutter * 0.5};
  }

  &--md {
    --gutter: #{$gutter * 2};
  }

  &--lg {
    --gutter: #{$gutter * 3};
  }
}

@each $prefix,
$size in $breakpoints {
  @media only screen and (min-width: $size) {
    .ui-container:not(.ui-container--fluid) {
      max-width: if($prefix==xs, 100%, $size - 32px);
    }

    @for $i from 1 through 12 {
      .ui-col--#{$i}\@#{$prefix} {
        flex: 0 0 calc(#{($i/12) * 100%} - var(--gutter));
      }
    }

    .ui-col--auto\@#{$prefix} {
      flex: 0 0 auto;
    }

    .ui-col--fill\@#{$prefix} {
      flex: 1 1 1rem;
    }

    .ui-col--stretch\@#{$prefix} {
      align-self: stretch;
    }

    .ui-col--top\@#{$prefix} {
      align-self: flex-start;
    }

    .ui-col--bottom\@#{$prefix} {
      align-self: flex-end;
    }

    .ui-col--middle\@#{$prefix} {
      align-self: center;
    }

    .ui-hide--up\@#{$prefix} {
      display: none;
    }

    .ui-show--down\@#{$prefix} {
      display: none;
    }
  }

  @media only screen and (max-width: $size) {
    .ui-hide--down\@#{$prefix} {
      display: none;
    }

    .ui-show--up\@#{$prefix} {
      display: none;
    }
  }
}

.col .row {
  margin-left: -0.75rem;
  margin-right: -0.75rem;
}

.row {
  &:after {
    content: "";
    display: table;
    clear: both;
  }

  .col {
    float: left;
    box-sizing: border-box;
    padding: 0 0.75rem;
    min-height: 1px;
    margin-left: auto;
    left: auto;
    right: auto;

    @for $indCol from 1 through 12 {
      &.s#{$indCol} {
        width: calc(100% / 12 * #{$indCol});
      }
    }

    @for $indCol from 1 through 12 {
      $perc: unquote((100 / (12 / $indCol)) + "%");
      @include grid-classes("s", $indCol, $perc);
      @include grid-classes("m", $indCol, $perc);
      @include grid-classes("l", $indCol, $perc);
      @include grid-classes("xl", $indCol, $perc);
    }
  }
}

@media only screen and (min-width: $breakpoints-md) {
  .row {
    .col {
      @for $indCol from 1 through 12 {
        &.m#{$indCol} {
          width: calc(100% / 12 * #{$indCol});
        }
      }
    }
  }
}

@media only screen and (min-width: $breakpoints-lg) {
  .row {
    .col {
      @for $indCol from 1 through 12 {
        &.l#{$indCol} {
          width: calc(100% / 12 * #{$indCol});
        }
      }
    }
  }
}

@media only screen and (min-width: $breakpoints-xl) {
  .row {
    .col {
      @for $indCol from 1 through 12 {
        &.xl#{$indCol} {
          width: calc(100% / 12 * #{$indCol});
        }
      }
    }
  }
}

.hide-on-small-only,
.hide-on-small-and-down {
  @media only screen and (max-width : $breakpoints-sm) {
    display: none !important;
  }
}

.hide-on-med-and-down {
  @media only screen and (max-width : $breakpoints-md) {
    display: none !important;
  }
}

.hide-on-med-and-up {
  @media only screen and (min-width : $breakpoints-md) {
    display: none !important;
  }
}

.hide-on-med-only {
  @media only screen and (min-width: $breakpoints-sm) and (max-width: $breakpoints-md) {
    display: none !important;
  }
}

.hide-on-large-only {
  @media only screen and (min-width :$breakpoints-lg) {
    display: none !important;
  }
}

.hide-on-extra-large-only {
  @media only screen and (min-width : $breakpoints-xl) {
    display: none !important;
  }
}

.show-on-extra-large {
  @media only screen and (min-width : $breakpoints-xl) {
    display: block !important;
  }
}

.show-on-large {
  @media only screen and (min-width : $breakpoints-lg) {
    display: block !important;
  }
}

.show-on-medium {
  @media only screen and (min-width: $breakpoints-sm) and (max-width: $breakpoints-md) {
    display: block !important;
  }
}

.show-on-small {
  @media only screen and (max-width : $breakpoints-sm) {
    display: block !important;
  }
}

.show-on-medium-and-up {
  @media only screen and (min-width : $breakpoints-md) {
    display: block !important;
  }
}

.show-on-medium-and-down {
  @media only screen and (max-width : $breakpoints-md) {
    display: block !important;
  }
}

.center-on-small-only {
  @media only screen and (max-width : $breakpoints-sm) {
    text-align: center;
  }
}