import { mdiPalette } from "@mdi/js";
import type { CSSResultGroup } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import {
  array,
  assert,
  assign,
  boolean,
  number,
  object,
  optional,
  string,
  union,
} from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { computeDomain } from "../../../../common/entity/compute_domain";
import { hasLocation } from "../../../../common/entity/has_location";
import type { LocalizeFunc } from "../../../../common/translations/localize";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import "../../../../components/ha-formfield";
import "../../../../components/ha-selector/ha-selector-select";
import "../../../../components/ha-switch";
import type { SelectSelector } from "../../../../data/selector";
import type { HomeAssistant, ValueChangedEvent } from "../../../../types";
import { DEFAULT_HOURS_TO_SHOW, DEFAULT_ZOOM } from "../../cards/hui-map-card";
import type { MapCardConfig, MapEntityConfig } from "../../cards/types";
import "../../components/hui-entity-editor";
import "../hui-sub-element-editor";
import type {
  EditDetailElementEvent,
  SubElementEditorConfig,
  EntitiesEditorEvent,
} from "../types";
import type { HASSDomEvent } from "../../../../common/dom/fire_event";
import type { LovelaceCardEditor } from "../../types";
import { processEditorEntities } from "../process-editor-entities";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { configElementStyle } from "./config-elements-style";

export const mapEntitiesConfigStruct = union([
  object({
    entity: string(),
    label_mode: optional(string()),
    attribute: optional(string()),
    unit: optional(string()),
    focus: optional(boolean()),
    name: optional(string()),
  }),
  string(),
]);

const geoSourcesConfigStruct = union([
  object({
    source: string(),
    label_mode: optional(string()),
    attribute: optional(string()),
    unit: optional(string()),
    focus: optional(boolean()),
  }),
  string(),
]);

const cardConfigStruct = assign(
  baseLovelaceCardConfig,
  object({
    title: optional(string()),
    aspect_ratio: optional(string()),
    default_zoom: optional(number()),
    dark_mode: optional(boolean()),
    entities: optional(array(mapEntitiesConfigStruct)),
    hours_to_show: optional(number()),
    geo_location_sources: optional(array(geoSourcesConfigStruct)),
    auto_fit: optional(boolean()),
    fit_zones: optional(boolean()),
    theme_mode: optional(string()),
  })
);

const themeModes = ["auto", "light", "dark"] as const;

const SUB_SCHEMA = [
  { name: "entity", selector: { entity: {} }, required: true },
  { name: "name", selector: { text: {} } },
] as const;

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

  @state() private _config?: MapCardConfig;

  @state() private _subElementEditorConfig?: SubElementEditorConfig;

  @state() private _configEntities?: MapEntityConfig[];

  @state() private _possibleGeoSources?: { value: string; label?: string }[];

  private _schema = memoizeOne(
    (localize: LocalizeFunc) =>
      [
        { name: "title", selector: { text: {} } },
        {
          name: "",
          type: "expandable",
          iconPath: mdiPalette,
          title: localize(`ui.panel.lovelace.editor.card.map.appearance`),
          schema: [
            {
              name: "",
              type: "grid",
              schema: [
                { name: "aspect_ratio", selector: { text: {} } },
                {
                  name: "default_zoom",
                  default: DEFAULT_ZOOM,
                  selector: { number: { mode: "box", min: 0 } },
                },
                {
                  name: "theme_mode",
                  default: "auto",
                  selector: {
                    select: {
                      mode: "dropdown",
                      options: themeModes.map((themeMode) => ({
                        value: themeMode,
                        label: localize(
                          `ui.panel.lovelace.editor.card.map.theme_modes.${themeMode}`
                        ),
                      })),
                    },
                  },
                },
                {
                  name: "hours_to_show",
                  default: DEFAULT_HOURS_TO_SHOW,
                  selector: { number: { mode: "box", min: 0 } },
                },
              ],
            },
          ],
        },
      ] as const
  );

  public setConfig(config: MapCardConfig): void {
    assert(config, cardConfigStruct);

    // Migrate legacy dark_mode to theme_mode
    if (!this._config && !("theme_mode" in config)) {
      config = { ...config };
      if (config.dark_mode) {
        config.theme_mode = "dark";
      } else {
        config.theme_mode = "auto";
      }
      delete config.dark_mode;
      fireEvent(this, "config-changed", { config: config });
    }

    this._config = config;
    this._configEntities = config.entities
      ? (processEditorEntities(config.entities) as MapEntityConfig[])
      : [];
  }

  private _geoSourcesStrings = memoizeOne((sources): string[] | undefined =>
    sources?.map((s) => (typeof s === "string" ? s : s.source))
  );

  get _geo_location_sources(): string[] {
    return this._geoSourcesStrings(this._config!.geo_location_sources) || [];
  }

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

    if (this._subElementEditorConfig) {
      return html`
        <hui-sub-element-editor
          .hass=${this.hass}
          .config=${this._subElementEditorConfig}
          .schema=${SUB_SCHEMA}
          @go-back=${this._goBack}
          @config-changed=${this._handleSubEntityChanged}
        >
        </hui-sub-element-editor>
      `;
    }

    return html`
      <ha-form
        .hass=${this.hass}
        .data=${this._config}
        .schema=${this._schema(this.hass.localize)}
        .computeLabel=${this._computeLabelCallback}
        @value-changed=${this._valueChanged}
      ></ha-form>

      <hui-entity-editor
        .hass=${this.hass}
        .entities=${this._configEntities}
        .entityFilter=${hasLocation}
        can-edit
        @entities-changed=${this._entitiesValueChanged}
        @edit-detail-element=${this._editDetailElement}
      ></hui-entity-editor>

      <h3>
        ${this.hass.localize(
          "ui.panel.lovelace.editor.card.map.geo_location_sources"
        )}
      </h3>

      <ha-selector-select
        .label=${this.hass.localize("ui.panel.lovelace.editor.card.map.source")}
        .required=${false}
        .hass=${this.hass}
        .value=${this._geo_location_sources}
        @value-changed=${this._geoSourcesChanged}
        .selector=${this._selectSchema(
          this._possibleGeoSources,
          this.hass.localize
        )}
      ></ha-selector-select>
    `;
  }

  private _goBack(): void {
    this._subElementEditorConfig = undefined;
  }

  private _editDetailElement(ev: HASSDomEvent<EditDetailElementEvent>): void {
    this._subElementEditorConfig = ev.detail.subElementConfig;
  }

  private _handleSubEntityChanged(ev: CustomEvent): void {
    ev.stopPropagation();

    const index = this._subElementEditorConfig!.index!;

    const newEntities = this._configEntities!.concat();
    const newConfig = ev.detail.config as MapEntityConfig;
    this._subElementEditorConfig = {
      ...this._subElementEditorConfig!,
      elementConfig: newConfig,
    };
    newEntities[index] = newConfig;
    let config = this._config!;
    config = { ...config, entities: newEntities };
    this._config = config;
    this._configEntities = processEditorEntities(
      config.entities as any[]
    ) as MapEntityConfig[];

    fireEvent(this, "config-changed", { config });
  }

  private _selectSchema = memoizeOne(
    (options, localize: LocalizeFunc): SelectSelector => ({
      select: {
        sort: true,
        multiple: true,
        custom_value: true,
        options: options.length
          ? options
          : [
              {
                value: "",
                label: localize(
                  "ui.panel.lovelace.editor.card.map.no_geo_location_sources"
                ),
              },
            ],
      },
    })
  );

  private _entitiesValueChanged(
    ev: EntitiesEditorEvent<MapEntityConfig>
  ): void {
    if (ev.detail && ev.detail.entities) {
      this._config = { ...this._config!, entities: ev.detail.entities };

      this._configEntities = processEditorEntities(
        this._config.entities || []
      ) as MapEntityConfig[];
      fireEvent(this, "config-changed", { config: this._config! });
    }
  }

  private _geoSourcesChanged(ev: ValueChangedEvent<any>): void {
    if (!this._config || !this.hass) {
      return;
    }

    const value = ev.detail.value;

    if (this._geo_location_sources === value) {
      return;
    }

    if (value === "") {
      this._config = { ...this._config };
      delete this._config.geo_location_sources;
    } else {
      const newSources = value.map(
        (newSource) =>
          this._config!.geo_location_sources?.find(
            (oldSource) =>
              typeof oldSource === "object" && oldSource.source === newSource
          ) || newSource
      );
      this._config = {
        ...this._config,
        geo_location_sources: newSources,
      };
    }
    fireEvent(this, "config-changed", { config: this._config });
  }

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

  protected willUpdate() {
    if (this.hass && !this._possibleGeoSources) {
      const sources: Record<string, string> = {};
      Object.entries(this.hass.states).forEach(([entity_id, stateObj]) => {
        const domain = computeDomain(entity_id);
        if (domain === "geo_location" && stateObj.attributes.source) {
          sources[stateObj.attributes.source] = stateObj.attributes.attribution;
        }
      });

      this._possibleGeoSources = Object.entries(sources).map(
        ([source, attribution]) => ({
          value: source,
          label: attribution || source,
        })
      );
    }
  }

  private _computeLabelCallback = (
    schema: SchemaUnion<ReturnType<typeof this._schema>>
  ) => {
    switch (schema.name) {
      case "theme_mode":
        return this.hass!.localize(
          `ui.panel.lovelace.editor.card.map.${schema.name}`
        );
      case "default_zoom":
        return this.hass!.localize(
          `ui.panel.lovelace.editor.card.map.${schema.name}`
        );
      default:
        return this.hass!.localize(
          `ui.panel.lovelace.editor.card.generic.${schema.name}`
        );
    }
  };

  static get styles(): CSSResultGroup {
    return [configElementStyle, css``];
  }
}

declare global {
  interface HTMLElementTagNameMap {
    "hui-map-card-editor": HuiMapCardEditor;
  }
}
