import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {Observable, Subject} from "rxjs";
import {PagedResponse, ServiceResponse} from "../models/ServiceResponse";
import {LazyLoadEvent} from "primeng/api";
import {map, tap} from "rxjs/operators";
import {Configuration, Page} from "../models/Page";
import {environment} from "../../environments/environment";

@Injectable({
    providedIn: 'root'
})
export class PageService {
    private pageDetailSubject: Subject<any> = new Subject<any>();
    constructor(private http: HttpClient) {}

    public getData(data: LazyLoadEvent): Observable<ServiceResponse<PagedResponse>> {
        return this.http.post(environment.baseUrl + "/lecservice/admin/pages/list", data);
    }

    public getUserPages(): Observable<Page[]> {
        return this.http.get<ServiceResponse<any>>(environment.baseUrl + "/lecservice/pages")
        .pipe(
            tap(
                (serviceResponse: ServiceResponse<Page[]>) => {
                    if (!serviceResponse.success)
                    throw new Error(serviceResponse.messages?.join(", "));    
                }),
                map((serviceResponse: ServiceResponse<Page[]>) => {
                    return serviceResponse.success ? serviceResponse.result: [];
                })
        );
    }

    public getUserPagesWithCommunityId(): Observable<Page[]> {
        return this.http.get<ServiceResponse<any>>(environment.baseUrl + "/lecservice/pagesInCommunity?communityid=")
        .pipe(
            tap(
                (serviceResponse: ServiceResponse<Page[]>) => {
                    if (!serviceResponse.success)
                    throw new Error(serviceResponse.messages?.join(", "));    
                }),
                map((serviceResponse: ServiceResponse<Page[]>) => {
                    return serviceResponse.success ? serviceResponse.result: [];
                })
        );
    }


    public getAllPages(): Observable<Page[]> {
        return this.http.get<ServiceResponse<any>>(environment.baseUrl + "/lecservice/admin/pages/all")
        .pipe(
            tap(
                (serviceResponse: ServiceResponse<Page[]>) => {
                    if (!serviceResponse.success)
                    throw new Error(serviceResponse.messages?.join(", "));    
                }),
                map((serviceResponse: ServiceResponse<Page[]>) => {
                    return serviceResponse.success ? serviceResponse.result: [];
                })
        );
    }

    getPage(id: string): Observable<Page> {
        return this.http.get(environment.baseUrl + "/lecservice/admin/pages/" + id)
        .pipe(
            tap(
                (serviceResponse: ServiceResponse<Page>) => {
                    if (!serviceResponse.success)
                    throw new Error(serviceResponse.messages?.join(", "));    
                }),
                map((serviceResponse: ServiceResponse<Page>) => {
                    return serviceResponse.success ? new Page(serviceResponse.result): new Page();
                })
        );
    }

    getPageConfiguration(slug: string): Observable<Configuration> {
        return this.http.get(environment.baseUrl + "/lecservice/page-configuration/" + slug)
        .pipe(
            tap(
                (serviceResponse: ServiceResponse<Configuration>) => {
                    if (!serviceResponse.success) {
                        this.pageDetailSubject.next(serviceResponse.result);

                        throw new Error(serviceResponse.messages?.join(", "));
                    }
                    else {
                        this.pageDetailSubject.next(null);
                    }
                }),
                map((serviceResponse: ServiceResponse<Configuration>) => {
                    // return serviceResponse.success ? Configuration.fromJson(serviceResponse.result): null;
                    this.pageDetailSubject.next(null);
                    return serviceResponse.success ? serviceResponse.result: null;
                })
        );
    }

   /* getPageConfiguration(slug: string) {
        this.http.get(environment.baseUrl + "lecservice/page-configuration/" + slug).subscribe(
            (serviceResponse: any) => {
                if (serviceResponse.success) {
                    // this.pageDetailSubject.next(Configuration.fromJson(serviceResponse.result))
                    this.pageDetailSubject.next(serviceResponse.result);
                } else {
                    this.pageDetailSubject.next(null)
                }
            });
    }*/

    public save(data: Page): Observable<Page> {
        return this.http.post(environment.baseUrl + "/lecservice/admin/pages/save", data)
        .pipe(
            tap(
                (serviceResponse: ServiceResponse<Page>) => {
                    if (!serviceResponse.success)
                    throw new Error(serviceResponse.messages?.join(", "));    
                }),
                map((serviceResponse: ServiceResponse<Page>) => {
                    return serviceResponse.success ? serviceResponse.result: null;
                })
        );
    }

    public deletePage(data: any): Observable<ServiceResponse<boolean>> {
        return this.http.post(environment.baseUrl + "/lecservice/admin/pages/delete", data);
    }
}