@use '../../../../styles/_globals.scss' as *;

$this-size: 130px;
$this-handle-size: 20px;
$this-extra-touch-space: $this-handle-size * 0.5;
$this-margin: $this-extra-touch-space;
$this-handle-scale: 2;
$this-handle-color: var(--c-accent);
$this-circle-width: 2px;
$this-dot-size: 14px;

@mixin onCircle($item-count, $circle-size, $item-size, $start-rotation: -90) {
  display: block;
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -($item-size * 0.5);
  width: $item-size;
  height: $item-size;

  $angle: ddivide(360, $item-count);
  $rot: $start-rotation;

  @for $i from 1 through $item-count {
    &:nth-of-type(#{$i}) {
      transform: rotate($rot * 1deg) translate(calc(#{$circle-size} / 2));
    }

    $rot: $rot + $angle;
  }
}

:host {
  // NOTE: do NOT declare --input-duration-slider-size here — an own
  // declaration on :host would shadow any inherited override from a parent
  // (e.g. focus-mode sizing the slider to the clock face). Fallbacks at each
  // use site provide the default.
  overflow: hidden;
  display: block;
  user-select: none;
  width: calc(var(--input-duration-slider-size, #{$this-size}) + #{$this-margin * 2});
  min-width: calc(var(--input-duration-slider-size, #{$this-size}) + #{$this-margin * 2});
  color: var(--text-color);
  &.is-dragging .circle {
    cursor: grabbing;
  }

  &.is-dragging .handle-wrapper {
    transition: none;
  }

  &.is-dragging {
    .value-wrapper {
      z-index: 1;
      pointer-events: none;
    }
  }
}

.circle {
  margin: $this-margin;
  width: var(--input-duration-slider-size, #{$this-size});
  height: var(--input-duration-slider-size, #{$this-size});
  border-radius: 100%;
  position: relative;
  box-shadow: 1px 2px 2px 0 rgba(0, 0, 0, 0.26);
  cursor: grab;

  border-color: transparent;

  // touch spacer
  &:after {
    content: '';
    position: absolute;
    top: -$this-extra-touch-space;
    right: -$this-extra-touch-space;
    left: -$this-extra-touch-space;
    bottom: -$this-extra-touch-space;
    border-radius: 100%;
  }

  // inner circle
  .inner-circle {
    position: absolute;
    top: $this-circle-width;
    right: $this-circle-width;
    left: $this-circle-width;
    bottom: $this-circle-width;
    border-radius: 100%;
    background: var(--bg-darker);
    border-color: transparent;
  }

  .dots {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    width: var(--input-duration-slider-size, #{$this-size});
    height: var(--input-duration-slider-size, #{$this-size});
    z-index: 4;
    pointer-events: none;
  }

  .dot {
    box-shadow: 1px 1px 1px var(--separator-alpha);
    height: 8px;
    width: 8px;
    border-radius: 100%;
    cursor: grabbing;
    background: var(--palette-primary-300);
    @include onCircle(
      12,
      calc(var(--input-duration-slider-size, #{$this-size}) - #{$this-circle-width}),
      $this-dot-size
    );
  }
}

$this-handle-border-width: 4px;

.handle-wrapper {
  cursor: grab;
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 2;
  transition: var(--transition-duration-m) var(--ani-standard-timing);

  &:after {
    content: '';
    position: absolute;
    top: -$this-extra-touch-space;
    right: -$this-extra-touch-space;
    left: -$this-extra-touch-space;
    bottom: -$this-extra-touch-space;
  }
}

$this-stroke-width: 4px;

.handle {
  top: 0;
  background: $this-handle-color;
  left: 50%;
  width: 0;
  height: $this-handle-size + 5;
  position: absolute;
  transform-origin: 50% 100%;
  cursor: grabbing;

  // actual handle
  &:after {
    box-shadow: 1px 1px 2px var(--separator-alpha);
    content: '';
    top: -$this-handle-size * 0.5 + $this-circle-width * 0.5;
    background: $this-handle-color;
    left: 50%;
    margin-left: -#{$this-handle-size * 0.5};
    position: absolute;
    transform-origin: 50% 100%;
    height: $this-handle-size;
    width: $this-handle-size;
    border-radius: 100%;
    cursor: grabbing;
  }
}

.value-wrapper {
  z-index: 3;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 70%;
  text-align: center;
  transform: translate3d(-50%, -50%, 0);
}

.value {
  width: 100%;
  text-align: center;
  // Default matches historical 16px; parent can override via
  // --input-duration-slider-value-size (e.g. focus-mode scales with the circle).
  font-size: var(--input-duration-slider-value-size, 16px);
  line-height: 1;
  padding: 0;
  cursor: text;
  transition: all var(--transition-duration-m) var(--ani-leave-timing);
  transition-property: opacity, transform;
  display: block;
  position: relative;
  overflow: visible;
  user-select: none;
  backface-visibility: hidden;
  transform: translateZ(0);
  z-index: 4;
  border: none;
  border-bottom-left-radius: 20px;
  border-bottom-right-radius: 20px;
  background-color: transparent;

  caret-color: var(--text-color-most-intense);
  color: var(--text-color-most-intense);

  &:focus {
    @include inlineEditElevation();
    outline: none;
    z-index: 5;
    user-select: all;
    transform: scale(1.2);

    &:after {
      opacity: 1;
    }

    @media (max-width: var(--layout-xs)) {
      transform: scale(1.05);
    }
  }
}

label {
  font-size: 13px;
  margin-bottom: 3px;
  display: inline-block;
  cursor: pointer;
  text-align: center;
  backface-visibility: hidden;
}

// ---------------------------------------------------------------------------
// Opt-in variants (consumers set [bareRing] / [hideHandle]) — previously these
// lived as ::ng-deep overrides in focus-mode-main; owning them here keeps the
// shared component self-contained.
// ---------------------------------------------------------------------------

// Strip the slider's own circular chrome so a ring rendered behind it (e.g.
// focus-clock-face) provides the visual — only the handle dot and the editable
// value remain.
:host(.bare-ring) {
  // Let the dot sit on the ring at every rotation instead of being clipped.
  overflow: visible;

  .circle {
    background: transparent !important;
    box-shadow: none !important;
    cursor: grab;
  }

  .inner-circle,
  .dots {
    display: none;
  }

  // Round handle on the ring centerline, larger than the ring stroke; drop the
  // vertical bar so only the dot shows.
  .handle {
    background: transparent;
    height: 0;

    &::after {
      width: 30px;
      height: 30px;
      margin-left: -15px;
      top: -15px;
      background: var(--c-accent);
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    }
  }

  // Widen the value to the full ring so large (clock-time) digits fit centered.
  .value-wrapper {
    width: 100%;
  }

  .value {
    font-family: inherit;
    font-weight: bold;
    line-height: 1;
    font-variant-numeric: tabular-nums;
    color: inherit;
    // Base cursor is already `text`; make the click-to-edit affordance explicit
    // on hover with a subtle accent underline so the digits read as editable.
    text-decoration: underline transparent;
    text-decoration-thickness: 2px;
    text-underline-offset: 0.12em;
    transition: text-decoration-color var(--transition-duration-m)
      var(--ani-standard-timing);

    &:hover {
      text-decoration-color: var(--c-accent);
    }

    // Suppress the focus scale-up — at clock-time sizes it would push digits
    // past the ring.
    &:focus {
      transform: none;
    }
  }
}

// Type-only: no dragging. The host swallows pointer events so dragging the ring
// can't shift the value; only the editable input stays interactive.
:host(.hide-handle) {
  pointer-events: none;

  .handle {
    display: none;
  }

  .circle {
    cursor: default;
  }

  .value {
    pointer-events: auto;
  }
}
