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 {IOperatorContact} from '../../../../model/entities/operator-contact';
import {OperatorContactPersonEditComponent} from '../operator-contact-person-edit/operator-contact-person-edit.component';
import {IOperator} from '../../../../model/entities/operator';

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

export class OperatorContactPersonDetailComponent implements OnInit {

    @Output() deleteOperatorContact: EventEmitter<number> = new EventEmitter<number>();
    @Output() editOperatorContact: EventEmitter<IOperatorContact> = new EventEmitter<IOperatorContact>();
    @Output() shortcut: EventEmitter<IOperator> = new EventEmitter<IOperator>();
    @ViewChild('operatorContactEdit', { static: true }) operatorContactEdit: OperatorContactPersonEditComponent;

    public isEditMode: boolean;
    public operator: IOperator;
    public operatorContact: IOperatorContact;
    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_ACCOUNTING);
        this.config = this.appService.getConfig();
        this.isEditMode = false;
        this.operator = Factory.getEmptyOperator();
        this.operatorContact = Factory.getEmptyOperatorContact();
    }

    /**
     * Sets data
     */
    public setData(operator: IOperator, operatorContact: IOperatorContact): void {
        this.setContactPersonData(operator);
        this.operatorContact = operatorContact;
    }

    /**
     * Updates data
     * @param operator
     */
    public updateData(operator: IOperator): void {
        this.setContactPersonData(operator);
        for (let i = 0; i < this.operator.v_operator_contacts.length; i++) {
            if (this.operator.v_operator_contacts[i].id == this.operatorContact.id) {
                this.operatorContact = this.operator.v_operator_contacts[i];
            }
        }
    }

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

    /**
     * Edit contact entry
     */
    public editEntry(): void {
        this.isEditMode = true;
        this.operatorContactEdit.setData(Utils.deepClone(this.operatorContact) as IOperatorContact);
    }

    /**
     * Edits meeting entry
     * @param operatorContact
     */
    public editOperatorContactFunction(operatorContact: IOperatorContact) {
        this.editOperatorContact.emit(operatorContact);
    }

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

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

}
