import { Component } from '@angular/core';
import { DataService } from 'src/services/data/data.service';
import { ListService } from 'src/services/list/list.service';

@Component({
  selector: 'general',
  templateUrl: './general.component.html',
  styleUrl: './general.component.scss',
})
export class GeneralComponent {
  public data: any;
  public image: any;
  public races: any;
  public classes: any;
  public genders: any;

  public constructor(
    public dataAccessor: DataService,
    private list: ListService,
  ) {}

  ngOnInit(): void {
    this.data = this.dataAccessor.characterData;
    this.image = this.dataAccessor.image.value;
    this.races = this.list.races;
    this.classes = this.list.classes;
    this.genders = this.list.genders;
  }

  public updateData(): void {
    console.log(this.data);

    this.dataAccessor.characterData = this.data;
  }

  public onImageUpload(event: any): void {
    const file = event.target.files[0];
    const reader = new FileReader();
    reader.onload = () => {
      this.image = reader.result;
      this.dataAccessor.image = { value: reader.result };
    };
    reader.readAsDataURL(file);
  }
}
