import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { AnimationController, ModalController, NavParams } from '@ionic/angular';
import { Publication } from 'src/app/models/publication';
import { ImageModalPage } from 'src/app/pages/publications/gallery/image-modal/image-modal.page';
import { NewPublicationPage } from 'src/app/pages/publications/new-publication/new-publication.page';
import { PublicationsService } from 'src/app/services/publications.service';
import { ToastService } from 'src/app/services/toast.service';
import { BadRequestPage } from 'src/app/shared/popover/bad-request/bad-request.page';
import { SuccessMessagePage } from 'src/app/shared/popover/success-message/success-message.page';
import { ThemeService } from 'src/app/services/theme.service'
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';

@Component({
  selector: 'app-publication-detail-shared',
  templateUrl: './publication-detail.page.html',
  styleUrls: ['./publication-detail.page.scss'],
})
export class PublicationDetailPage implements OnInit {
  showLoader: boolean;
  /* folderId: string; */
  publication: Publication;

  @Input() publicationId: string;
  @Input() folderId: string;
  @Output() addNewPublication =  new EventEmitter<any>();
  @Output() closeDesktopComponent =  new EventEmitter<any>();
  @Output() goBackToViewPublications = new EventEmitter();

  constructor(
    private modalController: ModalController,
    private publications:PublicationsService,
    private animationController: AnimationController,
    private toastService: ToastService,
    public ThemeService: ThemeService
    ) {

      /* this.folderId = this.navParams.get('folderIdId'); */
      this.publication = {
        DateIndex: null,
        DocumentId: '',
        ProcessId:'',
        Title:'',
        Message: '',
        /* image: null, */
        DatePublication: null,
        FileBase64: '',
        OriginalFileName: '',
        FileExtension: '',
      };
    }

  ngOnInit() {
    console.log(this.folderId);

    /* console.log(this.publication.FileBase64); */
    this.getPublicationDetail();
  }
  doRefresh(event) {
    this.getPublicationDetail();

    setTimeout(() => {
      event.target.complete();
    }, 2000);
  }

  getPublicationDetail(){
    this.showLoader = true;
    console.log(this.publicationId);
    /* console.log(this.folderId); */
    this.publications.GetPublicationById(this.publicationId).subscribe(res=>{
      console.log(res);
      /* this.publication = res; */
      this.publication = {
        DateIndex: res.DateIndex,
        DocumentId: res.DocumentId,
        ProcessId:res.ProcessId,
        Title:res.Title,
        Message: res.Message,
        DatePublication: res.DatePublication,
        FileBase64: "data:image/jpg;base64," + res.FileBase64,
        OriginalFileName: res.OriginalFileName,
        FileExtension: 'jpeg',
      }
      this.showLoader = false;
    });
  }

  close() {
    this.modalController.dismiss();
  }

  async deletePost(){

    const laoder = this.toastService.loading()

    try {
      await this.publications.DeletePublication(this.folderId, this.publicationId).toPromise();
      this.toastService._successMessage("Publicação eliminado")
      this.goBackToViewPublications.emit();
    } catch (error) {
      this.toastService._badRequest("Publicação não eliminado")
    } finally {
      laoder.remove()
    }

  }

  async editPost(publicationType:any) {
    console.log(this.publication);

    if(window.innerWidth <= 1024){
      const modal = await this.modalController.create({
        component: NewPublicationPage,
        componentProps:{
          publicationType: publicationType,
          publication: this.publication,
        },
        cssClass: 'new-publication',
        backdropDismiss: false
      });
      await modal.present();
      modal.onDidDismiss().then(()=>{
        setTimeout(() => {
          this.getPublicationDetail();
      }, 5000);
      });
    } else {
      this.addNewPublication.emit({
        publicationType: publicationType,
        folderId: this.folderId,
        publication: this.publication,
      })
    }

  }

  async goBack(){

    this.goBackToViewPublications.emit();
  }

  async openPreview(item) {
    const modal = await this.modalController.create({
      component:  ViewMediaPage,
      cssClass: 'modal modal-desktop',
      componentProps: {
        image: item.FileBase64,
        username: item.Title,
        _updatedAt: item.DatePublication
      }
    });
    modal.present();
  }







}
