import { LitElement, css, html } from "lit";
import { shellApi } from "../../../api/api";

export class PocEventsEcap extends LitElement {
  render() {
    return html`<div>
        <h1>POC ECAP Test </h1>
    <hr>
    <p>Emplenar valors de url i accio per navegar a ECAP</p>
    <form>
        <label for="eventType">Tipus d'event (TipusEvent)</label>
        <input type="text" id="eventType" name="eventType" placeholder="IA_DEV">
        <label for="Accio">Acció a executar (Accio)</label>
        <input type="text" id="accio" name="accio" placeholder="NO_TANCAR">
        <label for="parameterName">Nom del parametre</label>
        <input type="text" id="parameterName" name="parameterName" placeholder="CIP">
        <label for="parameterValue">Valor del parametre</label>
        <input type="text" id="parameterValue" name="parameterValue" placeholder="12345678">

    </form>
    <button @click=${(e) => this.goToLinkOnEcap()}>LLennçar event ecap</button>
    </div>
    
    <form>
        <label for="pdfId">PDF ID</label>
        <input type="text" id="pdfId" name="pdfId" placeholder="unique-id">
        <label for="pdfName">PDF Name</label>
        <input type="text" id="pdfName" name="pdfName" placeholder="pdfName">
        <label for="pdfDate">PDF Date</label>
        <input type="text" id="pdfDate" name="pdfDate" placeholder="2024-01-15">
        <label for="url">URL</label>
        <input type="text" id="url" name="url" placeholder="url">


    </form>
    <button @click=${(e) => this.sendPdfToViewer()}>Add PDF</button>


    <button @click=${(e) => this.getPDFS()}>GET PDFs</button>
    `;
  }

  sendPdfToViewer() {
    const pdfId = (this as any).shadowRoot?.getElementById("pdfId")?.value as string;
    const pdfName = (this as any).shadowRoot?.getElementById("pdfName")?.value as string;
    const pdfDate = (this as any).shadowRoot?.getElementById("pdfDate")?.value as string;
    const fileName = (this as any).shadowRoot?.getElementById("url")?.value as string;

    shellApi.pdfViewerManager.add({ id: pdfId, name: pdfName, date: pdfDate, url: fileName });
  }

  getPDFS() {
    const pdfs = shellApi.pdfViewerManager.getPdfs();
    return pdfs;
  }

  goToLinkOnEcap() {
    const eventType = (this as any).shadowRoot?.getElementById("eventType")?.value;
    const accio = (this as any).shadowRoot?.getElementById("accio")?.value;
    const parameterName = (this as any).shadowRoot?.getElementById("parameterName")?.value;
    const parameterValue = (this as any).shadowRoot?.getElementById("parameterValue")?.value;

    //sending data to parent window if opened inside iframe
    shellApi.ecapEventManager.publish(eventType, accio, {
      [parameterName]: parameterValue,
    });
  }

  static styles = css`
  `;
}
