import { Injectable } from '@angular/core';
import { Headers, Http, RequestOptions, Response, ResponseContentType } from '@angular/http';

import { Observable } from 'rxjs/Observable';

import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

import { IFollows } from './account.model';

import { TokenService } from '../token/token.service';

import { IApiError, IApiResponseError, IApiResponseSuccess, isApiResponseError } from '../token/apiresponse.model';

import { ErrorObservable } from 'rxjs/observable/ErrorObservable';

@Injectable()
export class AccountService {
    constructor(private http: Http, private tokenService: TokenService) { }
    addFollow(userId: number): Observable<IFollows> {
        const headers: Headers = new Headers({
            'Content-Type': 'application/json',
            Authorization: this.tokenService.authorization
        });
        const options: RequestOptions = new RequestOptions({ headers });
        const req: Observable<Response> = this.http.post(`${SETTINGS.API_URL}/account/follows/${userId}/add`, {}, options);
        return req.map((response: Response) => {
            const data: IApiResponseError | IApiResponseSuccess<IFollows> = response.json();
            if (isApiResponseError(data)) {
                throw Observable.throw(data.error);
            }
            return data.result;
        }).catch(this.handleError);
    }

    isFollowed(userId: number): Observable<boolean> {
        const headers: Headers = new Headers({
            'Content-Type': 'application/json',
            Authorization: this.tokenService.authorization
        });
        const options: RequestOptions = new RequestOptions({ headers });
        const req: Observable<Response> = this.http.get(`${SETTINGS.API_URL}/account/follows/get`, options);
        return req.map((response: Response) => {
            const data: IApiResponseError | IApiResponseSuccess<{items: Array<number>}> = response.json();
            if (isApiResponseError(data)) {
                throw Observable.throw(data.error);
            }
            return (data.result.items.find(item => item === userId));
        }).catch((error) => {
            console.log(error);
            return Observable.throw(error.msg || error);
        });
    }

    removeFollow(userId: number): Observable<IFollows> {
        const headers: Headers = new Headers({
            'Content-Type': 'application/json',
            Authorization: this.tokenService.authorization
        });
        const options: RequestOptions = new RequestOptions({ headers });
        const req: Observable<Response> = this.http.post(`${SETTINGS.API_URL}/account/follows/${userId}/remove`, {}, options);
        return req.map((response: Response) => {
            const data: IApiResponseError | IApiResponseSuccess<IFollows> = response.json();
            if (isApiResponseError(data)) {
                throw Observable.throw(data.error);
            }
            return data.result;
        }).catch(this.handleError);
    }

    // getAvatar(): Observable<Blob> {
    //     const headers: Headers = new Headers({
    //         // 'Content-Type': 'application/json',
    //         Authorization: this.tokenService.authorization
    //     });
    //     const options: RequestOptions = new RequestOptions({ headers, responseType: ResponseContentType.Blob });
    //     const req: Observable<Response> = this.http.get(`${SETTINGS.API_URL}/account/avatar`, options);
    //     return req.map((response: Response) => {
    //         const data: IApiResponseError | IApiResponseSuccess<Blob> = {result: new Blob([response.blob()], {type: 'image/png'})}; // new Blob([response], { type: 'image/png' })}; // response.json();
    //         if (isApiResponseError(data)) {
    //             return Observable.throw(data.error);
    //         }
    //         return data.result;
    //     }).catch((error: IApiError) => {
    //         console.log(error);
    //         return Observable.throw(error.msg || error);
    //     });
    // }
    private handleError(error: IApiError): Observable<IFollows> {
        console.log(error);
        return Observable.throw(error.msg || error);
    }
}
