// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Player from "./player_PK_Game";

const { ccclass, property } = cc._decorator;

@ccclass
export default class EndLayer extends cc.Component {

    @property(cc.Node)
    winRoot: cc.Node = null;
    @property(cc.Node)
    loseRoot: cc.Node = null;

    @property(cc.Node)
    winGold: cc.Node = null;
    @property(cc.Node)
    loseGold: cc.Node = null;

    @property(cc.Node)
    originNode: cc.Node = null;

    @property(cc.Button)
    btn_restart: cc.Button = null;

    @property(cc.Node)
    blueHead: cc.Node = null;
    @property(cc.Label)
    blueScore: cc.Label = null;
    @property(cc.Label)
    blueName: cc.Label = null;

    @property(cc.Node)
    redHead: cc.Node = null;
    @property(cc.Label)
    redScore: cc.Label = null;
    @property(cc.Label)
    redName: cc.Label = null;
    mainCom: any;
    endpos: cc.Vec3;



    onLoad() {

        this.winGold.active = false;
        this.loseGold.active = false;
        this.endpos = this.winGold.position;
        this.btn_restart.node.on("click", this.onClickRestart, this)

        cc.tween(this.btn_restart.node).to(0.3, { scale: 1.1 }).to(0.2, { scale: 1 }).delay(1).union().repeatForever().start();

        this.btn_restart.node.opacity = 0;
    }

    onClickRestart() {

        this.mainCom.backHome();

    }
    init(data, mydata, otherData) {

        this.loseRoot.active = false;
        this.winRoot.active = false;
        this.winGold.active = false;
        this.loseGold.active = false;

        let root = this.loseRoot;
        let golden = this.loseGold;
        if (data.status.totalAddBlueScore >= data.status.totalAddRedScore) {
            root = this.winRoot;
            golden = this.winGold;
            this.mainCom.playLocalAudio("win")
        } else {

            this.mainCom.playLocalAudio("lose")
        }
        golden.active = true;
        golden.x = this.originNode.x;
        golden.y = this.originNode.y;
        golden.scale = this.originNode.scale;

        root.active = true;
        root.opacity = 0;
        cc.tween(root).delay(0.2).to(1, { opacity: 255 }).call(() => {
            this.btn_restart.node.opacity = 255;
        }).start();
        cc.tween(golden).to(0.5, { x: this.endpos.x, y: this.endpos.y, scale: 1 }, { easing: "sineOut" }).start()


        let icon = root.getChildByName("icon")
        icon.opacity = 0;
        icon.y += 30
        cc.tween(icon).delay(0.5).by(0.3, { y: -30, opacity: 255 }, { easing: "sinInOut" }).start()


        this.blueHead.getComponent(Player).init(mydata.url);
        this.blueName.string = mydata.name;
        this.blueScore.string = data.status.totalAddBlueScore

        this.redHead.getComponent(Player).init(otherData.headUrl);
        this.redName.string = otherData.name;
        this.redScore.string = data.status.totalAddRedScore


    }

    // update (dt) {}
}
