/**
 * Created by stefanos on 28/4/2017.
 */
import { Component, ViewChild, Input, Output, EventEmitter, OnInit } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { ActivatedRoute } from "@angular/router";
import { FormGroup } from "@angular/forms";
import { FAQService } from "../services/faq.service";
import { Topic } from "../domain/topic";
import { Question } from "../domain/question";
import { HelpContentService } from "../services/help-content.service";
import { Page } from "../domain/page";
import { Entity } from "../domain/entity";
import { Community } from "../domain/community";
import { DivId } from "../domain/divId";
import { EnvProperties } from '../openaireLibrary/utils/properties/env-properties';

@Component({
    selector: 'modal-form',
    templateUrl: './modal-form.component.html'
})
export class ModalFormComponent {

    constructor(private route: ActivatedRoute, private _faqService : FAQService, private _helpService: HelpContentService){
    }

    @ViewChild('autoShownModal')
    public autoShownModal:ModalDirective;

    @Input()
    public isModalShown:boolean = false;

    @Input()
    public saveText : string;

    @Input()
    public titleText : string;

    @Input()
    public formGroup : FormGroup;

    @Input()
    public type : string = 'topic';

    public errorMessage : string = null;

    @Output() emmitObject: EventEmitter<any> = new EventEmitter();

    @Output() emmitError: EventEmitter<any> = new EventEmitter();
    public properties:EnvProperties = null;

    ngOnInit() {

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

    public showModal():void {
        this.isModalShown = true;
    }

    public hideModal():void {
        this.autoShownModal.hide();
    }

    public onHidden():void {
        this.isModalShown = false;
        this.emmitError.emit(null);
        this.errorMessage = null;
    }

    public saveCustom(obj : any) {
        if (this.type == 'none') {
          this.emmitObject.emit(true);
          this.hideModal();
        } else {
          if(!this.formGroup.valid) {
              this.errorMessage = "Please fill in all required fields marked with *"
          } else {

              if (this.type == 'topic') {
                  this._faqService.saveTopic(<Topic> obj).subscribe(
                      data => this.emmitObject.emit(data),
                      error => this.emmitError.emit(error)
                  );
              } else if (this.type == 'question') {
                  this._faqService.saveQuestion(<Question> obj).subscribe(
                      data => this.emmitObject.emit(data),
                      error => this.emmitError.emit(error)
                  );
              } else if (this.type == 'community') {
                  if(this.saveText == 'Update') {
                    this._helpService.updateCommunity(<Community> obj, this.properties.adminToolsAPIURL).subscribe(
                       data => this.emmitObject.emit(data),
                       error => this.emmitError.emit(error)
                    );
                    console.info(<Community> obj);

                  } else if(this.saveText == 'Save') {
                    this._helpService.saveCommunity(<Community> obj, this.properties.adminToolsAPIURL).subscribe(
                       data => this.emmitObject.emit(data),
                       error => this.emmitError.emit(error)
                    );
                    console.info(<Community> obj);
                  }
              } else if (this.type == 'page') {
                  if(this.saveText == 'Update') {
                    this._helpService.updatePage(<Page> obj, this.properties.adminToolsAPIURL).subscribe(
                        data => this.emmitObject.emit(data),
                        error => this.emmitError.emit(error)
                    );
                  } else if(this.saveText == 'Save') {
                    this._helpService.savePage(<Page> obj, this.properties.adminToolsAPIURL).subscribe(
                        data => this.emmitObject.emit(data),
                        error => this.emmitError.emit(error)
                    );
                  }
              }  else if (this.type == 'entity') {
                  if(this.saveText == 'Update') {
                    this._helpService.updateEntity(<Entity> obj, this.properties.adminToolsAPIURL).subscribe(
                        data => this.emmitObject.emit(data),
                        error => this.emmitError.emit(error)
                    );
                  } else if(this.saveText == 'Save') {
                    this._helpService.saveEntity(<Entity> obj, this.properties.adminToolsAPIURL).subscribe(
                        data => this.emmitObject.emit(data),
                        error => this.emmitError.emit(error)
                    );
                  }
              } else if (this.type == 'divId') {
                  if(this.saveText == 'Update') {
                    this._helpService.updateDivId(<DivId> obj, this.properties.adminToolsAPIURL).subscribe(
                        data => this.emmitObject.emit(data),
                        error => this.emmitError.emit(error)
                    );
                  } else if(this.saveText == 'Save') {
                    this._helpService.saveDivId(<DivId> obj, this.properties.adminToolsAPIURL).subscribe(
                        data => this.emmitObject.emit(data),
                        error => this.emmitError.emit(error)
                    );
                  }
              }
              this.hideModal();
            }
        }
    }
}
