import type { CSSResultGroup } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators";
import memoizeOne from "memoize-one";
import type { ExtEntityRegistryEntry } from "../../../../data/entity/entity_registry";
import type { ExposeEntitySettings } from "../../../../data/expose";
import { voiceAssistants } from "../../../../data/expose";
import "../../../../panels/config/voice-assistants/entity-voice-settings";
import type { HomeAssistant } from "../../../../types";
import { showVoiceAssistantSettingsView } from "./show-view-voice-assistant-settings";

@customElement("ha-more-info-view-voice-assistants")
class MoreInfoViewVoiceAssistants extends LitElement {
  @property({ attribute: false }) public hass!: HomeAssistant;

  @property({ attribute: false }) public entry!: ExtEntityRegistryEntry;

  @property() public params?;

  private _calculateExposed = memoizeOne((entry: ExtEntityRegistryEntry) => {
    const exposed: ExposeEntitySettings = {};
    Object.keys(voiceAssistants).forEach((key) => {
      exposed[key] = entry.options?.[key]?.should_expose;
    });
    return exposed;
  });

  protected render() {
    if (!this.params) {
      return nothing;
    }
    return html`<entity-voice-settings
      .hass=${this.hass}
      .entityId=${this.entry.entity_id}
      .entry=${this.entry}
      .exposed=${this._calculateExposed(this.entry)}
      @edit-assistant=${this._editAssistant}
    ></entity-voice-settings>`;
  }

  private _editAssistant(ev: CustomEvent) {
    const assistant = ev.detail.assistant;
    showVoiceAssistantSettingsView(
      this,
      voiceAssistants[assistant].name,
      assistant
    );
  }

  static get styles(): CSSResultGroup {
    return [
      css`
        :host {
          display: flex;
          flex-direction: column;
        }
        .content {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          padding: var(--ha-space-6);
          flex: 1;
        }
      `,
    ];
  }
}

declare global {
  interface HTMLElementTagNameMap {
    "ha-more-info-view-voice-assistants": MoreInfoViewVoiceAssistants;
  }
}
