import { EventEmitter, Output } from '@angular/core';
import { isPresent, assign } from '../util/util';
import { NavParams } from './nav-params';
export class ViewController {
    constructor(component, data, rootCssClass = DEFAULT_CSS_CLASS) {
        this.component = component;
        this._isHidden = false;
        this.isOverlay = false;
        this._emitter = new EventEmitter();
        this.data = (data instanceof NavParams ? data.data : (isPresent(data) ? data : {}));
        this._cssClass = rootCssClass;
        this.willEnter = new EventEmitter();
        this.didEnter = new EventEmitter();
        this.willLeave = new EventEmitter();
        this.didLeave = new EventEmitter();
        this.willUnload = new EventEmitter();
    }
    init(componentRef) {
        this._cmp = componentRef;
        this.instance = this.instance || componentRef.instance;
        this._detached = false;
    }
    _setNav(navCtrl) {
        this._nav = navCtrl;
    }
    _setInstance(instance) {
        this.instance = instance;
    }
    subscribe(generatorOrNext) {
        return this._emitter.subscribe(generatorOrNext);
    }
    emit(data) {
        this._emitter.emit(data);
    }
    onDismiss(callback) {
        console.warn('onDismiss(..) has been deprecated. Please use onDidDismiss(..) instead');
        this.onDidDismiss(callback);
    }
    onDidDismiss(callback) {
        this._onDidDismiss = callback;
    }
    onWillDismiss(callback) {
        this._onWillDismiss = callback;
    }
    dismiss(data, role, navOptions = {}) {
        if (!this._nav) {
            return Promise.resolve(false);
        }
        let options = assign({}, this._leavingOpts, navOptions);
        this._onWillDismiss && this._onWillDismiss(data, role);
        return this._nav.remove(this._nav.indexOf(this), 1, options).then(() => {
            this._onDidDismiss && this._onDidDismiss(data, role);
            this._onDidDismiss = null;
            return data;
        });
    }
    getNav() {
        return this._nav;
    }
    getTransitionName(direction) {
        return this._nav && this._nav.config.get('pageTransition');
    }
    getNavParams() {
        return new NavParams(this.data);
    }
    setLeavingOpts(opts) {
        this._leavingOpts = opts;
    }
    enableBack() {
        if (this._nav) {
            let previousItem = this._nav.getPrevious(this);
            return !!(previousItem);
        }
        return false;
    }
    get name() {
        return this.component ? this.component.name : '';
    }
    get index() {
        return (this._nav ? this._nav.indexOf(this) : -1);
    }
    isFirst() {
        return (this._nav ? this._nav.first() === this : false);
    }
    isLast() {
        return (this._nav ? this._nav.last() === this : false);
    }
    _domShow(shouldShow, renderer) {
        if (this._cmp) {
            if (shouldShow === this._isHidden) {
                this._isHidden = !shouldShow;
                let value = (shouldShow ? null : '');
                renderer.setElementAttribute(this.pageRef().nativeElement, 'hidden', value);
            }
        }
    }
    _setZIndex(zIndex, renderer) {
        if (zIndex !== this._zIndex) {
            this._zIndex = zIndex;
            const pageRef = this.pageRef();
            if (pageRef) {
                renderer.setElementStyle(pageRef.nativeElement, 'z-index', zIndex);
            }
        }
    }
    pageRef() {
        return this._cmp && this._cmp.location;
    }
    _setContent(directive) {
        this._cntDir = directive;
    }
    getContent() {
        return this._cntDir;
    }
    _setContentRef(elementRef) {
        this._cntRef = elementRef;
    }
    contentRef() {
        return this._cntRef;
    }
    _setHeader(directive) {
        this._hdrDir = directive;
    }
    getHeader() {
        return this._hdrDir;
    }
    _setFooter(directive) {
        this._ftrDir = directive;
    }
    getFooter() {
        return this._ftrDir;
    }
    _setNavbar(directive) {
        this._nb = directive;
    }
    getNavbar() {
        return this._nb;
    }
    hasNavbar() {
        return !!this._nb;
    }
    setBackButtonText(val) {
        this._nb && this._nb.setBackButtonText(val);
    }
    showBackButton(shouldShow) {
        if (this._nb) {
            this._nb.hideBackButton = !shouldShow;
        }
    }
    _willLoad() {
        ctrlFn(this, 'WillLoad');
    }
    _didLoad() {
        if (this.instance && this.instance.ionViewLoaded) {
            try {
                console.warn('ionViewLoaded() has been deprecated. Please rename to ionViewDidLoad()');
                this.instance.ionViewLoaded();
            }
            catch (e) {
                console.error(this.name + ' iionViewLoaded: ' + e.message);
            }
        }
        ctrlFn(this, 'DidLoad');
    }
    _willEnter() {
        if (this._detached && this._cmp) {
            this._cmp.changeDetectorRef.reattach();
            this._detached = false;
        }
        this.willEnter.emit(null);
        ctrlFn(this, 'WillEnter');
    }
    _didEnter() {
        this._nb && this._nb.didEnter();
        this.didEnter.emit(null);
        ctrlFn(this, 'DidEnter');
    }
    _willLeave() {
        this.willLeave.emit(null);
        ctrlFn(this, 'WillLeave');
    }
    _didLeave() {
        this.didLeave.emit(null);
        ctrlFn(this, 'DidLeave');
        if (!this._detached && this._cmp) {
            this._cmp.changeDetectorRef.detach();
            this._detached = true;
        }
    }
    _willUnload() {
        this.willUnload.emit(null);
        ctrlFn(this, 'WillUnload');
        if (this.instance && this.instance.ionViewDidUnload) {
            console.warn('ionViewDidUnload() has been deprecated. Please use ionViewWillUnload() instead');
            try {
                this.instance.ionViewDidUnload();
            }
            catch (e) {
                console.error(this.name + ' ionViewDidUnload: ' + e.message);
            }
        }
    }
    _destroy(renderer) {
        if (this._cmp) {
            if (renderer) {
                renderer.setElementAttribute(this._cmp.location.nativeElement, 'class', null);
                renderer.setElementAttribute(this._cmp.location.nativeElement, 'style', null);
            }
            this._cmp.destroy();
        }
        if (this._nav) {
            const index = this._nav.indexOf(this);
            if (index > -1) {
                this._nav._views.splice(index, 1);
            }
        }
        this._nav = this._cmp = this.instance = this._cntDir = this._cntRef = this._hdrDir = this._ftrDir = this._nb = this._onWillDismiss = null;
    }
    _lifecycleTest(lifecycle) {
        let instance = this.instance;
        let methodName = 'ionViewCan' + lifecycle;
        if (instance && instance[methodName]) {
            try {
                return instance[methodName]();
            }
            catch (e) {
                console.error(`${this.name} ionViewCan${lifecycle} error: ${e}`);
                return false;
            }
        }
        return true;
    }
}
ViewController.propDecorators = {
    '_emitter': [{ type: Output },],
};
function ctrlFn(viewCtrl, fnName) {
    if (viewCtrl.instance) {
        if (viewCtrl.instance['ionView' + fnName]) {
            try {
                viewCtrl.instance['ionView' + fnName]();
            }
            catch (e) {
                console.error(viewCtrl.name + ' ionView' + fnName + ': ' + e.message);
            }
        }
    }
}
export function isViewController(viewCtrl) {
    return !!(viewCtrl && viewCtrl._didLoad && viewCtrl._willUnload);
}
const DEFAULT_CSS_CLASS = 'ion-page';
