/**
 * Created by rockyl on 2018-12-12.
 * 弹层
 */

import TouchInterrupt from "components/base/TouchInterrupt";
import ScillaComponent from "scilla-components/src/base/ScillaComponent";
import Transform from "scilla-components/src/base/Transform";
import RectRenderer from "scilla-components/src/renderer/RectRenderer";
import { Entity } from "scilla/src";
import SingleSceneNavigator from "../navigator/SingleSceneNavigator";
import DialogContent from "./DialogContent";
import { none } from "./PopupEffect";

export default class Popup extends ScillaComponent {
	blackLayerDuration = 300;

	bgRenderer: Entity;
	private _touchInterrupt: TouchInterrupt;
	private _dialogStack = [];

	sceneContainer: Entity;
	_navigator: SingleSceneNavigator;

	onAwake() {
		super.onAwake();

		this._touchInterrupt = this.getComponent(TouchInterrupt);
		this._touchInterrupt.enabled = false;

		// this.bgRenderer.enabled = false;

		this._navigator = this.sceneContainer.getComponent(SingleSceneNavigator);

		this.entity.children.forEach(child => child.enabled = false);
	}

	fuckNavigator(p) {
		console.log('fuckNavigator', p)
		this._navigator.push(p)
	}

	onUpdate(t) {
		super.onUpdate(t);
	}

	onSleep() {
		super.onSleep();
	}

	onDestroy() {
		super.onDestroy();
	}

	setBgVisible(visible) {
		this.bgRenderer.enabled = visible;
		// if (visible) {
		// 	this._bgRenderer.enabled = true;
		// }
		// this._bgRenderer.alpha = visible ? 0 : 1;
		// createTween(this, this._bgRenderer, true)
		// 	.to({ alpha: visible ? 1 : 0 }, this.blackLayerDuration)
		// 	.call(() => {
		// 		if (!visible) {
		// 			this._bgRenderer.enabled = false;
		// 		}
		// 	});
	}

	getDialogInStack(name) {
		let result;
		for (let dialog of this._dialogStack) {
			if (dialog.name === name) {
				result = dialog;
				break;
			}
		}
		return result;
	}

	private bringToTop(dialogConfig) {
		let index = this._dialogStack.indexOf(dialogConfig);
		if (index >= 0) {
			this._dialogStack.splice(index, 1);
		}
		this._dialogStack.push(dialogConfig);
	}

	private delete(dialogConfig) {
		let index = this._dialogStack.indexOf(dialogConfig);
		this._dialogStack.splice(index, 1);
	}

	async showDialog(name, data?, callback?) {
		let dialogConfig = this.getDialogInStack(name);

		let dialog = this.entity.getChildrenByName(name)[0];

		if (!dialogConfig) {
			dialogConfig = {
				name, data, callback, dialog
			};
			dialog.enabled = true;
		}

		this.bringToTop(dialogConfig);

		const parent = dialog.parent;
		parent.removeChild(dialog);
		parent.addChildAt(dialog, parent.children.length);

		this.setBgVisible(true);
		this._touchInterrupt.enabled = true;

		const content: DialogContent = dialog.getComponent(DialogContent);
		content && content.setup(data);

		const transform = dialog.getComponent(Transform);

		const effect = content ? content.effectImpl : none;
		const effectOptions = content ? content.showEffectOptions : null;

		// await effect.show(transform, effectOptions);
	}

	async hideDialog(name, action?: string, data?) {
		let dialogConfig = this.getDialogInStack(name);

		if (!dialogConfig) {
			return;
		}

		let dialog = this.entity.getChildrenByName(name)[0];

		this.delete(dialogConfig);
		if (this._dialogStack.length === 0) {
			this.setBgVisible(false);
			this._touchInterrupt.enabled = false;
		}

		const transform = dialog.getComponent(Transform);

		const content: DialogContent = dialog.getComponent(DialogContent);

		const effect = content ? content.effectImpl : none;
		const effectOptions = content ? content.hideEffectOptions : null;

		// await effect.hide(transform, effectOptions);
		dialog.enabled = false;

		if (dialogConfig.callback) {
			dialogConfig.callback(action, data);
			dialogConfig.callback = null;
		}
	}

	hideAll() {
		for (let dialog of this._dialogStack) {
			this.hideDialog(dialog.name);
		}
	}
}
