import { INetRequest } from "./Core/INetRequest";
import { NetConnector } from "./NetConnector";

export class NetManager {
	static get IsConnected() {
		return this._connector && this._connector.IsConnected;
	}

	static get HasInit() {
		return this._connector != null;
	}

	private static _connector: NetConnector;

	static Initialize(connector: NetConnector) {
		this._connector = connector;
	}

	static ConnectAsync() {
		this.CheckConnector();
		return this._connector.ConnectAsync();
	}

	/**
	 * 斷線
	 */
	static Disconnect() {
		this.CheckConnector();
		this._connector.Disconnect();
	}

	/**
	 * 傳送資料給Server, 不等待回應
	 * @param req
	 */
	static Send(req: INetRequest<any, any>) {
		this.CheckConnector();
		this._connector.Send(req);
	}

	/**
	 * 傳送資料給Server, 並等待回應
	 * @param req
	 */
	static SendAsync(req: INetRequest<any, any>, mask: boolean) {
		this.CheckConnector();
		return this._connector.SendAsync(req, mask);
	}

	private static CheckConnector() {
		if (!this._connector) throw new Error("請先呼叫CasinoNetManager.Initialize()初始化connector");
	}

}