import { Component, ChangeDetectionStrategy, OnInit, OnDestroy, inject, input, output, computed } from '@angular/core';
import { FastButtonComponent } from './fast-button.component';
import { FastWindowComponent } from "./fast-window.component";
import { FastHRComponent } from "./fast-hr.component";
import { DomSanitizer } from '@angular/platform-browser';
import { QueryStringBuilder } from '../utils/queryStringBuilder';
import { FastSVGComponent } from './fast-svg.component';

@Component({
  selector: 'fast-frame',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [FastButtonComponent, FastWindowComponent, FastHRComponent, FastSVGComponent],
  styles: `
    :host { @apply absolute w-full h-full }`
  ,
  template: `
  @if(isWindow()) {
    <fast-window (close)="close()" [height]="height()" [width]="width()" [title]="title()">
        <!-- use an iframe even when you could use a component because kendo window popups inside the componenent don't position correctly otherwise -->
        <!-- this also helps with escape key handling and error encapsulation -->
        <iframe class="flex grow" [src]="sanitizedSrc()"></iframe>
        @if (isCloseVisible()) {
        <fast-hr />
        <div class="flex justify-end">
          <fast-button class="m-2" (click)="close()">
            <fast-svg icon="cancel" />Close
          </fast-button>
        </div>
        }
    </fast-window>
    }
  @else {
    <iframe class="flex grow" [src]="sanitizedSrc()"></iframe>
  }
  `,
})
export class FastFrameComponent implements OnInit, OnDestroy {
  title = input<string>('');
  src = input<string>('');
  externalParams = input<unknown>(null);
  width = input<number>(1800);
  height = input<number>(0);
  isCloseVisible = input<boolean>(true);
  closed = output();
  isWindow = input<boolean>(true);

  sanitizer = inject(DomSanitizer);

  sanitizedSrc = computed(() => {
    let url = this.src();

    if (this.externalParams()) {
      const params = QueryStringBuilder.buildFromObj(this.externalParams());
      url = url + '?' + params.toString();
    }

    const safeUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url);
    return safeUrl;
  });

  // this is needed so we can add and remove the same event listener correctly
  private boundHandleMessage: (event: MessageEvent) => void;

  ngOnInit(): void {
    // console.log('FastFrameComponent.ngOnInit()');
    this.boundHandleMessage = this.handleMessage.bind(this);
    this.addMessageEventListener();
  }

  ngOnDestroy(): void {
    // console.log('FastFrameComponent.ngOnDestroy()');
    this.removeMessageEventListener();
  }

  handleMessage(event: MessageEvent): void {
    // console.log('FastFrameComponent.handleMessage()');
    // console.log(event);
    if (event.data === 'close') {
      // console.log('FastFrameComponent:  window close message received');
      this.close();
    }
  }

  addMessageEventListener(): void {
    // console.log('FastFrameComponent.addMessageEventListener()');
    window.addEventListener('message', this.boundHandleMessage);
  }

  removeMessageEventListener(): void {
    // console.log('FastFrameComponent.removeMessageEventListener()');
    window.removeEventListener('message', this.boundHandleMessage);
  }

  close(): void {
    // console.log('FastFrameComponent.close()');
    this.closed.emit();
  }
}
