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


@Component({
  selector: 'app-add-boilerplate',
  templateUrl: './add-boilerplate.component.html',
  styleUrls: ['./add-boilerplate.component.scss']
})
export class AddBoilerplateComponent implements OnInit {
  boilerplate: Boilerplate = {
    group_of_boilerplate: this.route.snapshot.params.gb,
    title: "",
    has_prefix: false,
    has_main: true,
    has_suffix: false
  };
  submitted = false;
  private readonly notifier: NotifierService;

  searchText = '';

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

  ngOnInit(): void {
  }

  saveBoilerplate(): void {
    const data = {
      group_of_boilerplate: this.route.snapshot.params.gb,
      title: this.boilerplate.title,
      has_prefix: this.boilerplate.has_prefix,
      has_main: this.boilerplate.has_main,
      has_suffix: this.boilerplate.has_suffix
    };
    if (data.title != "") {
      data.title = data.title?.replace(/[^\w\s]/gi, ' ')
      data.title = data.title?.replace(/ /g, "_")
    }

    this.boilerplatelService.create(this.route.snapshot.params.gb, data)
      .subscribe(
        response => {
          this.notifier.notify("success", "Requirement " + this.boilerplate.title + " created");
          this.submitted = true;
          this.router.navigate(['/groupboilerplates/' + this.route.snapshot.params.gb + '/boilerplates/details/' + response.id]);
        },
        error => {
          console.log(error);
          //error, warning, success, info, default
          this.notifier.notify("warning", (JSON.stringify(error["error"]["title"][0])));
        });
  }

  newBoilerplate(): void {
    this.submitted = false;
    this.boilerplate = {
      group_of_boilerplate: this.route.snapshot.params.gb,
      title: "",
      has_prefix: false,
      has_main: false,
      has_suffix: false
    };
  }

  prefixs = [
    { name: 'P1', details: "If/Unless <logical expression>", explanation: 'Expresses a logical condition. Paraphrases: in case, provided that, on condition that.' },
    { name: 'P2', details: "As soon as <occuring functionality>", explanation: 'Expresses a temporal stimulation concerned with the point in time of an completed occuring functionality. Paraphrases: in the moment, immediately, once.' },
    { name: 'P3', details: "As long as <occuring functionality>", explanation: 'Expresses a temporal condition concerned with a period. The prefix clause and the main clause take place simultaneously. Paraphrases: meanwhile' },
  ];

  mains = [
    { name: 'M1', details: "system/function shall [not] set [<quantifier>] item [to stateValue]", explanation: 'Sets item Paraphrases: initiate, reset, update.' },
    { name: 'M2', details: "system/function shall [not] set state to stateValue", explanation: 'Sets state to value Paraphrases: initiate, reset, update.' },
    { name: 'M3', details: "system/function shall [not] send [<quantifier>] item", explanation: 'Sends item Paraphrases: forward.' },
    { name: 'M4', details: "system/function shall [not] receive [<quantifier>] item", explanation: 'Receives item Paraphrases: accept, acquire.' },
    { name: 'M5', details: "system/function shall [not] ingest flow", explanation: 'Ingests flow. Paraphrases: get.' },
    { name: 'M6', details: "system/function shall [not] emit flow", explanation: 'Emits flow. Paraphrases: produce.' },
    { name: 'M7', details: "system shall [not] perform function", explanation: 'System requirement. Paraphrases: execute.' },
    { name: 'M8', details: "function shall [not] invoke function", explanation: 'Function invokes another function. Paraphrases: call, request' },
    { name: 'M9', details: "system shall [not] present interface [to system]", explanation: 'Presents interface. Paraphrases: provide' },
    { name: 'M10', details: "system/interface shall [not] transfer flow/item", explanation: 'Transfers TraversingConcept. Paraphrases: transmit, broadcast' },
    { name: 'M11', details: "system shall [not] interact with system [using <connection>] [as defined in <text>]", explanation: 'System requirement expressing interface interraction' },
    { name: 'M12', details: "system shall [not] have state state [with values stateSet]", explanation: 'System requirement expressing system modes' },
    { name: 'M13', details: "system state stateValue shall [not] have substate state [with values stateSet]", explanation: 'System requirement expressing system sub modes per mode' },
    { name: 'M14', details: "system item shall [not] take values from stateSet", explanation: 'System requirement expressing item stateSet' },
    { name: 'M15', details: "system item shall [not] be composed from item", explanation: 'System requirement expressing item decomposition stateSet' },
    { name: 'M16', details: "system shall [not] contain system", explanation: 'System decomposition.' },
  ];

  suffixs = [
    { name: 'S1', details: "<numerical-afirmative> j <closed-interval> [per <time-unit>]", explanation: '' },
    { name: 'S2', details: "after/before flow", explanation: '' },
    { name: 'S3', details: "[every/for a period of/within/for at least] <number> <time-unit> [from flow]", explanation: '' },
    { name: 'S4', details: "at the beginning/at the end", explanation: '' },
    { name: 'S5', details: "at even intervals", explanation: '' },
    { name: 'S6', details: "sequentially", explanation: '' },
  ];

  systems = [{ pos: "Prefix", data: this.prefixs }, { pos: "Main", data: this.mains }, { pos: "Suffix", data: this.suffixs }]

}
