import { NetManager } from './../libs/tw/manager/NetManager';
import { DataManager } from './../libs/tw/manager/DataManager';
import { INetData } from './../libs/tc/interface/INetData';
/**
 * 游戏常量
 */
export default class GameConst {
	// gameconst版本
	public static version: number = 1.0;

	// 场景
	public static stage: egret.Stage;

	// 用于控制全局音乐是否播放和音乐按钮状态
	public static isPlayMusic: boolean = true;

	// 移除数组里的元素
	public static removeEle(e, arr) {
		let index = arr.indexOf(e);
		if (index >= 0) {
			arr.splice(index, 1)
		}
	}

	public static drawRigidBody(obj: any) {

	}

	// 二次贝塞尔,用于做曲线运动
	public static bezier(t: number, p0: number, p1: number, p2: number) {
		// (1-t)^2 P0 + 2(1-t)tP1+ t^2P2
		return Math.pow(1 - t, 2) * p0 + 2 * (1 - t) * t * p1 + Math.pow(t, 2) * p2;
	}

	/**
	 * 计算距离
	 * @param obj1 第一个物体 
	 * @param obj2 第二个物体
	 */
	public static distance(obj1: any, obj2: any) {
		let distance = Math.sqrt(Math.pow(obj1.x - obj2.x, 2) + Math.pow(obj1.y - obj2.y, 2));
		return distance;
	}

	/**
	 * 重新随机排序
	 * @param a 
	 * @param b 
	 */
	public static randomsort(a, b) {
		return Math.random() > .5 ? -1 : 1;
	}

	public static randomT(e, n?) {
		return e && "number" == typeof e.length && e.length ? e[Math.floor(Math.random() * e.length)] : ("number" != typeof n && (n = e || 1, e = 0), e + Math.random() * (n - e))
	}

	/**
	 * 从数组里取出
	 *  */
	public static takeOut(arr) {
		if (arr && arr.length) {
			return arr.shift();
		}
		return false;
	}

	/**
	 * 是否是IOS系统
	 *  */
	public static isIOS() {
		const u = navigator.userAgent;
		let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
		return isiOS;
	}

	/**
	 * 设置cookie
	 * @param name cookie的名称
	 * @param value cookie的值
	 * @param day cookie的过期时间
	 */
	public static setCookie(name, value, day) {
		if (day !== 0) {     //当设置的时间等于0时，不设置expires属性，cookie在浏览器关闭后删除
			var expires = day * 24 * 60 * 60 * 1000;
			var date = new Date(+new Date() + expires);
			document.cookie = name + "=" + escape(value) + ";expires=" + date.toUTCString();
		} else {
			document.cookie = name + "=" + escape(value);
		}
	};

	/**
     * 获取对应名称的cookie
	 * @param name cookie的名称
     * @returns {null} 不存在时，返回null
     */
	public static getCookie(name) {
		let arr;
		let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		if (arr = document.cookie.match(reg))
			return unescape(arr[2]);
		else
			return null;
	};

	/**
	 * 白鹭截canvas为img
	 * @param scene 当前场景
	 * @param startX 截图开始的x坐标
	 * @param startY 截图开始的y坐标
	 * @param width 截图的宽度
	 * @param height 截图的高度
	 * return base64的png图片
	 */
	public static getPrintScreenData(scene, startX, startY, width, height): any {
		let renderTexture: egret.RenderTexture = new egret.RenderTexture();
		renderTexture.drawToTexture(scene, new egret.Rectangle(startX, startY, width, height));
		// egret.updateAllScreens();
		let imageData = renderTexture.toDataURL("image/png");
		return imageData;
	}

	/**
	 * 将截图的图片展示到img标签，提供长按保存功能
	 * @param img dom的Img标签 document.getElementById('shareImg');
	 * @param imageData base64图片数据
	 * @param startX 截图的x
	 * @param startY 截图的y
	 * @param width 截图的宽度
	 * @param imgLoadCallBack 加载图片完成后的回调
	 */
	public static showImgByImgData(img: any, imageData: string, startX: number, startY: number, width: number, imgLoadCallBack: Function): any {
		img.style.position = 'absolute';
		let widthP = width / egret.MainContext.instance.stage.stageWidth;
		img.style.width = `${widthP * 100}%`;
		let leftP = startX / egret.MainContext.instance.stage.stageWidth;
		img.style.left = `${leftP * 100}%`;
		let topP = startY / egret.MainContext.instance.stage.stageHeight;
		img.style.top = `${topP * 100}%`;
		img['src'] = imageData;
		img.onload = imgLoadCallBack;
	}



	/**
	 * 屈臣氏宠物养成——领养宠物
	 * @param viplevel 用户vip等级 （1/2/3）
	 * @param identifier 宠物标识（wastonA/wastonB/wastonC）
	 * @param callback 回调
	 */
	public static adoptePig(viplevel: number, identifier: string, callback?: Function) {
		const param: any = {
			viplevel: viplevel,
			identifier: identifier,
			actId: DataManager.ins.petIndexData.activityId
		};
		const net: INetData = {
			name: 'adoptePig',
			uri: '/signpet/watson/adopte',
			type: 'post',
			dataType: 'json',
			param: param,
			callback: callback,
			hideMsg: true
		};
		NetManager.ins.send(net);
	}

	/**
	 * 宠物爱心值增加
	 * @param petId 宠物id
	 * @param callback 回调
	 */
	public static addValue(petId: number, callback?: Function) {
		const param: any = {
			petId: petId
		};
		const net: INetData = {
			name: 'addValue',
			uri: '/signpet/watson/addValue',
			type: 'post',
			dataType: 'json',
			param: param,
			callback: callback,
			hideMsg: true
		};
		NetManager.ins.send(net);
	}

	/**
	 * 爱心值清空（清空爱心值&&记录用户当日已抽奖）
	 * @param petId 宠物id
	 * @param callback 回调
	 */
	public static clearValue(petId: number, callback?: Function) {
		const param: any = {
			petId: petId
		};
		const net: INetData = {
			name: 'clearValue',
			uri: '/signpet/watson/clearValue',
			type: 'post',
			dataType: 'json',
			param: param,
			callback: callback,
			hideMsg: true
		};
		NetManager.ins.send(net);
	}

	/**
	 * 获取宠物信息
	 * @param petId 宠物id
	 * @param callback 回调
	 */
	public static getPetInfo(petId: number, callback?: Function) {
		const param: any = {
			petId: petId
		};
		const net: INetData = {
			name: 'getPetInfo',
			uri: '/signpet/watson/getPetInfo',
			type: 'get',
			dataType: 'json',
			param: param,
			callback: callback,
			hideMsg: false
		};
		NetManager.ins.send(net);
	}

	/**
	 * 获取当前用户所有宠物信息
	 * @param actId 活动id
	 * @param callback 回调函数
	 */
	public static getAllPetsInfo(actId: number, callback?: Function) {
		const param: any = {
			actId: actId
		};
		const net: INetData = {
			name: 'getAllPetsInfo',
			uri: '/signpet/watson/getAllPet',
			type: 'get',
			dataType: 'json',
			param: param,
			callback: callback,
			hideMsg: false
		};
		NetManager.ins.send(net);
	}

	/**
	 * 道具使用信息同步
	 * @param toys 道具标识符（逗号连接） 例：aa,bb,cc
	 * @param petId 宠物ID
	 * @param callback 回调
	 */
	public static updateUsingToys(toys, petId, callback?: Function) {
		const param: any = {
			toys: toys,
			petId: petId
		};
		const net: INetData = {
			name: 'updateUsingToys',
			uri: '/signpet/watson/toyUse',
			type: 'post',
			dataType: 'json',
			param: param,
			callback: callback,
			hideMsg: true
		};
		NetManager.ins.send(net);
	}
}