
import { Router } from '@angular/router';
import { Component, OnInit, Input, Output, EventEmitter, inject } from '@angular/core';
import { pdfDefaultOptions, NgxExtendedPdfViewerModule } from 'ngx-extended-pdf-viewer';

import { UserService } from '../../../providers/user/user.service.';

@Component({
  standalone: true,
  selector: 'view-document',
  templateUrl: './viewdocument.component.html',
  imports: [NgxExtendedPdfViewerModule]
})
export class ViewDocumentComponent implements OnInit
{
  private readonly service = inject(UserService);

  @Input() public fromParent: { reportId: number; txnId: number; fileName: string; data: any; cashPurchase: boolean };
  @Output() public okayClicked: EventEmitter<boolean> = new EventEmitter();

  @Input() set showDownloadButton(value: boolean) { this.viewDownloadButton = value; }
  @Input() set setHeight(value: number) { value = Math.max(value, 370); this.height = (value - 110) + 'px'; };

  public errors: string = '';
  public base64Data: string = '';
  public height: string = '460px';
  public viewDownloadButton: boolean = true;

  constructor()
  {
    const router = inject(Router);

    pdfDefaultOptions.assetsFolder = 'bleeding-edge';
    if (!this.service.currentUserValue)
    {
      router.navigate(['/login']);
    }
  }

  ngOnInit(): void
  {
    this.errors = '';
    const txnId: number = this.fromParent.txnId || 0;
    const data: string = (this.fromParent.data || '');
    const reportId: number = this.fromParent.reportId || 0;
    const fileName: string = (this.fromParent.fileName || '');
    const cashPurchase: boolean = this.fromParent.cashPurchase || false;

    if (reportId && reportId > 0 && txnId && txnId > 0)
    {
      if (reportId === 14)
      {
        this.service.printWO(txnId).subscribe(
          {
            next: (resp: any) =>
            {
              if (resp.returnStatus === 200)
              {
                this.base64Data = resp.value;
              }
            },
            error: (error: string) =>
            {
              this.errors = error;
              this.height = '452px';
            }
          });
      }
      else if (reportId === 13)
      {
        this.service.printPO(txnId).subscribe(
          {
            next: (resp: any) =>
            {
              if (resp.returnStatus === 200)
              {
                this.base64Data = resp.value;
              }
            },
            error: (error: string) =>
            {
              this.errors = error;
              this.height = '452px';
            }
          });
      }
      else if (reportId === 12)
      {
        this.service.printInvoice(txnId).subscribe(
          {
            next: (resp: any) =>
            {
              if (resp.returnStatus === 200)
              {
                this.base64Data = resp.value;
              }
            },
            error: (error: string) =>
            {
              this.errors = error;
              this.height = '452px';
            }
          });
      }
      else if (reportId === 11)
      {
        this.service.printVoucher(txnId).subscribe(
          {
            next: (resp: any) =>
            {
              if (resp.returnStatus === 200)
              {
                this.base64Data = resp.value;
              }
            },
            error: (error: string) =>
            {
              this.errors = error;
              this.height = '452px';
            }
          });
      }
      else
      {
        this.service.printFaReport(reportId, txnId, cashPurchase).subscribe(
          {
            next: (resp: any) =>
            {
              if (resp.returnStatus === 200)
              {
                this.base64Data = resp.value;
              }
            },
            error: (error: string) =>
            {
              this.errors = error;
              this.height = '452px';
            }
          });
      }
    }
    else if (fileName && fileName.length > 0)
    {
      this.service.getDocument(fileName).subscribe(
        {
          next: (resp: any) =>
          {
            if (resp.returnStatus === 200)
            {
              this.base64Data = resp.value;
            }
          },
          error: (error: string) =>
          {
            this.errors = error;
            this.height = '452px';
          }
        });
    }
    else if (data && data.length > 100)
    {
      this.base64Data = data;
    }
  }

  public okay(): void
  {
    this.okayClicked.emit(true);
  }

  public close(): void
  {
    this.okayClicked.emit(false);
  }
}

