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 {IServiceNote} from '../../../../model/entities/service-note';
import {IService} from '../../../../model/entities/service';
import {IServiceContact} from '../../../../model/entities/service-contact';

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

export class ServiceNoteEditComponent extends FormComponent implements OnInit {

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

    public service: IService;
    public serviceNote: IServiceNote;
    public serviceContacts: IServiceContact[];
    public serviceContactsById: {};
    public isExistingServiceContact: boolean;
    public contactId: string;

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

    ngOnInit() {
        this.service = Factory.getEmptyService();
        this.serviceNote = Factory.getEmptyServiceNote();
        this.serviceContacts = [];
        this.serviceContactsById = {};
        this.errorMsg = '';
        this.contactId = '';
        this.isCreationMode = false;
        this.isExistingServiceContact = false;
    }

    /**
     * Sets data
     * @param service
     * @param serviceNote
     * @param isCreationMode
     */
    public setData(service: IService, serviceNote: IServiceNote, isCreationMode = false): void {
        this.service = service;
        this.serviceNote = serviceNote;
        this.serviceContacts = this.service.v_service_contacts;
        this.serviceContactsById = Utils.getSortedObjFromArray(this.serviceContacts, 'id');
        this.serviceNote.service_id = this.service.id;
        this.contactId = '';
        this.isCreationMode = isCreationMode;
        this.errorMsg = '';
        if (isNull(this.serviceNote.service_contact_id)) {
            this.serviceNote.service_contact_id = -1;
        }
        if (this.serviceNote.service_contact_id > 0) {
            this.isExistingServiceContact = true;
        }
        this.resetFormValidation();
    }

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

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

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

}
