/* Default Ubuntu Dock styling, append !important to any changed rules
   This must be in sync with
   - https://github.com/micheleg/dash-to-dock/blob/ubuntu-dock/_stylesheet.scss
 */

// Dash to dock specifics
$dash_padding: $base_padding + 4px; // 10px
$dash_border_radius: $modal_radius * 1.5;

$dock_spacing: round($base_padding / 4);
$dock_bottom_margin: $base_margin * 4;
$dock_start_margin: $dock_bottom_margin;
$dock_side_margin: $dock_start_margin / 4;
$dock_edge_items_padding: $dash_padding - $dock_spacing;
$dock_fixed_inner_margin: $dock_side_margin;
$dock_icons_distance: 4px;

@function shrink($val) {
    @return round($val / 4);
}

@function shrink_light($val) {
    @return round($val * 0.75);
}

@function is_horizontal($side) {
    @return $side == top or $side == bottom;
}

@function checked-map-get($map, $key) {
    $value: map-get($map, $key);
    @if ($value == null) {
        @error 'Key ' + $key + ' not found';
    }
    @return $value;
}

@function opposite($val) {
    @return checked-map-get(
        (
            left: right,
            right: left,
            top: bottom,
            bottom: top,
        ),
        $val
    );
}

// minor-dimension is vertical space in horizontal mode and
// horizontal space in vertical mode.
@mixin set-internal-children-property($side, $property,
                                      $minor_dimension,
                                      $major_dimension: $minor_dimension) {
    @if $property != margin and $property != padding {
        @error "Property " + $property + " not supported!";
    }

    @if is_horizontal($side) {
        #{$property}: $minor_dimension $major_dimension;
        &:ltr:first-child, &:rtl:last-child {
            #{$property}-left: 0;
        }
        &:ltr:last-child, &:rtl:first-child {
            #{$property}-right: 0;
        }
    } @else {
        #{$property}: $major_dimension $minor_dimension;
        &:first-child {
            #{$property}-top: 0;
        }
        &:last-child {
            #{$property}-bottom: 0;
        }
    }
}

@mixin padded-edge-child($chid, $side, $padding) {
    @if $chid == first {
        @if is_horizontal($side) {
            padding-left: $padding;
        } @else {
            padding-top: $padding;
        }
    } @else if $chid == last {
        @if is_horizontal($side) {
            padding-right: $padding;
        } @else {
            padding-bottom: $padding;
        }
    } @else {
        @error "Invalid rule";
    }
}

// In extended mode we need to use the first and last .dash-item-container's
// to apply the padding on the dock, to ensure that the actual first or last
// child show-apps item will actually include the padding area so that it will
// be clickable up to the dock edge, and make Fitts happy.
// I don't think the same should happen for normal icons, so in the other side
// the padding will be applied via the scrolled area, given we can't get the
// parent of the first/last app-well-app icon to apply a rule there.
@mixin padded-dash-edge-items($side, $padding) {
    @each $child_pos in first, last {
        > :#{$child_pos}-child {
            // Use this instead of #dashtodockDashScrollview rule to apply the
            // padding via the last app-icon item
            //.dash-item-container:#{$child_pos}-child .app-well-app,
            .show-apps {
                @include padded-edge-child($child_pos, $side, $padding);
            }
        }

        #dashtodockDashScrollview:#{$child_pos}-child {
            @include padded-edge-child($child_pos, $side, $padding);
        }
    }
}

$dock_sides: [bottom, top, left, right];
$dock_style_modes: [null, shrink, extended, extended-shrink];

@mixin dock-container-style($side, $style_mode, $parameters) {
    $style_selector: '';
    $is_shrink: checked-map-get($parameters, is_shrink);
    $is_extended: checked-map-get($parameters, is_extended);
    $base_padding: checked-map-get($parameters, base_padding);
    $spacing: checked-map-get($parameters, spacing);
    $border_radius: checked-map-get($parameters, border_radius);
    $side_margin: checked-map-get($parameters, side_margin);
    $padding: checked-map-get($parameters, padding);
    $margin: checked-map-get($parameters, margin);
    $edge_items_padding: checked-map-get($parameters, edge_items_padding);
    $fixed_inner_margin: checked-map-get($parameters, fixed_inner_margin);

    @if ($is_extended) {
        $style_selector: $style_selector +'.extended';
    }

    @if ($is_shrink) {
        $style_selector: $style_selector +'.shrink';
    }

    &#{$style_selector} {
        #dash {
            margin: 0px;
            padding: 0px;

            .dash-background {
                margin: 0;
                margin-#{$side}: $side_margin;
                padding: 0;
                border-radius: $border_radius;
                spacing: $base_padding;
            }

            .dash-separator {
                background-color: $borders_color_dark;
                @if is_horizontal($side) {
                    margin: 0 $margin;
                    width: 1px;
                    height: 0;
                } @else {
                    width: 0;
                    height: 1px;
                    margin: $margin 0;
                }
                margin-#{$side}: $side_margin;
            }

            #dashtodockDashContainer {
                padding: if($is_extended, 0, $padding);
                padding-#{$side}: 0;
                padding-#{opposite($side)}: 0;

                @if ($is_extended) {
                    @include padded-dash-edge-items($side, $edge_items_padding);
                }
            }

            .dash-item-container {
                /* Disable all margins defined upstream, we handle them here */
                > * {margin: 0;}

                @include set-internal-children-property($side, margin,
                    0, $dock_icons_distance / 2);

                .app-well-app,
                .show-apps {
                    @include set-internal-children-property($side, padding, $spacing);
                    padding-#{$side}: $padding + $side_margin;
                    padding-#{opposite($side)}: $padding;
                }

                .app-well-app {
                    &.running .overview-icon {
                        background-image: none;
                    }
                    &.focused .overview-icon {
                        background-color: rgba(238, 238, 236, 0.2);
                    }

                    .app-well-app-running-dot {
                        margin-bottom: 2px;
                    }
                }

                > StButton {
                    transition-duration: 250;
                    background-size: contain;
                }
            }
        }

        &.fixed {
            #dash {
                .dash-background {
                    margin-#{opposite($side)}: $fixed_inner_margin;
                }

                .dash-item-container {

                    .app-well-app,
                    .show-apps {
                        padding-#{opposite($side)}: $padding + $fixed_inner_margin;
                    }
                }
            }
        }
    }
}

@each $side in $dock_sides {
    @each $style_mode in $dock_style_modes {
        $is_shrink: str-index(#{$style_mode}, shrink) !=null;
        $is_extended: str-index(#{$style_mode}, extended) !=null;

        #dashtodockContainer.#{$side} {
            $parameters: (
                is_shrink: $is_shrink,
                is_extended: $is_extended,
                base_padding: $base_padding,
                spacing: $dock_spacing,
                border_radius: $dash_border_radius,
                side_margin: $dock_side_margin,
                padding: $dash_padding,
                margin: $base_margin,
                edge_items_padding: $dock_edge_items_padding,
                fixed_inner_margin: $dock_fixed_inner_margin,
            );

            @if ($is_shrink) {
                $parameters: map-merge($parameters, (
                    base_padding: shrink($base_padding),
                    spacing: shrink($dock_spacing),
                    border_radius: shrink_light($dash_border_radius),
                    side_margin: /* not shrunk */ $dock_side_margin,
                    padding: shrink($dash_padding),
                    margin: shrink($base_margin),
                    edge_items_padding: shrink_light($dock_edge_items_padding),
                    fixed_inner_margin: shrink($dock_fixed_inner_margin),
                ));
            }

            @if ($is_extended) {
                $parameters: map-merge($parameters, (
                    border_radius: 0,
                    side_margin: 0,
                    fixed_inner_margin: 0,
                ));
            }

            @include dock-container-style($side, $style_mode, $parameters);
        }

        .dash-label.#{$side} {
            $label_offset: if($is_shrink, shrink($dash_padding), $dash_padding);
            $style_selector: if($is_shrink, '.shrink', null);

            &#{$style_selector} {
                @if is_horizontal($side) {
                    margin-#{$side}: $label_offset;
                    -x-offset: 0;
                    -y-offset: 0;
                } @else {
                    margin: 0;
                    -x-offset: $label_offset;
                    -y-offset: 0;
                }
            }
        }
    }
}

#dashtodockContainer.top.shrink #dash .dash-background {
    margin-top: 4px;
    margin-bottom: 0;
}

#dashtodockContainer.straight-corner #dash .dash-background,
#dashtodockContainer.shrink.straight-corner #dash .dash-background {
    border-radius: 0px;
}

/* Scrollview style */
.bottom #dashtodockDashScrollview,
.top #dashtodockDashScrollview {
    -st-hfade-offset: 24px;
}

.left #dashtodockDashScrollview,
.right #dashtodockDashScrollview {
    -st-vfade-offset: 24px;
}

#dashtodockContainer.dashtodock #dash .dash-background {
    background: $dash_background_color;
}

#dashtodockContainer.dashtodock .progress-bar {
    /* Customization of the progress bar style, e.g.:
      -progress-bar-background: rgba(0.8, 0.8, 0.8, 1);
      -progress-bar-border: rgba(0.9, 0.9, 0.9, 1);
    */
}

#dashtodockContainer.top #dash .placeholder,
#dashtodockContainer.bottom #dash .placeholder {
    width: 32px;
    height: 1px;
}

/*
 * This is applied to a dummy actor. Only the alpha value for the background and border color
 * and the transition-duration are used
 */
#dashtodockContainer.dummy-opaque {
    background-color: rgba(0, 0, 0, 0.8);
    border-color: rgba(0, 0, 0, 0.4);
    transition-duration: 300ms;
}

/*
 * This is applied to a dummy actor. Only the alpha value for the background and border color
 * and the transition-duration are used
 */
#dashtodockContainer.dummy-transparent {
    background-color: rgba(0, 0, 0, 0.2);
    border-color: rgba(0, 0, 0, 0.1);
    transition-duration: 500ms;
}

#dashtodockContainer .number-overlay {
    color: rgba(255, 255, 255, 1);
    background-color: rgba(0, 0, 0, 0.8);
    text-align: center;
}

#dashtodockContainer .notification-badge {
    color: rgba(255, 255, 255, 1);
    background-color: rgba(255, 0, 0, 1);
    padding: 0.2em 0.5em;
    border-radius: 1em;
    font-weight: bold;
    text-align: center;
    margin: 2px;
}

#dashtodockPreviewSeparator.popup-separator-menu-item-horizontal {
    width: 1px;
    height: auto;
    border-right-width: 1px;
    margin: 32px 0px;
}

.dashtodock-app-well-preview-menu-item {
    padding: 1em 1em 0.5em 1em;
}

#dashtodockContainer .metro .overview-icon {
    border-radius: 0px;
}

@for $i from 2 through 4 {
    #dashtodockContainer.bottom .metro.running#{$i}.focused,
    #dashtodockContainer.top .metro.running#{$i}.focused {
        background-image: url("./media/highlight_stacked_bg.svg");
        background-position: 0px 0px;
        background-size: contain;
    }

    #dashtodockContainer.left .metro.running#{$i}.focused,
    #dashtodockContainer.right .metro.running#{$i}.focused {
        background-image: url("./media/highlight_stacked_bg_h.svg");
        background-position: 0px 0px;
        background-size: contain;
    }
}

/* Yaru Dock styling */

$dock_color: $panel_bg_color;

@each $side in bottom, top, left, right {
    #dashtodockContainer.#{$side} {
        #dash,
        &.dashtodock #dash,
        &.shrink #dash {
            margin: 0px;
            padding: 0px;

            .dash-background {
                background: transparentize($dock_color, 0.25);
                transition-duration: $panel_transition_duration;
            }

            .dash-separator {
                background: transparentize($osd_fg_color, 0.7);
            }
        }

        &.overview {
            #dash,
            &.dashtodock #dash,
            &.shrink #dash {
                .dash-background {
                    background-color: $dock_color;
                }
            }
        }

        &.extended {
            #dash,
            &.dashtodock #dash,
            &.shrink #dash {
                .dash-background {
                    // Yaru: remove the border we apply to the upstream dock
                    border: none;
                }
            }
        }

        .dash-item-container {
            .show-apps, .app-well-app {
                border-radius: $dash_border_radius;
                &:active, &:checked {
                    .overview-icon {
                        background-color: $base_active_color; box-shadow: none;
                    }
                }
            }
        }

        .notification-badge {
            background-color: $success_color;
            border: 1px solid darken($success_color,10%);
            box-shadow: -1px 1px 5px 0px transparentize(black, 0.5);
        }
    }
}
