////////////////////////////////////////////////////////////////////////////////
//
//  Licensed to the Apache Software Foundation (ASF) under one or more
//  contributor license agreements.  See the NOTICE file distributed with
//  this work for additional information regarding copyright ownership.
//  The ASF licenses this file to You under the Apache License, Version 2.0
//  (the "License"); you may not use this file except in compliance with
//  the License.  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

// Jewel Layouts

// Layouts variables
$gaps: 10
$gap-step: 3
$gap: 0px

$grid-columns: 12 !default
$gap-size: 10px !default

.viewport
	overflow: hidden
	
	&.scroll
		overflow: auto

	&.visible
		overflow: visible

// Basic
.layout.basic
	position: relative
	> *
		position: absolute

.layout
	display: flex
	//position: relative

	// Horizontal
	&.horizontal
		flex-direction: row
		flex-wrap: nowrap
		align-items: flex-start
	
		> *
			flex: 0 0 auto

		&.flow
			flex-wrap: wrap
			max-width: 100%

		&.centered
			justify-content: center
			align-items: center
			align-content: center
			height: 100%
			> *
				flex: 0 0 auto

		// FormItemLayout
		&.formitem
			width: 100%
			// flex-wrap: wrap
			align-items: flex-start
			// justify-content: center

		@for $i from 1 through $gaps
			&.gap-#{$i}x#{$gap-step}px
				> *:first-child
					margin: 0px
				> *
					margin: 0px 0px 0px ($gap + $i * $gap-step)

	// Vertical
	&.vertical
		flex-direction: column
		flex-wrap: nowrap
		align-items: flex-start

		> *
			flex: 0 0 auto
		
		&.flow
			flex-wrap: wrap

		&.centered
			justify-content: center
			align-items: center
			align-content: center
			flex-direction: column
			height: 100%

		// FormLayout (consider for now Forms are always vertical)
		&.form
			width: 100%
		
		// FormItemLayout
		// &.formitem
		// 	width: 100%

		@for $i from 1 through $gaps
			&.gap-#{$i}x#{$gap-step}px
				> *:first-child
					margin: 0px
				> *
					margin: ($gap + $i * $gap-step) 0px 0px

	// Tile	
	&.tile
		flex-flow: row wrap
		align-items: flex-start
	
	// this not compile******
	// &.gap
	// 	> *:first-child
	// 		margin: 0px
	// 	> *:nth-child(-n+3)
	// 		margin: 0px 0px 0px $gap-size
	// 	> *:nth-child(3n)
	// 		margin: $gap-size 0px 0px
	// 	> *
	// 		margin: $gap-size 0px 0px $gap-size

	// Grid
	&.grid
		flex-flow: row wrap
		width: 100%
		// height: 100%
		
		> *
			flex: 1 1 auto // flex-grow, flex-shrink and flex-basis
			max-width: 100%
			width: 100%
			
		
		@each $size in $size-names
			@media (min-width: map-get($sizes, $size))
				@for $i from 1 through $grid-columns
					@for $j from 1 through $i
						.#{$size}-col-#{$j}-#{$i}
							width: percentage($j / $i)
		&.gap	
			> *
				margin: $gap-size / 2
			@each $size in $size-names
				@media (min-width: map-get($sizes, $size))
					@for $i from 1 through $grid-columns
						@for $j from 1 through $i
							.#{$size}-col-#{$j}-#{$i}
								width: calc(#{percentage($j / $i)} - #{$gap-size})

	&.horizontal, &.vertical, &.grid
		// Vertical Items Align
		&.itemsSameHeight
			//align-items: stretch  --> this doen't seems to work as expected
			> *
				> *
					min-height: 100%
		&.itemsCentered
			align-items: center
		&.itemsTop
			align-items: flex-start
		&.itemsBottom
			align-items: flex-end

		// Horizontal Items Align
		&.itemsLeft
			justify-content: flex-start
		&.itemsCenter
			justify-content: center
		&.itemsRight
			justify-content: flex-end
		&.itemsSpaceBetween
			justify-content: space-between
		&.itemsSpaceAround
			justify-content: space-around
			
		&.itemsExpand
			width: 100%
			> *
				flex: 1 0 auto

	&.table
		display: table

@each $size in $size-names			
	@if index($size-names, $size) == 1
		@media (max-width: nth(nth($sizes, 2), 2) - 1)
			+visible-hidden-responsive-rules($size)
	@else if index($size-names, $size) != 1 and index($size-names, $size) < length($sizes)
		@media (min-width: map-get($sizes, $size)) and (max-width: nth(nth($sizes, index($size-names, $size) + 1), 2) - 1)
			+visible-hidden-responsive-rules($size)
	@else if index($size-names, $size) == length($sizes)
		@media (min-width: nth(nth($sizes, length($sizes)), 2))
			+visible-hidden-responsive-rules($size)

#{$visible-sizes}
	display: none !important

#{$hidden-sizes}
	display: flex !important

// #{$hidden-flex-sizes}
// 	display: flex !important

