import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { GroupBoilerplate } from 'src/app/models/boilerplate.model';
import { BoilerplateService } from 'src/app/services/boilerplate.service';
import { NotifierService } from "angular-notifier";

@Component({
  selector: 'app-add-groupboil',
  templateUrl: './add-groupboil.component.html',
  styleUrls: ['./add-groupboil.component.scss']
})
export class AddGroupboilComponent implements OnInit {
  ontologyList=[]

  groupboilerplate: GroupBoilerplate = {
    title_bgroup: "",
    description: "",
    ontology_file: "/Default",
  };
  submitted = false;
  private readonly notifier: NotifierService;

  constructor(private boilerplatelService: BoilerplateService, private router: Router, notifierService: NotifierService) {
    this.notifier = notifierService;
  }

  ngOnInit(): void {
    this.getOntologies();
  }
  saveBoilerplate(): void {
    const data = {
      title_bgroup: this.groupboilerplate.title_bgroup,
      description: this.groupboilerplate.description ? this.groupboilerplate.description : this.groupboilerplate.title_bgroup,
      ontology_file: '../../Ontologies' + this.groupboilerplate.ontology_file ,
    };
   
    this.submitted = true;
    this.boilerplatelService.creategroupboil(data)
      .subscribe(
        response => {
          this.notifier.notify("success", "Project " + this.groupboilerplate.title_bgroup + ' created');
          this.submitted = false;
          this.router.navigate(['/groupboilerplates']);
        },
        error => {
          console.log(error);
          //error, warning, success, info, default
          this.submitted = false;
          this.notifier.notify("warning", (JSON.stringify(error["error"]["title_bgroup"][0])));
        });
  }
  newBoilerplate(): void {
    this.submitted = false;
    this.groupboilerplate = {
      title_bgroup: "",
      description: "",

    };
  }

  getOntologies(): void {
    this.boilerplatelService.getOntolgies()
      .subscribe(
        data => {
          this.ontologyList=data;
          console.log(data);
        },
        error => {
          console.log(error);
        });
  }

}
