
import { getTextureByName, createSvga } from './utils';
import { props } from "../props";

export default class Gameinit extends engine.Container {

    private _isSetUp = false;
    private totleY = 1200;
    _body: engine.Rect;
    isMove = false;
    isChoose = true;
    isDid = true;
    num;
    listGift = [];
    constructor() {
        super();
        console.log("11111111111111");
        this.once(engine.Event.ADDED_TO_STAGE, this.setup, this);
    }
    /**首页加载 */
    setup() {
        console.log("初始化加载000000000000000000000000！");
        if (this._isSetUp) {
            return;
        }
        else {
            this.loadThings();
            console.log("this._isSetUp=", this._isSetUp, '<<<<');
        }
    }
    /**套圈资源加载 */
    loadThings() {
        let self = this;
        let body;
        body = this._body = new engine.Rect;
        body.width = 0.0001;
        body.height = 0.0001;
        this.addChild(body);
        // let gift4 = this.addRes(body, '礼盒', 35, 115, 215, 122);
        let gift0 = this.addRes(body, '礼盒', 250, 100, 247, 188);
        let gift1 = this.addRes(body, '礼盒', 485, 85, 247, 190);
        let gift2 = this.addRes(body, '礼盒', 385, -15, 222, 170);
        let gift3 = this.addRes(body, '礼盒', 165, -15, 222, 170);
        let gift4 = this.addRes(body, '礼盒', 20, 85, 247, 190);
        this.listGift = [gift0, gift1, gift2, gift3, gift4];
        let circle = this.addRes(body, '圈', 260, 350, 240, 80);
        let giftSvga = createSvga("套中动效");
        giftSvga.visible = false;
        body.addChild(giftSvga);
        let [v2X, v2Y] = props.ferruleModuleGlobalPosition;
        body.x = v2X;
        body.y = v2Y;
        /* VVVVVVVVVVVVVVVV */
        // let btn = this.addRes(body, '礼盒', 300, 900, 150, 150);
        // btn.addEventListener(engine.MouseEvent.MOUSE_DOWN, function () {
        //     self.isMove = true;
        // }, this);
        // let btn1 = this.addRes(body, '礼盒', 500, 900, 200, 200);
        // btn1.addEventListener(engine.MouseEvent.MOUSE_DOWN, function () {
        //     self.isChoose = true;
        //     giftSvga.visible = false;
        //     console.log(body.children[0], body.children[1], body.children[2], body.children[3], body.children[4], body.children[5]);
        //     body.children[self.num].visible = true;
        //     circle.x = 260; circle.y = 350; circle.width = 240; circle.height = 80;
        //     circle.visible = true;
        // }, this);
        /* AAAAAAAAAAAAAAAAA */
        engine.globalEvent.addEventListener("circle-game-start", function () {
            self.isMove = true;
            console.log('this.isMove===', self.isMove);
        }, this);
        engine.globalEvent.addEventListener("circle-game-init", function () {
            self.isChoose = true;
            giftSvga.visible = false;
            // console.log(body.children[0], body.children[1], body.children[2], body.children[3], body.children[4], body.children[5]);
            body.children[self.num].visible = true;
            circle.x = 260; circle.y = 350; circle.width = 240; circle.height = 80;
            circle.visible = true;
        }, this);

        circle.addEventListener(engine.Event.ENTER_FRAME, function () {
            if (self.isMove) {
                console.log("物体移动");
                if (self.isChoose) {
                    self.num = Math.floor(Math.random() * self.listGift.length);
                    self.isChoose = false;
                }
                self.nodeMove(circle, self.listGift[self.num], giftSvga);
            }
        });
    }
    /**套中动画播放 */
    aniPlay(node0, targetNode, bool) {
        node0.scaleX = this.currentScale(targetNode.y, this.totleY) - 0.05 * this.currentScale(targetNode.y, this.totleY);
        node0.scaleY = this.currentScale(targetNode.y, this.totleY) - 0.05 * this.currentScale(targetNode.y, this.totleY);
        // node0.anchor.x = node0.width / 2;
        // node0.anchor.y = node0.height / 2;
        node0.x = targetNode.x;
        node0.y = targetNode.y;
        node0.visible = true;
        node0.play(false, false);
        /**动画播放完毕，派发（执行）一个事件 */
        node0.addEventListener(engine.Event.END_FRAME, function () {
            console.log("播放到最后一帧", bool, '<<<');
            engine.globalEvent.dispatchEvent("circle-game-end");
            bool = false;
        })
    }
    /**给场景添加图片,resName:资源名字，v2X：资源X位置, v2Y：资源Y位置, resW：资源Width, resH:资源Height */
    addRes(fatherNode, resName, v2X, v2Y, resW, resH) {
        console.log("进行资源的添加");
        let thing = new engine.Sprite(getTextureByName(resName));
        thing.x = v2X;
        thing.y = v2Y;
        thing.width = resW;
        thing.height = resH;
        return fatherNode.addChild(thing);
    }
    /** circle move  */
    nodeMove(moveNode, targetNode, nodePlay) {
        console.log("moveNode.x=", moveNode.x, "targetNode.x=", targetNode.x);
        let moveX = targetNode.x - moveNode.x;
        let moveY = targetNode.y - moveNode.y;
        console.log('moveX=', moveX, 'moveY=', moveY);
        if (this.isDid) {
            if (Math.abs(moveX) > 2) {
                moveNode.x += moveX / Math.abs(moveX) * 5;
            }
            if (Math.abs(moveY) > 2) {
                moveNode.y += moveY / Math.abs(moveY) * 5;
                moveNode.scaleX = this.currentScale(moveNode.y, this.totleY) + 0.2;
                moveNode.scaleY = moveNode.scaleX;
            }
            if (Math.abs(moveX) < 3 && Math.abs(moveY) < 3) {
                targetNode.visible = false;
                moveNode.visible = false;
                this.aniPlay(nodePlay, targetNode, this.isDid);
                this.isMove = false;
            }
        }
    }
    /**当前位置应该的尺寸大小 */
    currentScale(currentY, totoleDisY) {
        return (currentY + 830) / totoleDisY;
    }
}
