
import { Component, OnInit, HostListener } from '@angular/core';
import { PDFDocumentProxy, PDFProgressData } from 'ng2-pdf-viewer';
import { HyperService } from '../../core/services/http.service';
import { Router } from "@angular/router";
import { ActivatedRoute } from '@angular/router';
import { DatePipe } from '@angular/common';
import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser';

@Component({
    selector: 'app-content-popup',
    templateUrl: './content-popup.component.html',
    styleUrls: ['./content-popup.component.scss']
})
export class ContentPopupComponent implements OnInit {

    is_modal_visible: boolean = false;
    content_data: any;
    page: number = 1;
    totalPages: number;
    isLoaded: boolean = false;
    is_completed: boolean = false;
    today: any;
    screen_callback: any;
    pdf_zoom: any;
    pdf_preloader: boolean;
    type: any;
    file_url: string = ""; 
    pdf_languages:any=[];
    defaultValue:any; 


    constructor(private server: HyperService, private route: ActivatedRoute, private datePipe: DatePipe, private sanitizer: DomSanitizer) { }

    ngOnInit() {
        var date = new Date();
        this.today = this.datePipe.transform(date, "yyyy-MM-dd"); //output : 2018-02-13
    } 

    showcontentrModal(url: string = "", data: any = {}, callBack: any = null) {
        this.pdf_languages=[]
        this.is_modal_visible = true;
        this.content_data = data;
        this.file_url=this.content_data.list.content_files[0].file_url
        console.log('Received', this.content_data.list)
        this.type = this.content_data.list.content_files[0].filetypeid
        for (let i = 0; i <  this.content_data.list.content_files.length; i++) {
            
            this.pdf_languages.push( this.content_data.list.content_files[i].language_title);
        }
        console.log('pdf_languages',this.pdf_languages)
        this.defaultValue= this.pdf_languages[0];
        this.screen_callback = callBack;
        if (this.content_data.list.content_files[0].filetypeid == 1) {
            this.pdf_preloader = true;
            if (this.content_data.list.status != 2) {
                this.statusUpdate(0);
            }
            this.page = 1;
            this.pdf_zoom = 0.5;
        } 

       else if (this.content_data.list.content_files[0].filetypeid == 2) {
            if (this.content_data.list.status != 2) {
                this.statusUpdate(0);
                setTimeout(() => {
                    document.getElementById('videoID').addEventListener('ended', (e) => {
                        this.statusUpdate(1);
                    })
                }, 150);
            }
        }
    }


    onOptionsSelected(event) { 
        let current_file=this.content_data.list.content_files.filter(item => item.language_title === event)
        this.file_url=current_file[0].file_url;
        this.pdf_preloader = true;
        console.log(event); //option value will be sent as event
    }


    onCloseModal(type) {
        this.is_modal_visible = false;
        this.screen_callback(this.is_completed);
    }

    //Pdf file 
    onScroll(event) {
        console.log('OnScroll')
        let tracker = event.target;
        let limit = tracker.scrollHeight - tracker.clientHeight;
        console.log('OnScroll', event.target.scrollTop)
        console.log('OnScroll', limit - 1)
        if (event.target.scrollTop === limit || event.target.scrollTop > limit || event.target.scrollTop > limit - 1) {
            if (this.content_data.list.status != 2) {
                this.is_completed = true;
                this.statusUpdate(1); //page scroll to bottom
            }
        }
    }

    pdfZoomPlus() {
        this.pdf_zoom += 0.1;
        console.log(this.pdf_zoom)
    }

    pdfZoommMinus() {
        // if (this.pdf_zoom < 0) return;
        if (this.pdf_zoom < 0) {
            console.log("Too Small")
        }
        else {
            this.pdf_zoom -= 0.1;
        }
        console.log(this.pdf_zoom)
    }

    nextPage() {
        this.page++;
        if (this.page == this.totalPages && this.content_data.status != 2) {
            this.is_completed = true;
            this.statusUpdate(1);
        }
    }

    prevPage() {
        this.page--;
    }

    onProgress(progressData: PDFProgressData) {
        console.log(progressData)
        if (progressData.loaded == progressData.total) {
            this.pdf_preloader = false;
        }
    }


    callBackFn(pdf: PDFDocumentProxy) {
        this.totalPages = pdf.numPages;
        this.isLoaded = true;
    }

    pageRendered(e: CustomEvent) {
        this.pdf_preloader = false;
        console.log(e)
    }

    statusUpdate(val) {
        this.server.post("topics/content_status", {
            contentid: this.content_data.list.contentid,
            assettopicid: this.content_data.list.assettopicid,
            status: (val == 0) ? 1 : 2
        }).then((data) => {
            if (data.status == 200 && data.result.response == "success") {
            } else if (data.result.response == "failed") {
            } else if (data.status == 500) { } else {
                alert("Please check your internet connection");
            }
        })
    }

     

}
