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 {
  any,
  array,
  assert,
  assign,
  boolean,
  dynamic,
  enums,
  literal,
  number,
  object,
  optional,
  string,
  type,
  union,
} from "superstruct";
import type { HASSDomEvent } from "../../../../common/dom/fire_event";
import { fireEvent } from "../../../../common/dom/fire_event";
import { customType } from "../../../../common/structs/is-custom-type";
import "../../../../components/ha-card";
import "../../../../components/ha-formfield";
import "../../../../components/ha-icon";
import "../../../../components/ha-switch";
import "../../../../components/ha-theme-picker";
import "../../../../components/input/ha-input";
import { isCustomType } from "../../../../data/lovelace_custom_cards";
import type { HomeAssistant } from "../../../../types";
import {
  computeShowHeaderToggle,
  migrateEntitiesCardConfig,
} from "../../cards/hui-entities-card";
import type { EntitiesCardConfig } from "../../cards/types";
import { processConfigEntities } from "../../common/process-config-entities";
import { timeFormatConfigStruct } from "../../components/types";
import type { LovelaceRowConfig } from "../../entity-rows/types";
import { headerFooterConfigStructs } from "../../header-footer/structs";
import type { LovelaceCardEditor } from "../../types";
import "../header-footer-editor/hui-header-footer-editor";
import "../hui-entities-card-row-editor";
import "../hui-sub-element-editor";
import { processEditorEntities } from "../process-editor-entities";
import { actionConfigStruct } from "../structs/action-struct";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { buttonEntityConfigStruct } from "../structs/button-entity-struct";
import { entitiesConfigStruct } from "../structs/entities-struct";
import type {
  EditDetailElementEvent,
  EditorTarget,
  SubElementEditorConfig,
} from "../types";
import { configElementStyle } from "./config-elements-style";

const buttonEntitiesRowConfigStruct = object({
  type: literal("button"),
  entity: optional(string()),
  name: optional(string()),
  icon: optional(string()),
  action_name: optional(string()),
  tap_action: actionConfigStruct,
  hold_action: optional(actionConfigStruct),
  double_tap_action: optional(actionConfigStruct),
});

const castEntitiesRowConfigStruct = object({
  type: literal("cast"),
  view: optional(union([string(), number()])),
  dashboard: optional(string()),
  name: optional(string()),
  icon: optional(string()),
  hide_if_unavailable: optional(boolean()),
});

const callServiceEntitiesRowConfigStruct = object({
  type: enums(["call-service", "perform-action"]),
  name: string(),
  service: optional(string()),
  action: optional(string()),
  icon: optional(string()),
  action_name: optional(string()),
  // "service_data" is kept for backwards compatibility. Replaced by "data".
  service_data: optional(any()),
  data: optional(any()),
});

const conditionalEntitiesRowConfigStruct = object({
  type: literal("conditional"),
  row: any(),
  conditions: array(any()),
});

const dividerEntitiesRowConfigStruct = object({
  type: literal("divider"),
  style: optional(any()),
});

const sectionEntitiesRowConfigStruct = object({
  type: literal("section"),
  label: optional(string()),
});

const webLinkEntitiesRowConfigStruct = object({
  type: literal("weblink"),
  url: string(),
  name: optional(string()),
  icon: optional(string()),
});

const buttonsEntitiesRowConfigStruct = object({
  type: literal("buttons"),
  entities: array(buttonEntityConfigStruct),
});

const attributeEntitiesRowConfigStruct = object({
  type: literal("attribute"),
  entity: string(),
  attribute: string(),
  prefix: optional(string()),
  suffix: optional(string()),
  name: optional(string()),
  icon: optional(string()),
  time_format: optional(timeFormatConfigStruct),
});

const textEntitiesRowConfigStruct = object({
  type: literal("text"),
  name: string(),
  text: string(),
  icon: optional(string()),
});

const customEntitiesRowConfigStruct = type({
  type: customType(),
});

const entitiesRowConfigStruct = dynamic<any>((value) => {
  if (value && typeof value === "object" && "type" in value) {
    if (isCustomType((value as LovelaceRowConfig).type!)) {
      return customEntitiesRowConfigStruct;
    }

    switch ((value as LovelaceRowConfig).type!) {
      case "attribute": {
        return attributeEntitiesRowConfigStruct;
      }
      case "button": {
        return buttonEntitiesRowConfigStruct;
      }
      case "buttons": {
        return buttonsEntitiesRowConfigStruct;
      }
      case "perform-action":
      case "call-service": {
        return callServiceEntitiesRowConfigStruct;
      }
      case "cast": {
        return castEntitiesRowConfigStruct;
      }
      case "conditional": {
        return conditionalEntitiesRowConfigStruct;
      }
      case "divider": {
        return dividerEntitiesRowConfigStruct;
      }
      case "section": {
        return sectionEntitiesRowConfigStruct;
      }
      case "text": {
        return textEntitiesRowConfigStruct;
      }
      case "weblink": {
        return webLinkEntitiesRowConfigStruct;
      }
    }
  }

  // No "type" property => has to be the default entity row config struct
  return entitiesConfigStruct;
});

const cardConfigStruct = assign(
  baseLovelaceCardConfig,
  object({
    title: optional(union([string(), boolean()])),
    entity: optional(string()),
    theme: optional(string()),
    icon: optional(string()),
    show_header_toggle: optional(boolean()),
    state_color: optional(boolean()),
    entities: array(entitiesRowConfigStruct),
    header: optional(headerFooterConfigStructs),
    footer: optional(headerFooterConfigStructs),
  })
);

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

  @state() private _config?: EntitiesCardConfig;

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

  @state() private _subElementEditorConfig?: SubElementEditorConfig;

  public setConfig(config: EntitiesCardConfig): void {
    const migratedConfig = migrateEntitiesCardConfig(config);
    assert(migratedConfig, cardConfigStruct);
    this._config = migratedConfig;
    this._configEntities = processEditorEntities(migratedConfig.entities);
  }

  private _showHeaderToggle = memoizeOne((config: EntitiesCardConfig) => {
    if (config.show_header_toggle !== undefined) {
      return config.show_header_toggle;
    }
    return computeShowHeaderToggle(
      config,
      processConfigEntities(config.entities)
    );
  });

  get _title(): string {
    return this._config!.title || "";
  }

  get _theme(): string {
    return this._config!.theme || "";
  }

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

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

    return html`
      <div class="card-config">
        <ha-input
          .label="${this.hass.localize(
            "ui.panel.lovelace.editor.card.generic.title"
          )} (${this.hass.localize(
            "ui.panel.lovelace.editor.card.config.optional"
          )})"
          .value=${this._title}
          .configValue=${"title"}
          @input=${this._valueChanged}
        ></ha-input>
        <ha-theme-picker
          .value=${this._theme}
          .label=${`${this.hass!.localize(
            "ui.panel.lovelace.editor.card.generic.theme"
          )} (${this.hass!.localize(
            "ui.panel.lovelace.editor.card.config.optional"
          )})`}
          .configValue=${"theme"}
          @value-changed=${this._valueChanged}
        ></ha-theme-picker>
        <div class="side-by-side">
          <ha-formfield
            .label=${this.hass.localize(
              "ui.panel.lovelace.editor.card.entities.show_header_toggle"
            )}
          >
            <ha-switch
              .checked=${this._showHeaderToggle(this._config)}
              .configValue=${"show_header_toggle"}
              @change=${this._valueChanged}
            ></ha-switch>
          </ha-formfield>
          <ha-formfield
            .label=${this.hass.localize(
              "ui.panel.lovelace.editor.card.generic.state_color"
            )}
          >
            <ha-switch
              .checked=${this._config!.state_color}
              .configValue=${"state_color"}
              @change=${this._valueChanged}
            ></ha-switch>
          </ha-formfield>
        </div>
        <hui-header-footer-editor
          .hass=${this.hass}
          .configValue=${"header"}
          .config=${this._config.header}
          @value-changed=${this._valueChanged}
          @edit-detail-element=${this._editDetailElement}
        ></hui-header-footer-editor>
        <hui-header-footer-editor
          .hass=${this.hass}
          .configValue=${"footer"}
          .config=${this._config.footer}
          @value-changed=${this._valueChanged}
          @edit-detail-element=${this._editDetailElement}
        ></hui-header-footer-editor>
      </div>
      <hui-entities-card-row-editor
        .hass=${this.hass}
        .entities=${this._configEntities}
        @entities-changed=${this._valueChanged}
        @edit-detail-element=${this._editDetailElement}
      ></hui-entities-card-row-editor>
    `;
  }

  private _valueChanged(ev: CustomEvent): void {
    ev.stopPropagation();
    if (!this._config || !this.hass) {
      return;
    }

    const target = ev.target! as EditorTarget;
    const configValue =
      target.configValue || this._subElementEditorConfig?.type;
    const value =
      target.checked !== undefined
        ? target.checked
        : target.value || ev.detail.config || ev.detail.value;

    if (
      (configValue === "title" && target.value === this._title) ||
      (configValue === "theme" && target.value === this._theme)
    ) {
      return;
    }

    if (configValue === "row" || (ev.detail && ev.detail.entities)) {
      const newConfigEntities =
        ev.detail.entities || this._configEntities!.concat();
      if (configValue === "row") {
        if (!value) {
          newConfigEntities.splice(this._subElementEditorConfig!.index!, 1);
          this._goBack();
        } else {
          newConfigEntities[this._subElementEditorConfig!.index!] = value;
        }

        this._subElementEditorConfig!.elementConfig = value;
      }

      this._config = { ...this._config!, entities: newConfigEntities };
      this._configEntities = processEditorEntities(this._config!.entities);
    } else if (configValue) {
      if (value === "") {
        this._config = { ...this._config };
        delete this._config[configValue!];
      } else {
        this._config = {
          ...this._config,
          [configValue]: value,
        };
      }
    }

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

  private _handleSubElementChanged(ev: CustomEvent): void {
    ev.stopPropagation();
    if (!this._config || !this.hass) {
      return;
    }

    const configValue = this._subElementEditorConfig?.type;
    const value = ev.detail.config;

    if (configValue === "row") {
      const newConfigEntities = this._configEntities!.concat();
      if (!value) {
        newConfigEntities.splice(this._subElementEditorConfig!.index!, 1);
        this._goBack();
      } else {
        newConfigEntities[this._subElementEditorConfig!.index!] = value;
      }

      this._config = { ...this._config!, entities: newConfigEntities };
      this._configEntities = processEditorEntities(this._config!.entities);
    } else if (configValue) {
      if (value === "") {
        this._config = { ...this._config };
        delete this._config[configValue!];
      } else {
        this._config = {
          ...this._config,
          [configValue]: value,
        };
      }
    }

    this._subElementEditorConfig = {
      ...this._subElementEditorConfig!,
      elementConfig: value,
    };

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

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

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

  static get styles(): CSSResultGroup {
    return [
      configElementStyle,
      css`
        .edit-entity-row-header {
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: var(--ha-font-size-l);
        }

        hui-header-footer-editor {
          padding-top: var(--ha-space-1);
        }

        ha-input {
          --ha-input-padding-bottom: var(--ha-space-4);
        }
      `,
    ];
  }
}

declare global {
  interface HTMLElementTagNameMap {
    "hui-entities-card-editor": HuiEntitiesCardEditor;
  }
}
