import { initialState } from "@lit/task";
import { html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators";
import type { HassEntity } from "home-assistant-js-websocket";
import { AsyncValueTask } from "../../common/controllers/async-value-task";
import { fireEvent } from "../../common/dom/fire_event";
import { entityIcon } from "../../data/icons";
import type { IconSelector } from "../../data/selector";
import type { HomeAssistant } from "../../types";
import "../ha-icon-picker";
import "../ha-state-icon";

@customElement("ha-selector-icon")
export class HaIconSelector extends LitElement {
  @property({ attribute: false }) public hass!: HomeAssistant;

  @property({ attribute: false }) public selector!: IconSelector;

  @property() public value?: string;

  @property() public label?: string;

  @property() public helper?: string;

  @property({ type: Boolean, reflect: true }) public disabled = false;

  @property({ type: Boolean }) public required = true;

  @property({ attribute: false }) public context?: {
    icon_entity?: string;
  };

  private get _stateObj(): HassEntity | undefined {
    const iconEntity = this.context?.icon_entity;
    return iconEntity ? this.hass.states[iconEntity] : undefined;
  }

  private _placeholderTask = new AsyncValueTask(this, {
    task: ([
      placeholder,
      attributeIcon,
      entities,
      config,
      connection,
      stateObj,
    ]) => {
      if (placeholder || attributeIcon || !stateObj) {
        return initialState;
      }
      return entityIcon(entities, config, connection, stateObj);
    },
    args: () => {
      const stateObj = this._stateObj;
      return [
        this.selector.icon?.placeholder,
        stateObj?.attributes.icon,
        this.hass.entities,
        this.hass.config,
        this.hass.connection,
        stateObj,
      ] as const;
    },
  });

  protected render() {
    const stateObj = this._stateObj;

    const placeholder =
      this.selector.icon?.placeholder ||
      stateObj?.attributes.icon ||
      (stateObj && this._placeholderTask.value);

    return html`
      <ha-icon-picker
        .hass=${this.hass}
        .label=${this.label}
        .value=${this.value}
        .required=${this.required}
        .disabled=${this.disabled}
        .helper=${this.helper}
        .placeholder=${this.selector.icon?.placeholder ?? placeholder}
        @value-changed=${this._valueChanged}
      >
        ${!placeholder && stateObj
          ? html`
              <ha-state-icon slot="start" .stateObj=${stateObj}></ha-state-icon>
            `
          : nothing}
      </ha-icon-picker>
    `;
  }

  private _valueChanged(ev: CustomEvent) {
    fireEvent(this, "value-changed", { value: ev.detail.value });
  }
}

declare global {
  interface HTMLElementTagNameMap {
    "ha-selector-icon": HaIconSelector;
  }
}
