import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable({
    providedIn: 'root'
})
export class UserService {
    BASE_URL: string = 'https://fan-engagement.techmahindra.com';

// /like-article/:id
// /bookmark-article/:id

    constructor(
        private http: HttpClient
    ) { }

    likePost(postId: string) {
        const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + JSON.parse(localStorage.getItem('FBToken')).token
			})
		};

        return this.http.get(this.BASE_URL + '/like-article/' + postId, httpOptions).toPromise();
    }

    dislikePost(postId: string) {
        const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + JSON.parse(localStorage.getItem('FBToken')).token
			})
		};

        return this.http.get(this.BASE_URL + '/unlike-article/' + postId, httpOptions).toPromise();
    }

    BookmarkPost(postId: string) {
        const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + JSON.parse(localStorage.getItem('FBToken')).token
			})
		};

        return this.http.get(this.BASE_URL + '/bookmark-article/' + postId, httpOptions).toPromise();
    }

    UnBookmarkPost(postId: string) {
        const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + JSON.parse(localStorage.getItem('FBToken')).token
			})
		};

        return this.http.get(this.BASE_URL + '/unbookmark-article/' + postId, httpOptions).toPromise();
    }

    getUserProfile() {
        if (localStorage.getItem('FBToken')) {
            const httpOptions = {
                headers: new HttpHeaders({
                    'Access-Control-Allow-Origin': '*',
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer ' + JSON.parse(localStorage.getItem('FBToken')).token
                })
            };
            
            return this.http.get(this.BASE_URL + '/user-details/', httpOptions).toPromise();
        } else {
            return Promise.reject(null);
        }
    }
    
}
