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

import { Component, ElementRef, Input, ViewChild } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { InputAccessor } from '../input-accessor';


@Component({
    selector: 'ui-textarea',
    template: require('./ui-textarea.html'),
    styles: [
        require('styles/common.less'),
        require('./ui-textarea.less')
    ],
    providers: [
        {provide: NG_VALUE_ACCESSOR, useExisting: UiTextArea, multi: true}
    ],
})



export class UiTextArea extends InputAccessor {

    @Input() placeholder: string = '';
    @Input() id: string = '';

    @ViewChild('textarea') textareaView: ElementRef;
    @ViewChild('lineNumbers') lineNumbersView: ElementRef;

    lineCount: number = 0;

    constructor(public elementRef: ElementRef) {
        super();
    }

    public prepareValueHook() {
        this.updateNumbers();
    }

    updateNumbers() {
        if (typeof this.innerValue === 'string') {
            this.lineCount = this.innerValue.split('\n').length;
            this.lineNumbersView.nativeElement.scrollTop =
                this.textareaView.nativeElement.scrollTop;
        } else {
            this.lineCount = 1;
        }
    }

}
