import { _decorator } from 'cc';
const { ccclass } = _decorator;

@ccclass('Player')
export class Player {
    private static _instance: Player;

    /** 玩家ID（可从服务端返回赋值） */
    public playerId: string = "";

    /** 玩家昵称 */
    public nickname: string = "Guest";

    /** 玩家等级 */
    public level: number = 1;

    /** 玩家当前分数 */
    public score: number = 0;

    /** 玩家货币类型（比如 "USD", "CNY", "GOLD"） */
    public currencyType: string = "GOLD";

    public betConfig: number[] = [];

    /** 玩家登录状态 */
    public isLoggedIn: boolean = false;

    private constructor() {}

    /**
     * 获取单例实例
     */
    public static getInstance(): Player {
        if (!this._instance) {
            this._instance = new Player();
        }
        return this._instance;
    }

    /**
     * 初始化或从服务端同步玩家数据
     */
    public init(data: any) {
        if (!data) return;
        this.playerId = data.playerId || this.playerId;
        this.nickname = data.nickname || this.nickname;
        this.level = data.level || this.level;
        this.score = data.score || this.score;
        this.currencyType = data.currencyType || this.currencyType;
        this.betConfig = data.betConfig || this.betConfig;
        this.isLoggedIn = true;
    }

    /**
     * 增加分数
     */
    public addScore(amount: number) {
        this.score += amount;
        if (this.score < 0) this.score = 0;
    }

    /**
     * 增加金币或钻石
     */
    public addCurrency(type: 'coins' | 'gems', amount: number) {

    }

    /**
     * 消耗货币
     */
    public spendCurrency(type: 'coins' | 'gems', amount: number): boolean {
        return false;
    }

    /**
     * 重置玩家数据（例如登出时）
     */
    public reset() {
        this.playerId = "";
        this.nickname = "Guest";
        this.level = 1;
        this.score = 0;
        this.currencyType = "GOLD";
        this.isLoggedIn = false;
    }

    /**
     * 打印调试信息
     */
    public debugInfo() {
        console.log("=== Player Info ===");
        console.log(`ID: ${this.playerId}`);
        console.log(`Name: ${this.nickname}`);
        console.log(`Level: ${this.level}`);
        console.log(`Score: ${this.score}`);
        console.log(`Currency: ${this.currencyType}`);
        console.log("====================");
    }
}
