import { Component, Output, EventEmitter, Input } from '@angular/core';
import { Trait } from 'src/interfaces/traits';
import { ModalService } from 'src/services/modal/modal.service';

@Component({
  selector: 'trait-modal',
  templateUrl: './trait-modal.component.html',
  styleUrls: ['./trait-modal.component.scss'],
})
export class TraitModalComponent {
  @Input() public isUpdate: boolean = false;
  @Input() public trait: Trait | undefined;

  public name: string = '';
  public shortDescription: string = '';
  public longDescription: string = '';
  public origin: string = '';

  public constructor(public modalAccessor: ModalService) {}

  ngOnInit(): void {
    if (this.isUpdate) {
      this.loadItem();
    }
  }

  // FUNCTIONS

  public loadItem(): void {
    this.name = this.trait!.name;
    this.shortDescription = this.trait!.shortDescription;
    this.longDescription = this.trait!.longDescription;
    this.origin = this.trait!.origin;
  }

  public createTrait(): Trait {
    return {
      name: this.name,
      shortDescription: this.shortDescription,
      longDescription: this.longDescription,
      origin: this.origin,
    };
  }

  // RESPONSES

  public cancel(): void {
    this.modalAccessor.handleModalClosing('cancel', undefined);
  }

  public add(): void {
    this.modalAccessor.handleModalClosing('add', this.createTrait());
  }

  public update(): void {
    this.modalAccessor.handleModalClosing('update', this.createTrait());
  }
}
