import Slider from "@home-assistant/webawesome/dist/components/slider/slider";
import { css, type CSSResultGroup } from "lit";
import { customElement, property } from "lit/decorators";
import { mainWindow } from "../common/dom/get_main_window";

@customElement("ha-slider")
export class HaSlider extends Slider {
  @property({ reflect: true }) size: "s" | "m" = "s";

  @property({ type: Boolean, attribute: "with-tooltip" }) withTooltip = true;

  public connectedCallback() {
    super.connectedCallback();
    this.dir = mainWindow.document.dir;
  }

  static get styles(): CSSResultGroup {
    return [
      Slider.styles,
      css`
        :host {
          --track-size: var(--ha-slider-track-size, 4px);
          --marker-height: calc(var(--ha-slider-track-size, 4px) / 2);
          --marker-width: calc(var(--ha-slider-track-size, 4px) / 2);
          --wa-color-surface-default: var(--card-background-color);
          --wa-color-neutral-fill-normal: var(
            --ha-slider-track-color,
            var(--disabled-color)
          );
          --wa-tooltip-background-color: var(
            --ha-tooltip-background-color,
            var(--secondary-background-color)
          );
          --wa-tooltip-content-color: var(
            --ha-tooltip-text-color,
            var(--primary-text-color)
          );
          --wa-tooltip-font-family: var(
            --ha-tooltip-font-family,
            var(--ha-font-family-body)
          );
          --wa-tooltip-font-size: var(
            --ha-tooltip-font-size,
            var(--ha-font-size-s)
          );
          --wa-tooltip-font-weight: var(
            --ha-tooltip-font-weight,
            var(--ha-font-weight-normal)
          );
          --wa-tooltip-line-height: var(
            --ha-tooltip-line-height,
            var(--ha-line-height-condensed)
          );
          --wa-tooltip-padding: var(--ha-tooltip-padding, var(--ha-space-2));
          --wa-tooltip-border-radius: var(
            --ha-tooltip-border-radius,
            var(--ha-border-radius-sm)
          );
          --wa-tooltip-arrow-size: var(--ha-tooltip-arrow-size, 0px);
          --wa-tooltip-border-width: 0px;
          --wa-z-index-tooltip: 1000;
          min-width: 100px;
          min-inline-size: 100px;
          width: 200px;
        }

        /* Expand slider touch target to 32px */
        #slider {
          padding-block: 14px;
          margin-block: -14px;
        }

        #thumb {
          border: none;
          background-color: var(--ha-slider-thumb-color, var(--primary-color));
          overflow: hidden;
        }

        #thumb:after {
          content: "";
          border-radius: 50%;
          position: absolute;
          width: calc(var(--thumb-width) * 2 + 8px);
          height: calc(var(--thumb-height) * 2 + 8px);
          left: calc(-50% - 4px);
          top: calc(-50% - 4px);
          cursor: pointer;
        }

        #slider:focus-visible:not(.disabled) #thumb,
        #slider:focus-visible:not(.disabled) #thumb-min,
        #slider:focus-visible:not(.disabled) #thumb-max {
          outline: var(--wa-focus-ring);
        }

        #track:after {
          content: "";
          position: absolute;
          top: calc(-50% - 4px);
          left: 0;
          width: 100%;
          height: calc(var(--track-size) * 2 + 8px);
          cursor: pointer;
        }

        #indicator {
          background-color: var(
            --ha-slider-indicator-color,
            var(--primary-color)
          );
        }

        :host([size="m"]) {
          --thumb-width: 20px;
          --thumb-height: 20px;
        }

        :host([size="s"]) {
          --thumb-width: 16px;
          --thumb-height: 16px;
        }
      `,
    ];
  }
}

declare global {
  interface HTMLElementTagNameMap {
    "ha-slider": HaSlider;
  }
}
