/* Copyright CloudLinux Zug GmbH 2010-2017 All Rights Reserved
 *
 * Licensed under CLOUD LINUX LICENSE AGREEMENT
 * http://cloudlinux.com/docs/LICENSE.TXT
 */

import { Component, Input, HostListener } from '@angular/core';
import { ModalWindow } from './modal-window';
import { ESCAPE_KEYCODE } from '../../../services/constants';
import { Tool } from '../../../model/tool';
import { TypeOfDirections } from '../../types';
import { Message } from '../../../model/message';

@Component({
    selector: 'modal-dialog',
    template: require('./modal-dialog.html'),
    styles: [
        require('./modal-dialog.less')
    ],
})
export class ModalDialog extends ModalWindow {

    @Input() header: Message;
    @Input() tools: Tool[] = [];
    @Input() toolsDirectionType: TypeOfDirections = 'row';

    /**
     * Name of "Cancel" button.
     * @type {string}
     */
    @Input() cancelButtonName: string;

    /**
     * Name of "Agree" button.
     * @type {string}
     */
    @Input() agreeButtonName: string;


    @HostListener('document:keydown', ['$event'])
    handleKeyboardEvent(event: KeyboardEvent) {
        if (this.show && event.keyCode === ESCAPE_KEYCODE) {
            this.cancel();
        }
    }

    stopPropagation(event) {
        event.stopPropagation();
    }

    runAction(action: Function) {
        this.show = false;
        action(this.data);
        this.resolve();
    }
}
