import type { PropertyValues, TemplateResult } from "lit";
import { html, LitElement } from "lit";
import { customElement, query } from "lit/decorators";
import type { AlarmPanelCardConfig } from "../../../../src/panels/lovelace/cards/types";
import type { DemoCardConfig } from "../../components/demo-card";
import { provideHass } from "../../../../src/fake_data/provide_hass";
import "../../components/demo-cards";
import { mockIcons } from "../../../../demo/src/stubs/icons";

const ENTITIES = [
  {
    entity_id: "alarm_control_panel.alarm",
    state: "disarmed",
    attributes: {
      friendly_name: "Alarm",
    },
  },
  {
    entity_id: "alarm_control_panel.alarm_armed",
    state: "armed_home",
    attributes: {
      friendly_name: "Alarm",
    },
  },
  {
    entity_id: "alarm_control_panel.unavailable",
    state: "unavailable",
    attributes: {
      friendly_name: "Alarm",
    },
  },
  {
    entity_id: "alarm_control_panel.alarm_code",
    state: "disarmed",
    attributes: {
      friendly_name: "Alarm",
      code_format: "number",
    },
  },
];

const CONFIGS = [
  {
    heading: "Basic Example",
    config: {
      type: "alarm-panel",
      entity: "alarm_control_panel.alarm",
    },
  },
  {
    heading: "With Title",
    config: {
      type: "alarm-panel",
      entity: "alarm_control_panel.alarm_armed",
      name: "My Alarm",
    },
  },
  {
    heading: "Code Example",
    config: {
      type: "alarm-panel",
      entity: "alarm_control_panel.alarm_code",
    },
  },
  {
    heading: "Using only Arm_Home State",
    config: {
      type: "alarm-panel",
      entity: "alarm_control_panel.alarm",
      states: ["arm_home"],
    },
  },
  {
    heading: "Unavailable",
    config: {
      type: "alarm-panel",
      entity: "alarm_control_panel.unavailable",
      states: ["arm_home"],
    },
  },
  {
    heading: "Invalid Entity",
    config: {
      type: "alarm-panel",
      entity: "alarm_control_panel.alarm1",
    },
  },
] satisfies DemoCardConfig<AlarmPanelCardConfig>[];

@customElement("demo-lovelace-alarm-panel-card")
class DemoAlarmPanelEntity extends LitElement {
  @query("#demos") private _demoRoot!: HTMLElement;

  protected render(): TemplateResult {
    return html`<demo-cards id="demos" .configs=${CONFIGS}></demo-cards>`;
  }

  protected firstUpdated(changedProperties: PropertyValues<this>) {
    super.firstUpdated(changedProperties);
    const hass = provideHass(this._demoRoot);
    hass.updateTranslations(null, "en");
    hass.updateTranslations("lovelace", "en");
    hass.addEntities(ENTITIES);
    mockIcons(hass);
  }
}

declare global {
  interface HTMLElementTagNameMap {
    "demo-lovelace-alarm-panel-card": DemoAlarmPanelEntity;
  }
}
