import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { UserService } from '../services/user.service';
import { NewsService, IMAGE_BASE_URL } from '../services/news.service';
import { Platform } from '@ionic/angular';
import { Router } from '@angular/router';

@Component({
    selector: 'app-bookmarks',
    templateUrl: './bookmarks.page.html',
    styleUrls: ['./bookmarks.page.scss'],
})
export class BookmarksPage implements OnInit {
    bookmarked_articles: Array<{
        title: string,
        alias: string,
        image: string,
        type: string,
    }> = [];

    image_url = IMAGE_BASE_URL;

    constructor(
        private location: Location,
        private userService: UserService,
        private newsService: NewsService,
        private router: Router,
        private platform: Platform,
    ) { }

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

    getType(type: string) {
        let returnType: string = '';

        console.log(type);

        switch(type) {
            case 'Photos': returnType = 'gallery'; break;
            case 'Videos': returnType = 'videos'; break;
            case 'News': returnType = 'news'; break;
        }

        return returnType;
    }

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

    ngOnInit() {
        this.userService.getUserProfile().then((data: any) => {            
            let bookmarked_articles: Array<string> = data.bookmarked_articles;
            this.bookmarked_articles = [];

            for (let i = 0; i < bookmarked_articles.length; i += 1) {
                
                this.newsService.getArticleDetails(bookmarked_articles[i]).then((info: any) => {

                    if (this.platform.is('android') || this.platform.is('capacitor')) {
                        this.bookmarked_articles.push({
                            title: JSON.parse(info.data)['content'].data.short_title ? JSON.parse(info.data)['content'].data.short_title : JSON.parse(info.data)['content'].data.asset_title,
                            alias: bookmarked_articles[i],
                            image: this.image_url + JSON.parse(info.data)['content'].data.image_path + JSON.parse(info.data)['content'].data.image_file_name,
                            type: this.getType(JSON.parse(info.data)['EntityData'].entities[0].ent_display_name)
                        });

                    } else {
                        this.bookmarked_articles.push({
                            title: info.content.data.short_title ? info.content.data.short_title : info.content.data.asset_title,
                            alias: bookmarked_articles[i],
                            image: this.image_url + info.content.data.image_path + info.content.data.image_file_name,
                            type: this.getType(info.EntityData.entities[0].ent_display_name)
                        });
                    }

                    
                }, (e) => {
                    // alert(JSON.stringify(e));
                });
            }

        }, () => {
            this.bookmarked_articles = [];
        });
    }

}
