import Utils from "../Game/Utils";

const {ccclass, property} = cc._decorator;

@ccclass
export default class SettlePanel extends cc.Component {
    @property({type:cc.SpriteFrame})
    avatarFrameBlue:cc.SpriteFrame = null;
    @property({type:cc.SpriteFrame})
    avatarFramePink:cc.SpriteFrame = null;
    @property({type:cc.SpriteFrame})
    male:cc.SpriteFrame = null;
    @property({type:cc.SpriteFrame})
    female:cc.SpriteFrame = null;

    private playerInfo:MatchPlayerInfo;
    private canRestart:boolean = true;

    setPlayerInfo(playerInfo: MatchPlayerInfo){
        this.playerInfo = playerInfo;
    }

    onLoad(){
        let playerFrame = this.node.getChildByName("PlayerFrame");
        let avatar = playerFrame.getChildByName("Mask").getChildByName("Avatar").getComponent(cc.Sprite);
        let nickName = playerFrame.getChildByName("Layout").getChildByName("NickName").getComponent(cc.Label);
        let gender = playerFrame.getChildByName("Layout").getChildByName("Gender").getComponent(cc.Sprite);
        let avatarFrame = playerFrame.getChildByName("AvatarFrame").getComponent(cc.Sprite);
        
        nickName.string = this.playerInfo.nickname;
        if(this.playerInfo.gender==1){
            gender.spriteFrame = this.female;
            avatarFrame.spriteFrame = this.avatarFramePink;
        } else {
            gender.spriteFrame = this.male;
            avatarFrame.spriteFrame = this.avatarFrameBlue;
        }
        Utils.LoadSpriteFrame(this.playerInfo.avatarUrl, (spriteFrame) => {
            avatar.spriteFrame = spriteFrame;
        })

        let restartBtn = this.node.getChildByName("Restart").getComponent(cc.Button);
        restartBtn.node.on(cc.Node.EventType.TOUCH_END,()=>{
            if (!this.canRestart) return;
            this.canRestart = false;
            restartBtn.interactable = false;
            this.node.runAction(
                cc.sequence(
                    cc.fadeOut(1),
                    cc.callFunc(()=>{
                        cc.director.loadScene("Game");
                    })
                )
            );
        });
    }
    
    public static Show(playerInfo: MatchPlayerInfo): void {
        let node = cc.instantiate(window.rs.pf_settlePanel);
        node.setPosition(cc.Camera.main.node.getPosition());
        let settlePanel = node.getComponent(SettlePanel);
        settlePanel.setPlayerInfo(playerInfo);
        cc.find("Canvas").addChild(node);
        cc.audioEngine.playEffect(window.rs.ac_win,false);
    }
}
