import { Component, OnInit, ViewChild } from '@angular/core';
import { IonSlides } from '@ionic/angular';
import { Router } from '@angular/router';
import { NewsService, IMAGE_BASE_URL } from '../services/news.service';
import { ToastService } from '../services/toast.service';
import { DomSanitizer } from '@angular/platform-browser';
import { SocialSharing } from '@ionic-native/social-sharing/ngx';
import { Platform } from '@ionic/angular';
import { UserService } from '../services/user.service';

@Component({
    selector: 'app-home',
    templateUrl: './home.page.html',
    styleUrls: ['./home.page.scss'],
})

export class HomePage implements OnInit {
    @ViewChild('slides') slides: IonSlides;
    
    selectedTab: 'news' | 'videos' | 'gallery' = 'news';

    slideOpts = {
        slidesPerView: 1.5,
        spaceBetween: 15,
        initialSlide: 0,
        centeredSlides: true,
        // simulateTouch: false,
        // followFinger: false,
    };

    newsData = [];
    videoData = [];
    galleryData = [];

    showChat: boolean = false;

    image_url = IMAGE_BASE_URL;
    myuptime: string;

    liked_articles: Array<string> = [];
    bookmarked_articles: Array<string> = [];

    constructor(
        private router: Router,
        private newsService: NewsService,
        private toastService: ToastService,
        private dom: DomSanitizer,
        private socialSharing: SocialSharing,
        private platform: Platform,
        private userService: UserService
    ) { }

    transformYourHtml(htmlTextWithStyle) {
        return this.dom.bypassSecurityTrustHtml(htmlTextWithStyle += '<style type="text/css">p{ margin: 0px; }</style>');
    }

    share(message: string, image?: string, url?: string) {
        this.socialSharing.share(message, '', image ? image : '', url? url : '');
    }

    toggleLike(alias: string) {
        if(this.liked_articles.includes(alias)) {
            this.liked_articles.splice(this.liked_articles.indexOf(alias), 1);
            this.userService.dislikePost(alias);
        } else {
            this.liked_articles.push(alias);
            this.userService.likePost(alias);
        }
    }

    toggleBookmark(alias: string) {
        if(this.bookmarked_articles.includes(alias)) {
            this.bookmarked_articles.splice(this.bookmarked_articles.indexOf(alias), 1);
            this.userService.UnBookmarkPost(alias);
        } else {
            this.bookmarked_articles.push(alias);
            this.userService.BookmarkPost(alias);
        }
    }

    isArticleLiked(alias: string) {        
        return this.liked_articles.includes(alias);
    }

    isPostBookmarked(alias: string) {
        return this.bookmarked_articles.includes(alias);
    }

    ngOnInit() {
        this.newsService.getArticles().then((data: any) => {
            // alert(JSON.stringify());

            if (this.platform.is('android') || this.platform.is('capacitor')) {
                this.newsData = JSON.parse(data.data)['content'].items;
            } else {
                this.newsData = data.content.items;
            }

        }, (err) => {
            console.log(err);
            this.toastService.presentToastWithOptions("Failed to get News data", "danger");
        });

        this.newsService.getVideos().then((data: any) => {
            
            if (this.platform.is('android') || this.platform.is('capacitor')) {
                this.videoData = JSON.parse(data.data)['content'].items;
            } else {
                this.videoData = data.content.items;
            }
            
        }, (err) => {
            this.toastService.presentToastWithOptions("Failed to get Videos data", "danger");
        });

        this.newsService.getGalleries().then((data: any) => {
            
            if (this.platform.is('android') || this.platform.is('capacitor')) {
                this.galleryData = JSON.parse(data.data)['content'].items;
            } else {
                this.galleryData = data.content.items;
            }

        }, (err) => {
            console.log(err);
            this.toastService.presentToastWithOptions("Failed to get Gallery data", "danger");
        });

        this.userService.getUserProfile().then((data: any) => {
            this.liked_articles = data.liked_articles;
            this.bookmarked_articles = data.bookmarked_articles;
        }, () => {
            this.liked_articles = [];
            this.bookmarked_articles = [];
        });
    }    

    ionViewDidEnter() {
        if (localStorage.isPartyChatOn === 'yes') {
            this.showChat = true;
        }
    }
    
    getIndex(e: any) {
        console.log(this.slides);
    }

    showNewsDetails(alias_title: string) {
        this.router.navigate(['/home-details', { alias_title: alias_title, type: this.selectedTab }]);
    }

    
}
