/**
 * Created by stefania on 7/14/17.
 */
import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core';
import { PageContentFormComponent } from "./page-help-content-form.component";
import { Subscription } from "rxjs/Subscription";
import { HelpContentService } from "../../services/help-content.service";
import { PageHelpContent } from "../../domain/page-help-content";
import { ActivatedRoute, Router } from "@angular/router";
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';

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

export class EditPageHelpContentComponent implements OnInit, OnDestroy{

    @ViewChild(PageContentFormComponent)
    public formComponent : PageContentFormComponent;

    private communityPid: string;

    private pageId: string;

    private sub: Subscription;

    private pageHelpContent: PageHelpContent;

    private errorMessage : string = null;
    public properties:EnvProperties = null;

    constructor(
        private route: ActivatedRoute,
        private router: Router,
        private _helpContentService: HelpContentService) {}

    ngOnInit() {
      this.route.data
        .subscribe((data: { envSpecific: EnvProperties }) => {
           this.properties = data.envSpecific;
           this.sub = this.route.queryParams.subscribe(params => {
             //let id = params['id'];
             let pageContentId = params['pageContentId'];
             this.communityPid = params['community'];
             this.pageId = params['pageId'];
             this._helpContentService.getPageHelpContent(pageContentId as string, this.properties.adminToolsAPIURL).subscribe(
               pageHelpContent => this.updateForm(pageHelpContent),
               error => this.handleError('System error retrieving page help content', error));
             });
      });
    }
    ngOnDestroy() {
        this.sub.unsubscribe();
    }

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

    private updateForm(pageHelpContent : PageHelpContent) {
        this.pageHelpContent = pageHelpContent;
        console.info(pageHelpContent);
        this.formComponent.myForm.patchValue((pageHelpContent));
        // console.log("patching",pageHelpContent);
    }

    private saveCustom() {
        if(this.formComponent.myForm.valid) {
            let pageHelpContent : PageHelpContent = this.formComponent.myForm.value;
            console.info(pageHelpContent.community);
            this._helpContentService.updatePageHelpContent(pageHelpContent, this.properties.adminToolsAPIURL).subscribe(
                _ => {
                  if(this.pageId) {
                    this.router.navigate( ['/pageContents/'], { queryParams: { "community": this.communityPid, "page": this.pageId  } } );
                  } else {
                    this.router.navigate(['/pageContents']);
                  }
                },
                err => this.handleError('System error updating page content', err)
            );
        } else {
            this.errorMessage = "Please fill all required fields";
        }
    }

    private cancelCustom() {
      if(this.pageId) {
        this.router.navigate( ['/pageContents/'], { queryParams: { "community": this.communityPid, "page": this.pageId  } } );
      } else {
        this.router.navigate(['/pageContents']);
      }
    }
}
