import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { INews } from '../home/home.page';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
    selector: 'app-home-details',
    templateUrl: './home-details.page.html',
    styleUrls: ['./home-details.page.scss'],
})
export class HomeDetailsPage implements OnInit {
    newsDetails: INews;
    myComment: string = '';

    constructor(
        private route: ActivatedRoute,
        private location: Location,
        private dom:DomSanitizer
    ) { }

    getUrl(url) {
        return this.dom.bypassSecurityTrustResourceUrl(url);
    }

    ngOnInit() {
        this.newsDetails = JSON.parse(this.route.snapshot.paramMap.get('news'));
    }

    back() {
        this.location.back();
    }

    postComment() {
        this.newsDetails.comments.push({
            user: 'Test',
            comment: this.myComment,
            isLiked: false,
            likes: 0,
        });

        this.myComment = '';
    }

    scrollToAddComment() {
        document.querySelector('#comment-input').scrollIntoView({behavior: "smooth", block: "start"});
    }

}
