/*
* Options
* 1. Structure elements 
* 2. Modules display
* 3. Columns
* 4. Auxiliary elements
* 5. Collapse 
* 6. Responsive utilites
* 7. Text utulites
* 8. SCSS utulites
*   8.1 Functions
*   8.1 Mixins
*/

// TODO
/*
+ Add $columns gap in devices for each device,
in all slectors where using $columns_gap use column gap from devices
in default $devices add global $columns_gap
*/
// Problems:

// 1) padding-left:  map-get($device_obj, columns_gap)+px; // Error: Invalid null operation: "null plus "px"".
// = Р.: использовать синтаксис  padding-left:   #{map-get($device_obj, columns_gap)}px;

// 2) In user custom settings /sass/_mwgrid.scss - we erase (destroy) default columns_gap in devices
// ? В.: добавить columns gap к $devices после определения $devices




/* Options: */
$columns: 12 !default;  // columns number
$columns_gap: 15 !default;  // column one side gap
$container_width: 1400 !default;  // container max width
$breakpoints_between: 'responsive' !default; // 'responsive' || 'fixed'. When 'responsive' - 'container_max' don't use.
$vertical_alignment_responsive: 0 !default; // [0||1] vertical alignment classes for each device

$display_models: () !default; // types of CSS display modules
$display_models: map-merge(
    ( 
        block: 1,
        inline-block: 0,
        flex: 1
    ),
    $display_models
);


$devices: () !default;  // Devices - breakpoints, container max width, font base, and modular scale coeff
// This is some variant

// $devices: ( 
//     mv_: (
//         breakpoint: 320,
//         container_max: 300,
//         font_base: 15px,
//         ms_coeff: 1.10,
//     ),
//     mh_: (
//         breakpoint: 576,
//         container_max: 540,
//         font_base: 15px,
//         ms_coeff: 1.10,
//     ),
//     tb_: (
//         breakpoint: 768,
//         container_max: 750,
//         font_base: 15px,
//         ms_coeff: 1.17,
//     ), 
//     ds_: (
//         breakpoint: 1024,
//         container_max: 960,
//         font_base: 15px,
//         ms_coeff: 1.17
//     ),
//     hd_: (
//         breakpoint: 1440,
//         container_max: 1400,
//         font_base: 15px,
//         ms_coeff: 1.17
//     )

// );
// @debug $devices();

$devices: map-merge(
    ( 

        mv_: (
            breakpoint: 320,
            container_max: 300,
            font_base: 14px,
            ms_coeff: 1.10,
        ),
        mh_: (
            breakpoint: 576,
            container_max: 540,
            font_base: 14px,
            ms_coeff: 1.10,
        ),
        tb_: (
            breakpoint: 768,
            container_max: 750,
            font_base: 15px,
            ms_coeff: 1.17,
        ), 
        ds_: (
            breakpoint: 1024,
            container_max: 960,
            font_base: 16px,
            ms_coeff: 1.17
        ),
        hd_: (
            breakpoint: 1366,
            container_max: 1200,
            font_base: 16px,
            ms_coeff: 1.17
        )

    ),
    $devices
);

$devices-names: map-keys($devices);
@debug $devices-names;


// 0.1 Add columns gap with backward compability
@import '_columns_gap'; //

/* 1. Structure elements */

.section{
    float: left; width: 100%; clear: both;
}
.container{    
    max-width: $container_width+px;
    // padding-left: $columns_gap+px;
    // padding-right: $columns_gap+px;
    margin: 0 auto; 
    box-sizing: border-box;    
    position: relative;
}
.container-elastic {
    max-width: none;
}


/* 2. Modules display */

/*
Modules have:
    vertical aligment ( .middle, etc.. )
    reverse order ( .row-reverse )
*/


/* Block  model */
@if map-get($display_models, block) == 1 {
    .row{
        // margin-left: -$columns_gap+px;
        // margin-right: -$columns_gap+px;
    }

    .row>div{ float : left; }

    .row.reverse>div,
    .row.row-reverse>div { float: right;  }


}
/* Inline-block  model */
@if map-get($display_models, inline-block) == 1 {
    .row-line{
        // margin-left: -$columns_gap+px;
        // margin-right: -$columns_gap+px; 
        font-size: 0;
    }

    .row-line>div{ float : none; display : inline-block;}

    .row-line.middle>div{ vertical-align : middle;  }
    .row-line.top>div{ vertical-align : top; }
    .row-line.bottom>div{ vertical-align : bottom; }
    .row-line.baseline>div{ vertical-align : baseline; }

    .row-line.row-reverse,
    .row-line.reverse { direction : rtl; }
    .row-line.row-reverse,
    .row-line.reverse >div{ direction : ltr; }    
}
/* Flex  model */
@if map-get($display_models, flex) == 1 {
    .row-flex{
        display: flex; flex-wrap: wrap;
        // margin-left: -$columns_gap+px;
        // margin-right: -$columns_gap+px;    
    }

    .row-flexbox{ 
        display : flex; 
        // margin-left: -$columns_gap+px; 
        // margin-right: -$columns_gap+px; 
    } 
    [class*=row-flex].middle{ align-items : center; }
    [class*=row-flex].top{ align-items : flex-start; }
    [class*=row-flex].bottom{ align-items : flex-end; }
    [class*=row-flex].baseline{ align-items : baseline; }
    [class*=row-flex].stretch{ align-items : stretch; }

    .row-flex.row-reverse,
    .row-flex.reverse { flex-direction: row-reverse; }

}

@import '_vertical_alignment'; //




/* 3. Columns */

/* 3. Columns v.2 */

// column base styles
%column_{

    box-sizing: border-box; 
    // padding-left: $columns_gap+px;
    // padding-right: $columns_gap+px;
    position : relative; 
    min-height: 1px;
}

// container responsive by devices
// v.2
// @each $device_name, $device_obj in $devices {

//     $device_name_cutted: str-slice($device_name, 1, -2);

//     .container-#{$device_name_cutted} {
//         max-width: get_grid_c_m($device_name)+px;
//     }
// }



@each $device_name, $device_obj in $devices {

    // columns width and offsets
    @media (min-width : #{map-get($device_obj, breakpoint)}px) {

        // container responsive by devices
        // v.1
        .container-#{$device_name} {
            max-width: map-get($device_obj, container_max)+px;
        }
        // @debug string.slice("Helvetica Neue", 1, -6); // "Helvetica" // см.
        // str-slice($string, $start-at, $end-at: -1) //=> string 

        // Updt.: move to -> _columns_gap.scss // columns gap 
        .container{
            // padding-left: get_grid_property($device_name, columns_gap );
            // 
            // padding-left:  map-get($device_obj, columns_gap)+px; // Error: Invalid null operation: "null plus "px"".

            // padding-left:  map-get($device_obj, columns_gap)px; // 20 px

            // padding-left:  map-get($device_obj, columns_gap)+0px; // Error: Invalid null operation: ...

            // padding-left:  map-get($device_obj, 'columns_gap')+px; // Error: Invalid null operation: ...

            // padding-left:   #{map-get($device_obj, columns_gap)}px; // +
            // padding-right:  #{map-get($device_obj, columns_gap)}px; // +

            // padding-right: map-get($device_obj, container_max)+px; // + Почему здесь вывоид, а там нет
            // padding-right: sdf;
        }        

        // between points : Container max-width
        @if $breakpoints_between == 'fixed' {
            .container{
                max-width: map-get($device_obj, container_max)+px;
            }
        }

        // columns offset reset
        .#{$device_name}offset_0 { 
            margin-left:  0;
        }

        // columns width and offset
        $i: 1;
        @while $i < ($columns + 1) {

            .#{$device_name}#{$i} {

                $width: $i / $columns;
                width: $width * 100%;
            }

            .#{$device_name}offset_#{$i} {

                $width: $i / $columns;
                margin-left:  $width * 100%;
            }            

            $i: $i + 1;

        }

        // columns specific order
        $i: 1;
        @while $i < ($columns + 1) {

            .#{$device_name}order_#{$i} {
                order: $i;
            }

            // .#{$device_name}#{$i} {

            //     $width: $i / $columns;
            //     width: $width * 100%;
            // }         

            $i: $i + 1;

        }

        // columns self centered
        .row .#{$device_name}self_centered {
            float: none;
            margin: 0 auto;
        }        
        .row-flex .#{$device_name}self_centered {
            margin: 0 auto;
        }

    }

    // columns base styles
    [class*="#{$device_name}"] {
        @extend %column_;
    }    
}

// .mv_order-1
// .mv_order-2
// ..
// .ds_order-1
// .ds_order-2


/* 4. Auxiliary elements (Вспомогательные структурные элементы) */

.container:before,
.container:after,
.row:before,
.row:after
{
    display: table;
    content: " "; box-sizing: border-box;
}
.container:after, 
.row:after
{
    clear : both;
}


/* 5. Collapse */

%padding_nulls {
    padding-left: 0; padding-right: 0;  
}

.collapse {
    
    padding-left: 0; padding-right: 0;
    & > [class*=row]{
    // & [class*=row]{
        margin-left: 0; margin-right: 0;

        @each $device_name, $device_obj in $devices {
            // columns padding nulls
            & > [class*="#{$device_name}"] {
                @extend  %padding_nulls;
            }         
        }
    }
}

[class*=row].collapse {

    margin-left: 0; margin-right: 0; 
    [class*=row] {
        margin-left: 0; margin-right: 0; 
    }

    @each $device_name, $device_obj in $devices {
        // columns padding nulls
        [class*="#{$device_name}"] {
            @extend  %padding_nulls;
        }         
    }

}

[class*=row].collapse-this {

    margin-left: 0; margin-right: 0; 

    @each $device_name, $device_obj in $devices {
        // columns padding nulls
        & > [class*="#{$device_name}"] {
            @extend  %padding_nulls;
        }         
    }

}


/* 6. Responsive utilites (Утилиты адаптации)  */


// v2

/* Hide on large, show on small (Скрываем на больших, на малых показываем) */

@each $device_name, $device_obj in $devices {

    // Hide up (larger than this)
    $hidden_breakpoint: map-get($device_obj, breakpoint)+0px;
    @media  (min-width : $hidden_breakpoint ) {
        body .#{$device_name}hidden {
            display: none;
        }

    }

    // Show down (smaller than)
    $hidden_breakpoint: map-get($device_obj, breakpoint)-1px;
    @media  (max-width : $hidden_breakpoint ) {

        body .#{$device_name}visible {
            display: none;
        }

    } 

    
}


/* 7. Text utulites  */

    .lefted{ text-align : left; }
    .righted{ text-align : right; }
    .centered{ text-align : center; }
    .justifyed{ text-align : justify; }


@each $device_name, $device_obj in $devices {

    @media (min-width : #{map-get($device_obj, breakpoint)}px) {

        .#{$device_name}lefted{ text-align : left; }
        .#{$device_name}righted{ text-align : right; }
        .#{$device_name}centered{ text-align : center; }
        .#{$device_name}justifyed{ text-align : justify; }

    }

}


/* 8. SCSS Utulites  */

/* 8.1 Functions  */
@import 'utilites'; //

/* 8.2 Mixins  */
@import 'mixins'; //


/* 9 Beta test*/
@import 'beta_test'; //
// Beta test
// если есть место -  будет центрироваться, 
// если нет - будет разместившись по-центру наезжать на колонку, и а контент будет съезжать вниз

// .row {
//     .self_centered {
//         float: none;
//         margin: 0 auto;
//     }
// }
// // будет центрироваться в пространстве оставленном другими колонками
// .row-flex {
//     .self_centered {
//         margin: 0 auto;
//     }
// }

// ТЕСТЫ:
/*
+ В ряду .row одна ячейка
+ В ряду .row-flex одна ячейка

+ В ряду .row несколько ячеек - становится по центру, другние сталкивает на след-ю строку
+ В ряду .row-flex несколько ячеек - 
сначала вычисляется свободное пространство после заполнения другими  элементами контейнера,
после этого рассчитываются расположение с авто отступами margin, расталкивая другие элементы
*/
