import { mdiFolderUpload } from "@mdi/js";
import type { CSSResultGroup } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { isComponentLoaded } from "../../../../common/config/is_component_loaded";
import {
  fireEvent,
  type HASSDomEvent,
} from "../../../../common/dom/fire_event";
import "../../../../components/ha-alert";
import "../../../../components/ha-button";
import "../../../../components/ha-dialog";
import "../../../../components/ha-dialog-footer";
import "../../../../components/ha-file-upload";
import {
  CORE_LOCAL_AGENT,
  HASSIO_LOCAL_AGENT,
  INITIAL_UPLOAD_FORM_DATA,
  isSupportedBackupFile,
  SUPPORTED_UPLOAD_FORMAT,
  uploadBackup,
  type BackupUploadFileFormData,
} from "../../../../data/backup";
import type { HassDialog } from "../../../../dialogs/make-dialog-manager";
import { DirtyStateProviderMixin } from "../../../../mixins/dirty-state-provider-mixin";
import { haStyle, haStyleDialog } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import { showAlertDialog } from "../../../lovelace/custom-card-helpers";
import type { UploadBackupDialogParams } from "./show-dialog-upload-backup";

@customElement("ha-dialog-upload-backup")
export class DialogUploadBackup
  extends DirtyStateProviderMixin<BackupUploadFileFormData>()(LitElement)
  implements HassDialog<UploadBackupDialogParams>
{
  @property({ attribute: false }) public hass!: HomeAssistant;

  @state() private _params?: UploadBackupDialogParams;

  @state() private _uploading = false;

  @state() private _error?: string;

  @state() private _formData?: BackupUploadFileFormData;

  @state() private _open = false;

  public async showDialog(params: UploadBackupDialogParams): Promise<void> {
    this._params = params;
    this._formData = INITIAL_UPLOAD_FORM_DATA;
    this._open = true;
    this._initDirtyTracking({ type: "shallow" }, INITIAL_UPLOAD_FORM_DATA);
    this._updateDirtyState(this._formData);
  }

  private _dialogClosed() {
    if (this._params!.cancel) {
      this._params!.cancel();
    }
    this._formData = undefined;
    this._params = undefined;
    this._open = false;
    fireEvent(this, "dialog-closed", { dialog: this.localName });
  }

  public closeDialog() {
    this._open = false;
    return true;
  }

  protected render() {
    if (!this._params || !this._formData) {
      return nothing;
    }

    return html`
      <ha-dialog
        .open=${this._open}
        header-title=${this.hass.localize(
          "ui.panel.config.backup.dialogs.upload.title"
        )}
        .preventScrimClose=${this.isDirtyState || this._uploading}
        @closed=${this._dialogClosed}
      >
        ${
          this._error
            ? html`<ha-alert alert-type="error">${this._error}</ha-alert>`
            : nothing
        }
        <ha-file-upload
          .uploading=${this._uploading}
          .icon=${mdiFolderUpload}
          .accept=${SUPPORTED_UPLOAD_FORMAT}
          .localize=${this.hass.localize}
          .label=${this.hass.localize(
            "ui.panel.config.backup.dialogs.upload.input_label"
          )}
          .supports=${this.hass.localize(
            "ui.panel.config.backup.dialogs.upload.supports_tar"
          )}
          @file-picked=${this._filePicked}
          @files-cleared=${this._filesCleared}
        ></ha-file-upload>
        <ha-dialog-footer slot="footer">
          <ha-button
            slot="secondaryAction"
            appearance="plain"
            @click=${this.closeDialog}
            .disabled=${this._uploading}
          >
            ${this.hass.localize("ui.common.cancel")}
          </ha-button>
          <ha-button
            slot="primaryAction"
            @click=${this._upload}
            .disabled=${!this.isDirtyState || this._uploading}
          >
            ${this.hass.localize(
              "ui.panel.config.backup.dialogs.upload.action"
            )}
          </ha-button>
        </ha-dialog-footer>
      </ha-dialog>
    `;
  }

  private _filePicked(ev: HASSDomEvent<{ files: File[] }>) {
    this._error = undefined;
    const file = ev.detail.files[0];

    this._formData = {
      ...this._formData!,
      file,
    };
    this._updateDirtyState(this._formData);
  }

  private _filesCleared() {
    this._error = undefined;
    this._formData = INITIAL_UPLOAD_FORM_DATA;
    this._updateDirtyState(this._formData);
  }

  private async _upload() {
    const { file } = this._formData!;
    if (!file || !isSupportedBackupFile(file)) {
      showAlertDialog(this, {
        title: this.hass.localize(
          "ui.panel.config.backup.dialogs.upload.unsupported.title"
        ),
        text: this.hass.localize(
          "ui.panel.config.backup.dialogs.upload.unsupported.text"
        ),
        confirmText: this.hass.localize("ui.common.ok"),
      });
      return;
    }

    const agentIds = isComponentLoaded(this.hass.config, "hassio")
      ? [HASSIO_LOCAL_AGENT]
      : [CORE_LOCAL_AGENT];

    this._uploading = true;
    try {
      await uploadBackup(this.hass, file, agentIds);
      this._params!.submit?.();
      this._markDirtyStateClean();
      this.closeDialog();
    } catch (err: any) {
      this._error = err.message;
    } finally {
      this._uploading = false;
    }
  }

  static get styles(): CSSResultGroup {
    return [
      haStyle,
      haStyleDialog,
      css`
        ha-alert {
          display: block;
          margin-bottom: var(--ha-space-4);
        }
      `,
    ];
  }
}

declare global {
  interface HTMLElementTagNameMap {
    "ha-dialog-upload-backup": DialogUploadBackup;
  }
}
