import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { ModalDialogParams } from '@nativescript/angular';
import { TimePicker } from '@nativescript/core';

@Component({
    selector: 'modal-time-picker',
    moduleId: module.id,
    templateUrl: './modal-view-time-picker.component.html',
    styleUrls: ['./_modal-view-time-picker.component.scss']
})
export class ModalTimePickerComponent implements OnInit {
    @ViewChild('timePicker', { static: true }) dp: ElementRef;

    constructor(private _params: ModalDialogParams) {
    }

    ngOnInit(): void {
        const picker = <TimePicker>this.dp.nativeElement;
        picker.hour = this._params.context.hours;
        picker.minute = this._params.context.minutes;
    }

    onOK() {
        const timePicker = <TimePicker>this.dp.nativeElement;
        const time = {hours: timePicker.hour.toString(), minutes: timePicker.minute.toString()};
        if (time.hours === '0' && time.minutes === '0') {
            time.hours = '23';
            time.minutes = '59';
        }
        this._params.closeCallback(time);
    }

    onClose(): void {
        this._params.closeCallback(null);
    }
}
