/**
 * Created by rockyl on 2019-04-10.
 *
 * 警告对话框内容
 */

import { TextureRenderer } from "scilla-components/src";
import { AssetsManager, createTexture, EngineConfig, Entity, Texture, dynamic } from "scilla/src";
import DialogContent from "../popup/DialogContent";
import { setScale, setText } from "../transformUtils";
import { getRecordURL } from "./ScoreDialogContent";

export default class PrizeDialogContent extends DialogContent {
	pic: Entity;
	name: Entity;
	title: Entity;

	private loadImage = (url: string): Promise<HTMLImageElement> => {
		return new Promise<HTMLImageElement>((resolve, reject) => {
			let img = new Image();
			if (EngineConfig.imgCrossOrigin) {
				img.setAttribute('crossOrigin', 'anonymous');
			}
			img.onload = function (e) {
				resolve(img);
			};
			img.onerror = function (e) {
				reject(e);
			};
			img.src = url;
		});
	}

	private loadTexture = async (url: string): Promise<Texture> => {
		const img = await this.loadImage(url);
		const texture = createTexture(img);
		return texture;
	}

	onClick_usebtn() {
		// window.location.href=this.getOrderStatus.lottery.link
		window.location.href = getRecordURL();
	}

	onAwake() {
		super.onAwake();
	}

	async setup(data: any = {}) {
		setText(this.name, data.prizeName);
		setText(this.title, this.getTypeStr(data.prizeType))
		const texRender = this.pic.getComponent(TextureRenderer);
		const texture = await this.loadTexture(data.prizeImage);
		texRender.texture = texture;
		if (texture.bounds.width > texture.bounds.height) {
			const maxwidth = 250;
			setScale(this.pic, maxwidth / texture.bounds.width)
		} else {
			const maxheight = 250;
			setScale(this.pic, maxheight / texture.bounds.height)
		}
	}

	private getTypeStr(type: number) {
		if (type == 1) {
			return `获得一等奖`
		}
		if (type == 2) {
			return `获得二等奖`
		}
		if (type == 3) {
			return `获得三等奖`
		}
		if (type == 4) {
			return `获得四等奖`
		}
	}
}
