/*
 * Copyright (c) 2019-present Sonatype, Inc.
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which accompanies this
 * distribution and is available at https://www.eclipse.org/legal/epl-2.0/.
 */
@use 'sass:math';

.nx-policy-threat-slider {
  $rail-height: 8px;
  $thumb-size: 32px;
  $thumb-border-width: 2px;

  // distance from top of thumb to top of rail
  $thumb-vertical-overflow: math.div($thumb-size - $rail-height, 2);

  // flex so line-height doesn't get involved in the element overall height
  display: inline-flex;
  margin: var(--nx-spacing-2x) 0;

  // the .MuiSlider-thumb elements actually stick out of the sides of the .MuiSlider-root at their min and max
  // positions.  This padding ensures that they don't stick out of the overall box for this component
  padding: 0 math.div($thumb-size, 2);

  .MuiSlider-root {
    height: $rail-height;
    padding: $thumb-vertical-overflow 0;

    // NOTE: this number needs to be divisible by 10 for the track to render cleanly
    width: 220px;
  }

  .MuiSlider-markActive {
    background-color: currentColor;
    opacity: 1;
  }

  .MuiSlider-rail, .MuiSlider-track {
    display: none;
  }

  &, .Mui-disabled {
    .MuiSlider-thumb {
      box-sizing: border-box;
      background-color: var(--nx-color-component-background);
      border: $thumb-border-width solid;
      border-radius: var(--nx-border-radius);
      color: var(--nx-color-policy-threat-slider-text);
      font-size: var(--nx-font-size-s);
      font-weight: bold;
      height: $thumb-size;
      width: $thumb-size;
      line-height: 1;
      margin-left: math.div($thumb-size, -2);
      margin-top: -($thumb-vertical-overflow);
    }
  }

  .MuiSlider-thumb {
    align-items: center;
    display: flex;
    justify-content: center;
    position: absolute;

    &.MuiSlider-active, &:hover, &:focus-within {
      box-shadow: none;
    }

    &:focus-within::before {
      all: initial;
      box-shadow: inset 0 0 0 var(--nx-focus-outline-width) var(--nx-color-interactive-border-focus);
      border-radius: 10px;
      content: '';

      // this is a new shorthand for top, bottom, left, and right.
      // Note that these properties are measured from the inside of the container's border which is why the border
      // width is part of the calc
      inset: calc(0px - (var(--nx-spacing-base) + #{$thumb-border-width}));
      position: absolute;
    }
  }

  .nx-policy-threat-slider__value-label--none {
    .MuiSlider-thumb {
      border-color: var(--nx-color-threat-none);
    }
  }

  .nx-policy-threat-slider__value-label--low {
    .MuiSlider-thumb {
      border-color: var(--nx-color-threat-low);
    }
  }

  .nx-policy-threat-slider__value-label--moderate {
    .MuiSlider-thumb {
      border-color: var(--nx-color-threat-moderate);
    }
  }

  .nx-policy-threat-slider__value-label--severe {
    .MuiSlider-thumb {
      border-color: var(--nx-color-threat-severe);
    }
  }

  .nx-policy-threat-slider__value-label--critical {
    .MuiSlider-thumb {
      border-color: var(--nx-color-threat-critical);
    }
  }

  .Mui-disabled .nx-policy-threat-slider__value-label .MuiSlider-thumb {
    border-color: var(--nx-color-policy-threat-slider-border-disabled);
    background-color: var(--nx-color-policy-threat-slider-background-disabled);
    color: var(--nx-color-policy-threat-slider-text-disabled);
  }

  .MuiSlider-mark {
    $border-radius: math.div($rail-height, 2);

    height: $rail-height;

    // threat level 0
    border-radius: $border-radius 0 0 $border-radius;
    color: var(--nx-color-threat-none);

    top: $thumb-vertical-overflow;
    transform: none;

    // the first mark only goes from 0 - 0.5, so is only 5% wide.
    // The rest (except the last) each go from (n - 0.5) to (n + 0.5), so are 10% wide with a -5% left margin.
    // The last one doesn't go beyond the end so is also only 5% wide.
    // Extra px added to prevent browser rounding errors from causing visible gaps
    width: calc(5% + 1px);

    // ~ is the sibling selector (not to be confused with the adjacent sibling selector, +). Used here to count
    // which mark you are at. The marks are the elements that denote each step along the slider, and with this styling,
    // together constitute the background bar of the slider.  Different parts of the bar (i.e. different marks)
    // need to be different colors
    ~ .MuiSlider-mark {
      border-radius: 0;
      margin-left: -5%;

      width: calc(10% + 1px);

      // threat level 1
      color: var(--nx-color-threat-low);

      ~ .MuiSlider-mark {
        // threat level 2
        color: var(--nx-color-threat-moderate);

        ~ .MuiSlider-mark {
          // threat level 3

          ~ .MuiSlider-mark {
            // threat level 4
            color: var(--nx-color-threat-severe);

            ~ .MuiSlider-mark {
              // threat level 5

              ~ .MuiSlider-mark {
                // threat level 6

                ~ .MuiSlider-mark {
                  // threat level 7

                  ~ .MuiSlider-mark {
                    // threat level 8
                    color: var(--nx-color-threat-critical);

                    ~ .MuiSlider-mark {
                      // threat level 9

                      ~ .MuiSlider-mark {
                        // threat level 10
                        border-radius: 0 $border-radius $border-radius 0;
                        width: 5%;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
