import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {AppService} from '../../../../services/app.service';
import {Const} from '../../../../utils/const';
import {Factory} from '../../../../factory/factory';
import {Utils} from '../../../../utils/utils';
import {IOperatorNote} from '../../../../model/entities/operator-note';
import {OperatorNoteEditComponent} from '../operator-note-edit/operator-note-edit.component';
import {IOperator} from '../../../../model/entities/operator';

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

export class OperatorNoteDetailComponent implements OnInit {

    @Output() editNote: EventEmitter<IOperatorNote> = new EventEmitter<IOperatorNote>();
    @Output() deleteNote: EventEmitter<number> = new EventEmitter<number>();
    @ViewChild('operatorNoteEdit', { static: true }) operatorNoteEdit: OperatorNoteEditComponent;

    public operator: IOperator;
    public operatorNote: IOperatorNote;
    public creationDate: string;
    public isEditMode: boolean;
    public noteDate: string;
    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.operator = Factory.getEmptyOperator();
        this.operatorNote = Factory.getEmptyOperatorNote();
        this.creationDate = '';
        this.isEditMode = false;
        this.noteDate = '';
    }

    /**
     * Set data
     */
    public setData(operator: IOperator, operatorNote: IOperatorNote): void {
        this.operator = operator;
        this.operatorNote = operatorNote;
        this.creationDate = Utils.getDateTimeToDisplay(this.operatorNote.creation_date);
        this.isEditMode = false;
        this.noteDate = Utils.getDateTimeToDisplay(this.operatorNote.note_date);
    }

    /**
     * Updates data
     * @param operator
     */
    public updateData(operator: IOperator): void {
        this.operator = operator;
        for (let i = 0; i < this.operator.v_operator_notes.length; i++) {
            if (this.operator.v_operator_notes[i].id == this.operatorNote.id) {
                this.operatorNote = this.operator.v_operator_notes[i];
                this.noteDate = Utils.getDateTimeToDisplay(this.operatorNote.note_date);
            }
        }
        this.isEditMode = false;
    }

    /**
     * Edit note entry
     */
    public editEntry(): void {
        this.isEditMode = true;
        this.operatorNoteEdit.setData(this.operator, Utils.deepClone(this.operatorNote) as IOperatorNote);
    }

    /**
     * Edits note entry
     * @param operatorNote
     */
    public editOperatorNoteFunction(operatorNote: IOperatorNote) {
        this.editNote.emit(operatorNote);
    }

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

}
