import {Component, Input, OnInit} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {Page} from 'src/app/models/Page';
import {Permission} from "../../../models/Permission";
import {PermissionService} from "../../../services/permission.service";
import {MessageService} from "primeng/api";
import { AppService } from 'src/app/services/app.service';
import { Community } from 'src/app/models/Community';
import { UtilsService } from 'src/app/services/utils.service';
import { UserService } from 'src/app/services/user.service';
import { CommunityDTO } from 'src/app/models/enea/LocalUserDTO';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Component({
    selector: 'app-page-properties-editor',
    templateUrl: './page-properties-editor.component.html',
    styleUrls: ['./page-properties-editor.component.css']
})
export class PagePropertiesEditorComponent implements OnInit {

    @Input() page?: Page;
    languages: string[] = [];
    permissions: Permission[] = [];

    communities: Community[] = [];

    community: Community = new Community();

    constructor(
        private app: AppService,
        private translate: TranslateService,
        private permissionService: PermissionService,
        private messageService: MessageService,
        private userService: UserService,
        private utils: UtilsService,
        private http: HttpClient) {
    }

    ngOnInit(): void {
        this.getCommunities();
        this.languages = this.translate.getLangs();
        this.permissionService.getAll().subscribe(
            permissions => {
                this.permissions = permissions;
            },
            error => {
                this.messageService.add({severity: 'error', summary: 'Permissions list', detail: error})
            });
        this.getCommunities().subscribe(
            response => {
              this.communities = response;
            }
        );
    }

    communityChange(id: Number = 0){
        if (id === null) this.page!.communityId = 0;

    }

    
    getCommunities() {
        return this.http.get<Community[]>("lecservice/listCommunities");
          
    }

}
