/**
 * Switch 开关
 * @Author 瞿龙俊 - qulongjun@shine.design
 * @Date 2020-03-28 13:02
 */

@mixin shine-switch-base() {
    .#{$classPrefix}-switch  {
        // Base
        display: inline-block;
        font-size: 1rem;

        input:empty {
            margin-left: -999px;
            height: 0;
            width: 0;
            overflow: hidden;
            position: absolute;
            opacity: 0;
        }

        input:empty ~ span {
            display: inline-block;
            position: relative;
            float: left;
            width: 1px;
            text-indent: 0;
            cursor: pointer;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }

        input:empty ~ span:before,
        input:empty ~ span:after {
            position: absolute;
            display: block;
            top: 0;
            bottom: 0;
            left: 0;
            content: ' ';
            -webkit-transition: all 100ms ease-in;
            transition: all 100ms ease-in;
        }

        // State icons
        &.#{$classPrefix}-switch--icon {
            input:empty ~ span:after {
                @include shine-la-icon-self('\f342');
            }

            input:checked ~ span:after {
                content: '\f17b';
            }
        }

        // State icons
        &.#{$classPrefix}-switch--icon-check {
            input:checked ~ span:after {
                @include shine-la-icon-self('\f17b');
            }
        }

        // Default size
        & {
            $shine-switch-margin: 2px 0;
            $shine-switch-width: 54px;
            $shine-switch-height: 30px;
            $shine-switch-line-height: 26px;
            $shine-switch-space: 3px;
            $shine-switch-checked-tick-pos: 26px;
            $shine-switch-icon-font-size: 0.9em;

            @include shine-switch-size(
                $shine-switch-margin,
                $shine-switch-width,
                $shine-switch-height,
                $shine-switch-line-height,
                $shine-switch-space,
                $shine-switch-checked-tick-pos,
                $shine-switch-icon-font-size
            );
        }

        // Large size
        &.#{$classPrefix}-switch--lg {
            $shine-switch-margin: 2px 0;
            $shine-switch-width: 72px;
            $shine-switch-height: 40px;
            $shine-switch-line-height: 34px;
            $shine-switch-space: 3px;
            $shine-switch-checked-tick-pos: 34px;
            $shine-switch-icon-font-size: 1em;

            @include shine-switch-size(
                $shine-switch-margin,
                $shine-switch-width,
                $shine-switch-height,
                $shine-switch-line-height,
                $shine-switch-space,
                $shine-switch-checked-tick-pos,
                $shine-switch-icon-font-size
            );
        }

        // Small size
        &.#{$classPrefix}-switch--sm {
            $shine-switch-margin: 2px 0;
            $shine-switch-width: 38px;
            $shine-switch-height: 24px;
            $shine-switch-line-height: 20px;
            $shine-switch-space: 2px;
            $shine-switch-checked-tick-pos: 16px;
            $shine-switch-icon-font-size: 0.8em;

            @include shine-switch-size(
                $shine-switch-margin,
                $shine-switch-width,
                $shine-switch-height,
                $shine-switch-line-height,
                $shine-switch-space,
                $shine-switch-checked-tick-pos,
                $shine-switch-icon-font-size
            );
        }

        // Form group
        .form-group.row & {
            margin-top: 0.15rem;

            &.#{$classPrefix}-switch--lg {
                margin-top: 0rem;
                position: relative;
                top: -0.3rem;
            }

            &.#{$classPrefix}-switch--sm {
                margin-top: 0.3rem;
            }
        }
    }
}

// Component Size
@mixin shine-switch-size($margin, $width, $height, $line-height, $space, $checked-tick-pos, $icon-font-size) {
    input:empty ~ span {
        line-height: $height;
        margin: $margin;
        height: $height;
        width: $width + $space;
        border-radius: ($height/2);
    }

    input:empty ~ span:before,
    input:empty ~ span:after {
        width: $width;
        border-radius: ($height/2);
    }

    input:empty ~ span:after {
        height: $height - 2 * $space;
        width: $height - 2 * $space;
        line-height: $line-height;
        top: $space;
        bottom: $space;
        margin-left: $space;
        font-size: $icon-font-size;
        text-align: center;
        vertical-align: middle;
    }

    input:checked ~ span:after {
        margin-left: $checked-tick-pos;
    }
}

// Component Skin
@mixin shine-switch-skin() {
    $base-color: #e8ebf1;

    .#{$classPrefix}-switch {
        // Panel bg
        input:empty ~ span:before {
            background-color: $base-color;
        }

        // Tick bg
        input:empty ~ span:after {
            color: lighten($base-color, 5%);
            background-color: #ffffff;
        }

        // Checked state
        input:checked {
            // Panel bg
            ~ span:before {
                background-color: $base-color;
                //box-shadow: 0px 3px 20px 0px rgba($base-color, 0.41);
            }

            // Tick bg
            ~ span:after {
                background-color: shine-state-color(brand, base);
                color: shine-state-color(brand, inverse);
                //box-shadow: 0px 3px 20px 0px rgba($base-color, 0.41);
            }
        }

        input[disabled] {
            cursor: not-allowed;

            ~ span:after,
            ~ span:before {
                cursor: not-allowed;
                opacity: 0.7;
            }
        }

        // States
        @each $name, $color in $shine-state-colors {
            // states
            &.#{$classPrefix}-switch--#{$name}:not(.#{$classPrefix}-switch--outline) {
                input:empty ~ span:before {
                    background-color: shine-get($color, base);
                }

                input:empty ~ span:after {
                    color: shine-get($color, base);
                    background-color: shine-get($color, inverse);
                    opacity: 0.4;
                }

                input:checked {
                    // Panel bg
                    ~ span:before {
                        background-color: shine-get($color, base);
                    }

                    // Tick bg
                    ~ span:after {
                        opacity: 1;
                    }
                }
            }

            // outline style states
            &.#{$classPrefix}-switch--outline.#{$classPrefix}-switch--#{$name} {
                input:empty ~ span:before {
                    border: 2px solid darken($base-color, 3%);
                    background-color: $base-color;
                }

                input:empty ~ span:after {
                    color: shine-get($color, inverse);
                    background-color: shine-get($color, panel);
                }

                input:checked {
                    // Panel bg
                    ~ span:before {
                        background-color: shine-get($color, inverse);
                    }

                    // Tick bg
                    ~ span:after {
                        background-color: shine-get($color, base);
                        opacity: 1;
                    }
                }
            }
        }
    }
}

// Component Base
@include shine-switch-base();

// Component Skin
@include shine-switch-skin();
