import utils from 'somes';
import * as React from 'react';
import { ViewController } from '../lib/ctr';
import { DialogStack } from '../lib/dialog';
import { LayerGroup } from '../lib/layer';
export var Type;
(function (Type) {
    Type[Type["ACTIVITY"] = 1] = "ACTIVITY";
    Type[Type["WIDGET"] = 2] = "WIDGET";
    Type[Type["COVER"] = 3] = "COVER";
})(Type || (Type = {}));
export var CoverType;
(function (CoverType) {
    CoverType[CoverType["TOP"] = 0] = "TOP";
    CoverType[CoverType["BOTTOM"] = 1] = "BOTTOM";
})(CoverType || (CoverType = {}));
export class Window extends ViewController {
    constructor(props) {
        super(props);
        utils.assert(props.__app__);
        this.app = props.__app__;
        this.id = props.id;
    }
    triggerResume() {
    }
    triggerPause() {
    }
    close(animate = true) {
        return this.app.launcher.close(this.app, this.id, animate);
    }
}
export var ActivityNavStatus;
(function (ActivityNavStatus) {
    ActivityNavStatus[ActivityNavStatus["BACKGROUND"] = 1] = "BACKGROUND";
    ActivityNavStatus[ActivityNavStatus["FOREGROUND"] = 2] = "FOREGROUND";
    ActivityNavStatus[ActivityNavStatus["LEAVE"] = 3] = "LEAVE";
})(ActivityNavStatus || (ActivityNavStatus = {}));
export class Activity extends Window {
    constructor() {
        super(...arguments);
        this.permanent = 0;
        this.navStatus = (this.props.__navStatus || ActivityNavStatus.LEAVE);
    }
    get preventCover() {
        var _a, _b;
        return ((_a = this._dialogStack) === null || _a === void 0 ? void 0 : _a.preventCover) || ((_b = this._layerGroup) === null || _b === void 0 ? void 0 : _b.preventCover) || false;
    }
    get dialogStack() {
        if (!this._dialogStack) {
            this._dialogStack = new DialogStack(this.refs.__layers);
        }
        return this._dialogStack;
    }
    get layerGroup() {
        if (!this._layerGroup) {
            this._layerGroup = new LayerGroup(this.refs.__layers);
        }
        return this._layerGroup;
    }
    recoveryState() {
        if (this.navStatus == ActivityNavStatus.BACKGROUND) {
            return super.recoveryState();
        }
    }
    triggerRemove() {
        if (this._dialogStack)
            this._dialogStack.closeAll();
    }
    exit(rc, animate = true) {
        var cb = this.props.callback;
        if (cb) {
            return this.app.launcher.launch(cb.app, { animate, ...cb.args, ...rc });
        }
        else {
            return this.close(animate);
        }
    }
    present(activity, args, animate = true) {
        this.app.launcher.show(this.app, activity, args, animate);
    }
    presentDynamicModule(module, args, animate = true) {
        module.then(({ default: Act }) => this.present(Act, args, animate));
    }
    showDialog(D, opts, animate = true) {
        this.app.launcher.closeCoverAll();
        return this.dialogStack.show(D, opts, animate, this);
    }
    closeDialog(id, animate = true) {
        return this.dialogStack.close(id, animate);
    }
    showLayer(L, opts, animate = true, delay = 0) {
        this.app.launcher.closeCoverAll();
        return this.layerGroup.show(L, opts, animate, delay, this);
    }
    closeLayer(id, animate = true) {
        return this.layerGroup.close(id, animate);
    }
    render() {
        return (React.createElement("div", { ref: "__dom" },
            this.renderBody(),
            React.createElement("div", { ref: "__layers" })));
    }
    async intoBackground(animate) {
        var div = this.refs.__dom;
        var prevNavStatus = this.navStatus;
        if (prevNavStatus != ActivityNavStatus.BACKGROUND) {
            this.navStatus = ActivityNavStatus.BACKGROUND;
            this.triggerPause();
            if (div) {
                if (animate) {
                    div.style.zIndex = '2';
                    div.style.transitionDuration = `${animate}ms`;
                    div.style.transform = 'translateX(-50%) scale(1,1)';
                    await utils.sleep(400);
                }
                div.style.display = 'none';
            }
        }
        else {
            this.triggerPause();
        }
    }
    async intoForeground(animate) {
        var div = this.refs.__dom;
        var prevNavStatus = this.navStatus;
        if (prevNavStatus != ActivityNavStatus.FOREGROUND) {
            this.navStatus = ActivityNavStatus.FOREGROUND;
            this.triggerResume();
            if (div) {
                div.style.display = 'block';
                div.style.transitionDuration = '0ms';
                if (animate) {
                    div.style.transitionDuration = '0ms';
                    if (prevNavStatus == ActivityNavStatus.LEAVE) {
                        div.style.zIndex = '3';
                        div.style.transform = 'translateX(100%) scale(1,1)';
                    }
                    else {
                        div.style.zIndex = '1';
                        div.style.transform = 'translateX(-50%) scale(1,1)';
                    }
                    await utils.sleep(30);
                    div.style.transitionDuration = `${animate}ms`;
                    div.style.transform = 'translateX(0) scale(1,1)';
                    await utils.sleep(400);
                }
                else {
                    div.style.transform = 'translateX(0) scale(1,1)';
                }
            }
        }
        else {
            this.triggerResume();
        }
    }
    async intoLeave(animate) {
        var div = this.refs.__dom;
        var prevNavStatus = this.navStatus;
        if (prevNavStatus != ActivityNavStatus.LEAVE) {
            this.navStatus = ActivityNavStatus.LEAVE;
            this.triggerPause();
            if (div) {
                if (animate) {
                    div.style.zIndex = '2';
                    div.style.transitionDuration = `${animate}ms`;
                    div.style.transform = 'translateX(100%) scale(1,1)';
                    await utils.sleep(400);
                }
                div.style.display = 'none';
            }
        }
        else {
            this.triggerPause();
        }
    }
}
Activity.type = Type.ACTIVITY;
export class Widget extends Window {
}
Widget.type = Type.WIDGET;
export class Cover extends Window {
}
Cover.type = Type.COVER;
