import { Component, ElementRef, EventEmitter, forwardRef, HostListener, Input, Optional, Output, Renderer, ViewEncapsulation } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { Config } from '../../config/config';
import { Ion } from '../ion';
import { Item } from '../item/item';
import { ToggleGesture } from './toggle-gesture';
import { GestureController } from '../../gestures/gesture-controller';
import { Key } from '../../util/key';
import { Haptic } from '../../util/haptic';
import { Form } from '../../util/form';
import { isTrueProperty } from '../../util/util';
import { DomController } from '../../util/dom-controller';
export const TOGGLE_VALUE_ACCESSOR = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => Toggle),
    multi: true
};
export class Toggle extends Ion {
    constructor(_form, config, elementRef, renderer, _haptic, _item, _gestureCtrl, _domCtrl) {
        super(config, elementRef, renderer, 'toggle');
        this._form = _form;
        this._haptic = _haptic;
        this._item = _item;
        this._gestureCtrl = _gestureCtrl;
        this._domCtrl = _domCtrl;
        this._checked = false;
        this._init = false;
        this._disabled = false;
        this._activated = false;
        this._msPrv = 0;
        this._fn = null;
        this.ionChange = new EventEmitter();
        _form.register(this);
        if (_item) {
            this.id = 'tgl-' + _item.registerInput('toggle');
            this._labelId = 'lbl-' + _item.id;
            this._item.setElementClass('item-toggle', true);
        }
    }
    set color(val) {
        this._setColor(val);
    }
    set mode(val) {
        this._setMode(val);
    }
    ngAfterContentInit() {
        this._init = true;
        this._gesture = new ToggleGesture(this, this._gestureCtrl, this._domCtrl);
        this._gesture.listen();
    }
    _onDragStart(startX) {
        (void 0);
        (void 0);
        this._startX = startX;
        this._activated = true;
    }
    _onDragMove(currentX) {
        if (!this._startX) {
            (void 0);
            return;
        }
        (void 0);
        if (this._checked) {
            if (currentX + 15 < this._startX) {
                this.onChange(false);
                this._haptic.selection();
                this._startX = currentX;
                this._activated = true;
            }
        }
        else if (currentX - 15 > this._startX) {
            this.onChange(true);
            this._haptic.selection();
            this._startX = currentX;
            this._activated = (currentX < this._startX + 5);
        }
    }
    _onDragEnd(endX) {
        if (!this._startX) {
            (void 0);
            return;
        }
        (void 0);
        if (this.checked) {
            if (this._startX + 4 > endX) {
                this.onChange(false);
                this._haptic.selection();
            }
        }
        else if (this._startX - 4 < endX) {
            this.onChange(true);
            this._haptic.selection();
        }
        this._activated = false;
        this._startX = null;
    }
    get checked() {
        return this._checked;
    }
    set checked(val) {
        this._setChecked(isTrueProperty(val));
        this.onChange(this._checked);
    }
    _setChecked(isChecked) {
        if (!this._disabled && isChecked !== this._checked) {
            this._checked = isChecked;
            if (this._init) {
                this.ionChange.emit(this);
            }
            this._item && this._item.setElementClass('item-toggle-checked', isChecked);
        }
    }
    writeValue(val) {
        this._setChecked(isTrueProperty(val));
    }
    registerOnChange(fn) {
        this._fn = fn;
    }
    registerOnTouched(fn) {
        this.onTouched = fn;
    }
    get disabled() {
        return this._disabled;
    }
    set disabled(val) {
        this._disabled = isTrueProperty(val);
        this._item && this._item.setElementClass('item-toggle-disabled', this._disabled);
    }
    onChange(isChecked) {
        (void 0);
        this._fn && this._fn(isChecked);
        this._setChecked(isChecked);
        this.onTouched();
    }
    onTouched() { }
    _keyup(ev) {
        if (ev.keyCode === Key.SPACE || ev.keyCode === Key.ENTER) {
            (void 0);
            ev.preventDefault();
            ev.stopPropagation();
            this.onChange(!this._checked);
        }
    }
    initFocus() {
        this._elementRef.nativeElement.querySelector('button').focus();
    }
    ngOnDestroy() {
        this._form.deregister(this);
        this._gesture.destroy();
        this._fn = null;
    }
}
Toggle.decorators = [
    { type: Component, args: [{
                selector: 'ion-toggle',
                template: '<div class="toggle-icon" [class.toggle-checked]="_checked" [class.toggle-activated]="_activated">' +
                    '<div class="toggle-inner"></div>' +
                    '</div>' +
                    '<button role="checkbox" ' +
                    'type="button" ' +
                    'ion-button="item-cover" ' +
                    '[id]="id" ' +
                    '[attr.aria-checked]="_checked" ' +
                    '[attr.aria-labelledby]="_labelId" ' +
                    '[attr.aria-disabled]="_disabled" ' +
                    'class="item-cover">' +
                    '</button>',
                host: {
                    '[class.toggle-disabled]': '_disabled'
                },
                providers: [TOGGLE_VALUE_ACCESSOR],
                encapsulation: ViewEncapsulation.None,
            },] },
];
Toggle.ctorParameters = [
    { type: Form, },
    { type: Config, },
    { type: ElementRef, },
    { type: Renderer, },
    { type: Haptic, },
    { type: Item, decorators: [{ type: Optional },] },
    { type: GestureController, },
    { type: DomController, },
];
Toggle.propDecorators = {
    'color': [{ type: Input },],
    'mode': [{ type: Input },],
    'ionChange': [{ type: Output },],
    'checked': [{ type: Input },],
    'disabled': [{ type: Input },],
    '_keyup': [{ type: HostListener, args: ['keyup', ['$event'],] },],
};
//# sourceMappingURL=toggle.js.map