/**
 * Typography mixin description
 *
 * @typography-base-size - this is the minimal size that any element cannot go below
 * @typography-base-line-height - this is the minimal line-height that any element cannot go below
 * @typography-size-difference - the difference between each size
 * @typography-max-size - the number of headers you want to generate
 *
 * .typography-library - generates rules and classes for each size
 * .typography-rule - generates rule based on the given value inserted
 * .typography-family - sets family based on the given weight
 *
 */

.typography-library(@n, @i: 1) when (@i =< @n) {
    h@{i},
    .h@{i} {
        font-size: ~"calc(@{typography-base-size} + (((@{n} + 1) - @{i}) * @{typography-size-difference}))";
        line-height: @typography-base-line-height;
        margin: 0;
        .typography-family(~"@{font-weight-h@{i}}");
        .generate-spacing-rule(@n - @i, padding, bottom);
    }

    & when (@i = 1) {
        .small {
            font-size: @typography-base-size;
            line-height: @typography-base-line-height;
            .typography-family(@family-weight-regular);
        }

        p,
        .paragraph {
            font-size: ~"calc(@{typography-base-size} + (@{i} * @{typography-size-difference}))";
            line-height: @typography-base-line-height;
            .typography-family(@family-weight-regular);
            .generate-spacing-rule(@i, padding, bottom);
        }
    }

    .typography-library(@n, (@i + 1));
}



.typography-rule(@n, @w: @family-weight-regular, @p: false, @f: @typography-base-size, @l: @typography-base-line-height) {
    & when (@n = inherit) {
        .typography-font(@n,false);
        .typography-lineheight(@n);
        .typography-family(@n);
    }

    & when (@n = none) {
        .typography-font(0,false);
        .typography-lineheight(0);
        .typography-family(empty);
    }

    & when not (@n = none) {
        & when not (@n = inherit) {
            & when not (@n = normal) {
                .typography-font(@f);
                .typography-lineheight(@l);
                .typography-family(@w);
                &when (@p = true) {
                    .generate-spacing-rule(@n, padding, bottom);
                }
            }
        }
    }
}



.typography-family(@w) {
    & when (@w = light) {
        font-weight: @family-weight-light;
    }

    & when (@w = regular) {
        font-weight: @family-weight-regular;
    }

    & when (@w = bold) {
        font-weight: @family-weight-bold;
    }

    & when not (@w = light) {
        & when not (@w = regular) {
            & when not (@w = bold) {
                font-weight: @w;
            }
        }
    }
}

.typography-font(@f,@c: true) {
    & when (@c = true) {
        font-size: ~"calc(@{f} + (@{n} * @{typography-size-difference}))";
    }

    & when not (@c = true) {
        font-size: @f;
    }
}

.typography-lineheight(@l) {
    line-height: @l;
}

//Directional gradient
.background-gradient (@position, @from, @to, @percent_start,@percent_end) {
    background: @from;
    background: linear-gradient(@position, @from @percent_start, @to @percent_end);
}

//Radial gradient
.radial-gradient (@position,@shape,@from,@to,@percent_start,@percent_end) {
    background: @from;
    background: radial-gradient(@position, @shape, @from @percent_start, @to @percent_end);
}

//Horizontal gradient
.horizontal-gradient (@from, @to, @begin: 0%, @end: 100%) {
    background: @from;
    background: -moz-linear-gradient(left,  @from @begin, @to @end);
    background-image: -webkit-linear-gradient(left,  @from @begin,@to @end);
    background: linear-gradient(to right,  @from @begin,@to @end);

}


//Popup width
.popup_width(@size,@space_between) {
    & when(@size = "medium") {
        width: 320px * 2 + @space_between + 30;
    }
    & when(@size = "large") {
        width: 320px * 3 + @space_between * 2 + 30;
    }
}

//Display flex
.display_flex {
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    flex-flow: row wrap;
}

//Media
.respond-to(@breakpoint,@content) {
    & when(@breakpoint = "small") {
        @media only screen and (max-width: 480px) {
            @content();
        }
    }

    & when(@breakpoint = "small-min") {
        @media only screen and (min-width: 481px) {
            @content();
        }
    }

    & when(@breakpoint = "small-large") {
        @media only screen and (max-width: 680px) {
            @content();
        }
    }

    & when(@breakpoint = "small-large-min") {
        @media only screen and (min-width: 681px) {
            @content();
        }
    }

    & when(@breakpoint = "medium") {
        @media only screen and (max-width: 771px) {
            @content();
        }
    }

    & when(@breakpoint = "medium-min") {
        @media only screen and (min-width: 772px) {
            @content();
        }
    }

    & when(@breakpoint = "large") {
        @media only screen and (max-width: 991px) {
            @content();
        }
    }

    & when(@breakpoint = "large-min") {
        @media only screen and (min-width: 992px) {
            @content();
        }
    }

    & when(@breakpoint = "extra-large") {
        @media only screen and (max-width: 1023px) {
            @content();
        }
    }

     & when(@breakpoint = "extra-large-min") {
        @media only screen and (min-width: 1024px) {
            @content();
        }
    }
}

//Multiplier
.multiplier(@size,@n) {
    width: @size*@n;
    height: @size*@n;
}

//ClearFix
.clear_fix {
    &:after {
        content: "";
        display: table;
        clear: both;
    }
}

//Capitalize
.capitalize {
    text-transform: lowercase;
    &:first-letter {
        text-transform: uppercase;
        justify-content: center;
    }
}

//Placeholder
.placeholder(@color) {
    &::-webkit-input-placeholder {
        color: @color;
    }
    &::-moz-placeholder {
        color: @color;
        opacity: 1;
    }
    &:-ms-input-placeholder {
        color: @color;
    }
}

.generate-border-rule(@directions: all, @width: 1px, @color: @color-gray-9) {
    @length: length(@directions);

    & when (@length = 1) {
        & when (@directions = all) {
            border: @width solid @color;
        }

        & when not (@directions = all) {
            border-@{directions}: @width solid @color;
        }
    }

    & when (@length > 1) {
        .loop(@d) when (@d =< @length) {
            @direction: extract(@directions,@d);
            & when not (@d = all) {
                border-@{d}: @width solid @color;

                .loop(@d + 1);
            }

            .loop(1);
        }
    }
}



.generate-border-library(@n, @i: 1) when (@i =< @n) {
    .border-@{i} {
        border: unit((@i),px) solid @color-gray-9;
    }

    .border-top-@{i} {
        border-top: unit((@i),px) solid @color-gray-9;
    }

    .border-bottom-@{i} {
        border-bottom: unit((@i),px) solid @color-gray-9;
    }

    .border-left-@{i} {
        border-left: unit((@i),px) solid @color-gray-9;
    }

    .border-right-@{i} {
        border-right: unit((@i),px) solid @color-gray-9;
    }

    .generate-border-library(@n, @i + 1);
}

//@todo To research the loops and make it in to one mixin
//@todo Change names of variables


.generate-spacing-library(@n, @i: 0) when (@i =< @n) {
    .padding-@{i} {
        padding: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    };

    .padding-top-@{i} {
        padding-top: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    }

    .padding-right-@{i} {
        padding-right: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    }

    .padding-bottom-@{i} {
        padding-bottom: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    }

    .padding-left-@{i} {
        padding-left: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    }

    .padding-center-@{i} {
        padding: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))" 0;
    }

    .padding-vcenter-@{i} {
        padding: 0 ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    }

    .margin-@{i} {
        margin: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    };

    .margin-top-@{i} {
        margin-top: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    }

    .margin-right-@{i} {
        margin-right: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    }

    .margin-bottom-@{i} {
        margin-bottom: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    }

    .margin-left-@{i} {
        margin-left: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    }

	.margin-center-@{i} {
		margin: ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))" auto;
	}

    .margin-vcenter-@{i} {
        margin: 0 ~"calc(@{spacing-min} + (@{i} * @{spacing-step}))";
    }

    .generate-spacing-library(@n, @i + 1);
}



.generate-spacing-rule(@n, @type, @direction, @extra: 0px) {
	& when (@direction = all) {
		@{type}: ~"calc(@{spacing-min} + @{extra} + (@{n} * @{spacing-step}))";
	}

	& when not (@direction = all)  {
		& when not (@direction = center) {
			& when not (@direction = center_top) {
				& when not (@direction = center_bottom) {
					@{type}-@{direction}: ~"calc(@{spacing-min} + @{extra} + (@{n} * @{spacing-step}))";
				}
			}
		}
	}

	& when (@direction = center) {
		@type: margin;
		@{type}: ~"calc(@{spacing-min} + @{extra} + (@{n} * @{spacing-step}))" auto;
	}

	& when (@direction = center_top) {
		@type: margin;
		@{type}: ~"calc(@{spacing-min} + @{extra} + (@{n} * @{spacing-step}))" auto 0 auto;
	}

	& when (@direction = center_bottom) {
		@type: margin;
		@{type}: 0 auto ~"calc(@{spacing-min} + @{extra} + (@{n} * @{spacing-step}))" auto;
	}
}

.generate-gray(@n, @i: 1) when (@i =< @n) {
    @j: @i - 1;

    @gray-percent: 8% + (@j * 10%);

    .gray-@{i} {
        color: lighten(@color-gray-main,@gray-percent) !important;
    }

    .gray-background-@{i} {
        background: lighten(@color-gray-main,@gray-percent) !important;
    }

    .generate-gray(@n, @i + 1);
}

.generate-gray(10);