import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core';
import { DivContentFormComponent } from "./div-help-content-form.component";
import { Subscription } from "rxjs/Subscription";
import { HelpContentService } from "../../services/help-content.service";
import { DivHelpContent } from "../../domain/div-help-content";
import { ActivatedRoute, Router } from "@angular/router";
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';

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

export class EditDivHelpContentComponent implements OnInit, OnDestroy{

    @ViewChild(DivContentFormComponent)
    public formComponent : DivContentFormComponent;

    private communityPid: string;

    private pageId: string;

    private sub: Subscription;

    private divHelpContent: DivHelpContent;

    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 divContentId = params['classContentId'];
            this.communityPid = params['community'];
            this.pageId = params['pageId'];
            this._helpContentService.getDivHelpContent(divContentId as string, this.properties.adminToolsAPIURL).subscribe(
                divHelpContent => this.updateForm(divHelpContent),
                error => this.handleError('System error retrieving class help content', error));
        });
        });
    }
    ngOnDestroy() {
        this.sub.unsubscribe();
    }

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

    getDivId(divId: string) {
       this._helpContentService.getDivId(divId, this.properties.adminToolsAPIURL).subscribe(
          divId => {
            this.formComponent.pageId = divId.page;
            this.formComponent.getDivs(this.formComponent.pageId);
          },
          error => this.handleError('System error retrieving class', error)
       );
    }

    private updateForm(divHelpContent : DivHelpContent) {
        this.divHelpContent = divHelpContent;
        console.info(divHelpContent);

        if(!this.pageId) {
          this.getDivId(divHelpContent.divId as string);
        }
        this.formComponent.myForm.patchValue((divHelpContent));
        // console.log("patching",pageHelpContent);
    }

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

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