import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import {
  array,
  assert,
  assign,
  boolean,
  object,
  optional,
  string,
  union,
} from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { LocalizeFunc } from "../../../../common/translations/localize";
import "../../../../components/entity/ha-entities-picker";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { CalendarCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";

const cardConfigStruct = assign(
  baseLovelaceCardConfig,
  object({
    title: optional(union([string(), boolean()])),
    initial_view: optional(string()),
    theme: optional(string()),
    entities: array(string()),
  })
);

const views = ["dayGridMonth", "dayGridDay", "listWeek"] as const;

@customElement("hui-calendar-card-editor")
export class HuiCalendarCardEditor
  extends LitElement
  implements LovelaceCardEditor
{
  @property({ attribute: false }) public hass?: HomeAssistant;

  @state() private _config?: CalendarCardConfig;

  public setConfig(config: CalendarCardConfig): void {
    assert(config, cardConfigStruct);
    this._config = config;
  }

  private _schema = memoizeOne(
    (localize: LocalizeFunc) =>
      [
        {
          name: "",
          type: "grid",
          schema: [
            { name: "title", required: false, selector: { text: {} } },
            {
              name: "initial_view",
              required: false,
              selector: {
                select: {
                  options: views.map((view) => ({
                    value: view,
                    label: localize(
                      `ui.panel.lovelace.editor.card.calendar.views.${view}`
                    ),
                  })),
                },
              },
            },
          ],
        },
        { name: "theme", required: false, selector: { theme: {} } },
      ] as const
  );

  protected render() {
    if (!this.hass || !this._config) {
      return nothing;
    }

    const schema = this._schema(this.hass.localize);
    const data = { initial_view: "dayGridMonth", ...this._config };

    return html`
      <ha-form
        .hass=${this.hass}
        .data=${data}
        .schema=${schema}
        .computeLabel=${this._computeLabelCallback}
        @value-changed=${this._valueChanged}
      ></ha-form>
      <h3>
        ${
          this.hass.localize(
            "ui.panel.lovelace.editor.card.calendar.calendar_entities"
          ) +
          " (" +
          this.hass!.localize("ui.panel.lovelace.editor.card.config.required") +
          ")"
        }
      </h3>
      <ha-entities-picker
        .value=${this._config.entities}
        .includeDomains=${["calendar"]}
        @value-changed=${this._entitiesChanged}
      >
      </ha-entities-picker>
    `;
  }

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

  private _entitiesChanged(ev): void {
    const config = { ...this._config!, entities: ev.detail.value };
    fireEvent(this, "config-changed", { config });
  }

  private _computeLabelCallback = (
    schema: SchemaUnion<ReturnType<typeof this._schema>>
  ) => {
    if (schema.name === "title") {
      return this.hass!.localize("ui.panel.lovelace.editor.card.generic.title");
    }

    if (schema.name === "theme") {
      return `${this.hass!.localize(
        "ui.panel.lovelace.editor.card.generic.theme"
      )} (${this.hass!.localize(
        "ui.panel.lovelace.editor.card.config.optional"
      )})`;
    }

    return this.hass!.localize(
      `ui.panel.lovelace.editor.card.calendar.${schema.name}`
    );
  };

  static styles = css`
    ha-form {
      display: block;
      overflow: auto;
    }
  `;
}

declare global {
  interface HTMLElementTagNameMap {
    "hui-calendar-card-editor": HuiCalendarCardEditor;
  }
}
