import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
import { SafeHtmlPipe } from '../../openaireLibrary/utils/pipes/safeHTML.pipe';

@Component({
    selector: 'html-page-content-form',
    templateUrl: './html-page-content-form.component.html',
})

export class HtmlPageContentFormComponent implements OnInit{

    @Input('group')
    myForm: FormGroup;
    @Input('communityPid')
    communityPid: string;
    @Input('pageId')
    pageId: string;
    //@Input('mode')
    //mode: string;
    @ViewChild('myeditor') public myeditor;

    public properties:EnvProperties = null;

    public errorMessage: string = '';
    @Input() updateErrorMessage: string = '';

    constructor(private route: ActivatedRoute,private _fb: FormBuilder){}

    ngOnInit() {
        this.myForm = this.form;
        this.route.data
          .subscribe((data: { envSpecific: EnvProperties }) => {
             this.properties = data.envSpecific;
        });
    }

    public get form() {
        return this._fb.group({
            page : [this.pageId,Validators.required],
            community : this.communityPid,
            content : ['', Validators.required],
            _id : '',
        });
    }

    public reset() {
        this.myForm.patchValue({
            page : '',
            community : this.communityPid,
            content : '',
            _id : ''
        });
        this.myForm.markAsPristine();
    }

    handleError(message: string, error) {
        this.errorMessage = message;
        console.log('Server responded: ' + error);
    }
}
