import { Component, OnInit } from '@angular/core';
import { AnimationController, ModalController, NavParams } from '@ionic/angular';
import { Publication } from 'src/app/models/publication';
import { PublicationsService } from 'src/app/services/publications.service';
import { ToastService } from 'src/app/services/toast.service';
import { ImageModalPage } from '../gallery/image-modal/image-modal.page';
import { NewPublicationPage } from '../new-publication/new-publication.page';
import { Location } from '@angular/common';
import { SqliteService } from 'src/app/services/sqlite.service';
import { BackgroundService } from 'src/app/services/background.service';
import { ThemeService } from 'src/app/services/theme.service'
import { RouteService } from 'src/app/services/route.service';

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

  constructor(
    private modalController: ModalController,
    private navParams:NavParams,
    private publications:PublicationsService,
    private animationController: AnimationController,
    private toastService: ToastService,
    private RouteService: RouteService,
    private sqliteservice: SqliteService,
    private backgroundservice: BackgroundService,
    public ThemeService: ThemeService ) {

      this.publicationId = this.navParams.get('publicationId');
      /* 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.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;
    });
  }

  getFromDB() {

  }

  close() {
    this.RouteService.goBack()

  }

  async deletePost() {

    const loader = this.toastService.loading()

    try {
      await this.publications.DeletePublication(this.folderId, this.publicationId).toPromise();
      this.toastService.successMessage('Publicação eliminado')
      if(window['app-view-publications-page-doRefresh']) {
        window['app-view-publications-page-doRefresh']()
      }
      this.close();
    } catch (error) {
      this.toastService.badRequest('Publicação não eliminado')
    } finally {
      loader.remove()
    }

  }

  async editPost(publicationType:any) {

    console.log(this.publication);
    const modal = await this.modalController.create({
      component: NewPublicationPage,
      componentProps:{
        publicationType: publicationType,
        publication: this.publication,
      },
      cssClass: 'new-publication modal modal-desktop',
      backdropDismiss: false
    });
    await modal.present();
    modal.onDidDismiss().then(()=>{
      setTimeout(() => {
        this.getPublicationDetail();
    }, 5000);
    });
  }

  openPreview(imageUrl:string){
    this.modalController.create({
      component: ImageModalPage,
      componentProps: {
        imageUrl:imageUrl,
      }
    }).then(modal => modal.present());
  }





}
