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 {IProductionNote} from '../../../../model/entities/production-note';
import {ProductionNoteEditComponent} from '../production-note-edit/production-note-edit.component';
import {IProduction} from '../../../../model/entities/production';

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

export class ProductionNoteDetailComponent implements OnInit {

    @Output() editNote: EventEmitter<IProductionNote> = new EventEmitter<IProductionNote>();
    @Output() deleteNote: EventEmitter<number> = new EventEmitter<number>();
    @ViewChild('productionNoteEdit', { static: true }) productionNoteEdit: ProductionNoteEditComponent;

    public production: IProduction;
    public productionNote: IProductionNote;
    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_PRODUCTION) ||
            this.appService.userHasRole(Const.USER_ROLE_SERVICE);
        this.production = Factory.getEmptyProduction();
        this.productionNote = Factory.getEmptyProductionNote();
        this.creationDate = '';
        this.isEditMode = false;
        this.noteDate = '';
    }

    /**
     * Set data
     */
    public setData(production: IProduction, productionNote: IProductionNote): void {
        this.production = production;
        this.productionNote = productionNote;
        this.creationDate = Utils.getDateTimeToDisplay(this.productionNote.creation_date);
        this.isEditMode = false;
        this.noteDate = Utils.getDateTimeToDisplay(this.productionNote.note_date);
    }

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

    /**
     * Edit note entry
     */
    public editEntry(): void {
        this.isEditMode = true;
        this.productionNoteEdit.setData(this.production, Utils.deepClone(this.productionNote) as IProductionNote);
    }

    /**
     * Edits note entry
     * @param productionNote
     */
    public editProductionNoteFunction(productionNote: IProductionNote) {
        this.editNote.emit(productionNote);
    }

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

}
