import {Component, Input} from '@angular/core';
import {FormsService, ObjectsService} from "@app/_services";
import {Router} from "@angular/router";

@Component({
  selector: 'member-section-item',
  templateUrl: 'member-section-item.component.html',
  styleUrls: ['member-section-item.component.scss']
})
export class MemberSectionItemComponent {
  @Input() id: any;

  constructor(
    private formsService: FormsService,
    private router: Router,
    private objectsService: ObjectsService,
  ) {
  }

  public item: any;
  public fullScreen: boolean;

  ngOnInit() {
    this.fetch();
  }

  fetch() {
    this.objectsService.fetchObject(this.id, {include: 'groups.fields.value'}).subscribe(res => {
      this.item = res.data;
    })
  }

  parseParam(param: string): any {
    let result = '';
    this?.item?.groups?.data[0]?.fields?.data.forEach(res => {
      if (param === res.name) {
        result = res.value.data[0];
      }
    });
    return result;
  }

  parseParamFile(param: string): string {
    return this.parseParam(param)?.links?.full;
  }

  get name() {
    return this.parseParam('member-name-required');
  }

  get photo() {
    return this.parseParamFile('member-image');
  }

  get position() {
    return this.parseParam('member-position-required');
  }

  get document() {
    return this.parseParamFile('member-doc');
  }

  get phone() {
    return this.parseParam('member-phone');
  }

  get email() {
    return this.parseParam('member-email');
  }

  get description() {
    return this.parseParam('member-description');
  }
}
