import { consume } from "@lit/context";
import { format } from "date-fns";
import type { HassEntity } from "home-assistant-js-websocket";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { transform } from "../../../common/decorators/transform";
import "../../../components/ha-date-input";
import "../../../components/ha-time-input";
import { apiContext, internationalizationContext } from "../../../data/context";
import { setDateTimeValue } from "../../../data/datetime";
import { UNAVAILABLE, UNKNOWN } from "../../../data/entity/entity";
import type { FrontendLocaleData } from "../../../data/translation";
import type {
  HomeAssistantApi,
  HomeAssistantInternationalization,
  ValueChangedEvent,
} from "../../../types";

@customElement("more-info-datetime")
class MoreInfoDatetime extends LitElement {
  @property({ attribute: false }) public stateObj?: HassEntity;

  @state()
  @consume({ context: internationalizationContext, subscribe: true })
  @transform<HomeAssistantInternationalization, FrontendLocaleData>({
    transformer: ({ locale }) => locale,
  })
  private _locale!: FrontendLocaleData;

  @state()
  @consume({ context: apiContext, subscribe: true })
  private _api!: HomeAssistantApi;

  protected render() {
    if (!this.stateObj || this.stateObj.state === UNAVAILABLE) {
      return nothing;
    }

    const dateObj =
      this.stateObj.state === UNKNOWN
        ? undefined
        : new Date(this.stateObj.state);
    const time = dateObj ? format(dateObj, "HH:mm:ss") : undefined;
    const date = dateObj ? format(dateObj, "yyyy-MM-dd") : undefined;

    return html`<ha-date-input
        .locale=${this._locale}
        .value=${date}
        @value-changed=${this._dateChanged}
      >
      </ha-date-input>
      <ha-time-input
        .value=${time}
        .locale=${this._locale}
        @value-changed=${this._timeChanged}
        @click=${this._stopEventPropagation}
      ></ha-time-input>`;
  }

  private _stopEventPropagation(ev: Event): void {
    ev.stopPropagation();
  }

  private _timeChanged(ev: ValueChangedEvent<string>): void {
    if (ev.detail.value) {
      const dateObj = new Date(this.stateObj!.state);
      const newTime = ev.detail.value.split(":").map(Number);
      dateObj.setHours(newTime[0], newTime[1], newTime[2]);

      setDateTimeValue(
        this._api.callService,
        this.stateObj!.entity_id,
        dateObj
      );
    }
  }

  private _dateChanged(ev: ValueChangedEvent<string>): void {
    if (ev.detail.value) {
      const dateObj = new Date(this.stateObj!.state);
      const newDate = ev.detail.value.split("-").map(Number);
      dateObj.setFullYear(newDate[0], newDate[1] - 1, newDate[2]);

      setDateTimeValue(
        this._api.callService,
        this.stateObj!.entity_id,
        dateObj
      );
    }
  }

  static styles = css`
    :host {
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
    ha-date-input + ha-time-input {
      margin-left: var(--ha-space-1);
      margin-inline-start: var(--ha-space-1);
      margin-inline-end: initial;
    }
  `;
}

declare global {
  interface HTMLElementTagNameMap {
    "more-info-datetime": MoreInfoDatetime;
  }
}
