import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {Observable} 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 { Layout } from "../widgets/Layout";

@Injectable({
    providedIn: 'root'
})
export class PageService {
    constructor(private http: HttpClient) {}

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

    public getUserPages(): Observable<Page[]> {
        return this.http.get<ServiceResponse<any>>("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>>("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>>("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("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("lecservice/page-configuration/" + slug)
        .pipe(
            tap(
                (serviceResponse: ServiceResponse<Configuration>) => {
                    if (!serviceResponse.success)
                    throw new Error(serviceResponse.messages?.join(", "));    
                }),
                map((serviceResponse: ServiceResponse<Configuration>) => {
                    // return serviceResponse.success ? Configuration.fromJson(serviceResponse.result): null;
                    return serviceResponse.success ? serviceResponse.result: null;
                })
        );
    }

    public save(data: Page): Observable<Page> {
        return this.http.post("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("lecservice/admin/pages/delete", data);
    }
}