import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AnimationController, ModalController } 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 { ThemeService } from 'src/app/services/theme.service'
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';
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;
  isModal = false

  constructor(
    private modalController: ModalController,
    private publications:PublicationsService,
    private animationController: AnimationController,
    private toastService: ToastService,
    private activatedRoute: ActivatedRoute,
    private router: Router,
    private RouteService: RouteService,
    public ThemeService: ThemeService
    ) {

      this.activatedRoute.paramMap.subscribe(params => {
        console.log(params);
        if(params["params"]) {
          this.folderId = params["params"].folderId;
          this.publicationId = params["params"].publicationId;
          // console.log(params["params"]);
        }
      });

      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;
    });
  }

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

  goBack() {
    if(this.isModal) {
      this.close()
    } else {
      // alert('go back')
      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 eliminada')

      if(window['app-view-publications-page-doRefresh']) {
        window['app-view-publications-page-doRefresh']()
      }

      this.goBack();
    } catch (error) {
      this.toastService.badRequest('Publicaçao não eliminada')
    }  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,
        folderId: this.folderId
      },
      cssClass: 'new-publication modal modal-desktop',
      backdropDismiss: false
    });
    await modal.present();
    modal.onDidDismiss().then(()=>{
      setTimeout(() => {
        this.getPublicationDetail();
    }, 5000);
    });
  }

  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();
  }






}
