import { consume, type ContextType } from "@lit/context";
import { mdiStop } from "@mdi/js";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map";
import { consumeLocalize } from "../common/decorators/consume-context-entry";
import { computeCloseIcon, computeOpenIcon } from "../common/entity/cover_icon";
import { supportsFeature } from "../common/entity/supports-feature";
import type { LocalizeFunc } from "../common/translations/localize";
import { apiContext } from "../data/context";
import type { CoverEntity } from "../data/cover";
import { canClose, canOpen, canStop, CoverEntityFeature } from "../data/cover";
import "./ha-icon-button";

@customElement("ha-cover-controls")
class HaCoverControls extends LitElement {
  @state() @consumeLocalize() private _localize!: LocalizeFunc;

  @consume({ context: apiContext, subscribe: true })
  private _api!: ContextType<typeof apiContext>;

  @property({ attribute: false }) public stateObj!: CoverEntity;

  protected render() {
    if (!this.stateObj) {
      return nothing;
    }

    return html`
      <div class="state">
        <ha-icon-button
          class=${classMap({
            hidden: !supportsFeature(this.stateObj, CoverEntityFeature.OPEN),
          })}
          .label=${this._localize("ui.card.cover.open_cover")}
          @click=${this._onOpenTap}
          .disabled=${!canOpen(this.stateObj)}
          .path=${computeOpenIcon(this.stateObj)}
        >
        </ha-icon-button>
        <ha-icon-button
          class=${classMap({
            hidden: !supportsFeature(this.stateObj, CoverEntityFeature.STOP),
          })}
          .label=${this._localize("ui.card.cover.stop_cover")}
          .path=${mdiStop}
          @click=${this._onStopTap}
          .disabled=${!canStop(this.stateObj)}
        ></ha-icon-button>
        <ha-icon-button
          class=${classMap({
            hidden: !supportsFeature(this.stateObj, CoverEntityFeature.CLOSE),
          })}
          .label=${this._localize("ui.card.cover.close_cover")}
          @click=${this._onCloseTap}
          .disabled=${!canClose(this.stateObj)}
          .path=${computeCloseIcon(this.stateObj)}
        >
        </ha-icon-button>
      </div>
    `;
  }

  private _onOpenTap(ev): void {
    ev.stopPropagation();
    this._api.callService("cover", "open_cover", {
      entity_id: this.stateObj.entity_id,
    });
  }

  private _onCloseTap(ev): void {
    ev.stopPropagation();
    this._api.callService("cover", "close_cover", {
      entity_id: this.stateObj.entity_id,
    });
  }

  private _onStopTap(ev): void {
    ev.stopPropagation();
    this._api.callService("cover", "stop_cover", {
      entity_id: this.stateObj.entity_id,
    });
  }

  static styles = css`
    .state {
      white-space: nowrap;
    }
    .hidden {
      visibility: hidden !important;
    }
  `;
}

declare global {
  interface HTMLElementTagNameMap {
    "ha-cover-controls": HaCoverControls;
  }
}
