import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {FormComponent} from '../../../../components/form-component';
import {NgForm} from '@angular/forms';
import {ScrollToService} from '@nicky-lenaers/ngx-scroll-to';
import {Factory} from '../../../../factory/factory';
import {Utils} from '../../../../utils/utils';
import {isNull} from 'util';
import {IOperatorNote} from '../../../../model/entities/operator-note';
import {IOperator} from '../../../../model/entities/operator';
import {IOperatorContact} from '../../../../model/entities/operator-contact';

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

export class OperatorNoteEditComponent extends FormComponent implements OnInit {

    @Output() createNote: EventEmitter<IOperatorNote> = new EventEmitter<IOperatorNote>();
    @Output() editNote: EventEmitter<IOperatorNote> = new EventEmitter<IOperatorNote>();
    @ViewChild('operatorNoteForm', { static: true }) operatorNoteForm: NgForm;

    public operator: IOperator;
    public operatorNote: IOperatorNote;
    public operatorContacts: IOperatorContact[];
    public operatorContactsById: {};
    public isExistingOperatorContact: boolean;
    public contactId: string;

    constructor(protected scrollToService: ScrollToService) {
        super(scrollToService);
    }

    ngOnInit() {
        this.operator = Factory.getEmptyOperator();
        this.operatorNote = Factory.getEmptyOperatorNote();
        this.operatorContacts = [];
        this.operatorContactsById = {};
        this.errorMsg = '';
        this.contactId = '';
        this.isCreationMode = false;
        this.isExistingOperatorContact = false;
    }

    /**
     * Sets data
     * @param operator
     * @param operatorNote
     * @param isCreationMode
     */
    public setData(operator: IOperator, operatorNote: IOperatorNote, isCreationMode = false): void {
        this.operator = operator;
        this.operatorNote = operatorNote;
        this.operatorContacts = this.operator.v_operator_contacts;
        this.operatorContactsById = Utils.getSortedObjFromArray(this.operatorContacts, 'id');
        this.operatorNote.operator_id = this.operator.id;
        this.contactId = '';
        this.isCreationMode = isCreationMode;
        this.errorMsg = '';
        if (isNull(this.operatorNote.operator_contact_id)) {
            this.operatorNote.operator_contact_id = -1;
        }
        if (this.operatorNote.operator_contact_id > 0) {
            this.isExistingOperatorContact = true;
        }
        this.resetFormValidation();
    }

    /**
     * On select contact
     * @param e
     */
    public selectContact(e: any) {
        this.operatorNote.operator_contact_id = e.target.value;
        this.isExistingOperatorContact = false;
        this.operatorNote.gender = null;
        this.operatorNote.firstname = null;
        this.operatorNote.lastname = null;
        this.operatorNote.department = null;
        this.operatorNote.email = null;
        this.operatorNote.phone_no = null;
        this.operatorNote.mobile_no = null;
        this.operatorNote.fax_no = null;
        if (this.operatorNote.operator_contact_id > 0) {
            this.operatorNote.gender = this.operatorContactsById[this.operatorNote.operator_contact_id].gender;
            this.operatorNote.firstname = this.operatorContactsById[this.operatorNote.operator_contact_id].firstname;
            this.operatorNote.lastname = this.operatorContactsById[this.operatorNote.operator_contact_id].lastname;
            this.operatorNote.department = this.operatorContactsById[this.operatorNote.operator_contact_id].department;
            this.operatorNote.email = this.operatorContactsById[this.operatorNote.operator_contact_id].email;
            this.operatorNote.phone_no = this.operatorContactsById[this.operatorNote.operator_contact_id].phone_no;
            this.operatorNote.mobile_no = this.operatorContactsById[this.operatorNote.operator_contact_id].mobile_no;
            this.operatorNote.fax_no = this.operatorContactsById[this.operatorNote.operator_contact_id].fax_no;
            this.isExistingOperatorContact = true;
        }
    }

    /**
     * Resets Form Validation
     */
    public resetFormValidation(): void {
        this.operatorNoteForm.form.markAsUntouched();
    }

    /**
     * Saves operator
     * @param value
     * @param valid
     */
    public onFormSubmit({value, valid}: { value: any, valid: boolean }): void {
        this.errorMsg = 'Bitte beheben Sie alle Fehler.';
        if (valid) {
            if (this.operatorNote.operator_contact_id == -1) {
                this.operatorNote.operator_contact_id = null;
            }
            if (this.isCreationMode) {
                this.createNote.emit(this.operatorNote);
            } else {
                this.editNote.emit(this.operatorNote);
            }
            this.errorMsg = '';
        } else {
            this.scrollUp(this.isCreationMode ? 'operator-note-modal' : 'operator-note-edit-modal');
        }
    }

}
