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

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

export class OperatorContactPersonEditComponent extends FormComponent implements OnInit {

    @Output() createOperatorContact: EventEmitter<IOperatorContact> = new EventEmitter<IOperatorContact>();
    @Output() editOperatorContact: EventEmitter<IOperatorContact> = new EventEmitter<IOperatorContact>();
    @ViewChild('operatorContactForm', { static: true }) operatorContactForm: NgForm;

    public operatorContact: IOperatorContact;
    public config: IConfig;

    constructor(private appService: AppService, protected scrollToService: ScrollToService) {
        super(scrollToService);
    }

    ngOnInit() {
        this.operatorContact = Factory.getEmptyOperatorContact();
        this.config = this.appService.getConfig();
    }

    /**
     * Sets data
     * @param operatorContact
     */
    public setData(operatorContact: IOperatorContact): void {
        this.operatorContact = operatorContact;
        this.isCreationMode = isNull(this.operatorContact.id);
        this.errorMsg = '';
        this.resetFormValidation();
    }

    /**
     * Resets Form Validation
     */
    public resetFormValidation(): void {
        this.operatorContactForm.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) {
            // Check if date is valid
            if (isNull(this.operatorContact.date_of_birth) ||
                (!isNull(this.operatorContact.date_of_birth) && Utils.isValidDate(this.operatorContact.date_of_birth))) {
                if (this.isCreationMode) {
                    this.createOperatorContact.emit(this.operatorContact);
                } else {
                    this.editOperatorContact.emit(this.operatorContact);
                }
                this.errorMsg = '';
            } else {
                this.errorMsg = 'Bitte korrektes Datum eingeben.';
                this.scrollUp(this.isCreationMode ? 'operator-contact-person-modal' : 'operator-contact-person-edit-modal');
            }
        } else {
            this.scrollUp(this.isCreationMode ? 'operator-contact-person-modal' : 'operator-contact-person-edit-modal');
        }
    }

}
