import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {IConfig} from '../../../../model/virtual/config';
import {AppService} from '../../../../services/app.service';
import {Const} from '../../../../utils/const';
import {Factory} from '../../../../factory/factory';
import {Utils} from '../../../../utils/utils';
import {IServiceContact} from '../../../../model/entities/service-contact';
import {IService} from '../../../../model/entities/service';
import {
    ServiceContactPersonEditComponent
} from '../service-contact-person-edit/service-contact-person-edit.component';

@Component({
  selector: 'app-service-contact-person-detail',
  templateUrl: './service-contact-person-detail.component.html',
  styleUrls: ['./service-contact-person-detail.component.scss']
})

export class ServiceContactPersonDetailComponent implements OnInit {

    @Output() deleteServiceContact: EventEmitter<number> = new EventEmitter<number>();
    @Output() editServiceContact: EventEmitter<IServiceContact> = new EventEmitter<IServiceContact>();
    @Output() shortcut: EventEmitter<IService> = new EventEmitter<IService>();
    @ViewChild('serviceContactEdit', { static: true }) serviceContactEdit: ServiceContactPersonEditComponent;

    public isEditMode: boolean;
    public service: IService;
    public serviceContact: IServiceContact;
    public config: IConfig;
    public hasEditRights: boolean;

    constructor(public appService: AppService) { }

    ngOnInit() {
        this.hasEditRights =
            this.appService.userHasRole(Const.USER_ROLE_TECHNIQUE) ||
            this.appService.userHasRole(Const.USER_ROLE_PRODUCTION) ||
            this.appService.userHasRole(Const.USER_ROLE_SERVICE);
        this.config = this.appService.getConfig();
        this.isEditMode = false;
        this.service = Factory.getEmptyService();
        this.serviceContact = Factory.getEmptyServiceContact();
    }

    /**
     * Sets data
     */
    public setData(service: IService, serviceContact: IServiceContact): void {
        this.setContactPersonData(service);
        this.serviceContact = serviceContact;
    }

    /**
     * Updates data
     * @param service
     */
    public updateData(service: IService): void {
        this.setContactPersonData(service);
        for (let i = 0; i < this.service.v_service_contacts.length; i++) {
            if (this.service.v_service_contacts[i].id === this.serviceContact.id) {
                this.serviceContact = this.service.v_service_contacts[i];
            }
        }
    }

    /**
     * Sets contact person data
     * @param service
     */
    public setContactPersonData(service: IService): void {
        this.isEditMode = false;
        this.service = service;
    }

    /**
     * Edit contact entry
     */
    public editEntry(): void {
        this.isEditMode = true;
        this.serviceContactEdit.setData(Utils.deepClone(this.serviceContact) as IServiceContact);
    }

    /**
     * Edits meeting entry
     * @param serviceContact
     */
    public editServiceContactFunction(serviceContact: IServiceContact) {
        this.editServiceContact.emit(serviceContact);
    }

    /**
     * Delete contact entry
     */
    public deleteEntry(): void {
        const confirmAction = confirm('Wollen Sie diesen Ansprechpartner wirklich löschen?');
        if (confirmAction == true) {
            this.deleteServiceContact.emit(this.serviceContact.id);
        }
    }

    /**
     * Shortcut to service detail
     */
    public serviceShortcut(): void {
        this.shortcut.emit(this.service);
    }

}
