import { UserMgr } from "../../module_basic/scripts/UserMgr";
import {
  IFood,
  IGameData,
  IGamePlayer,
} from "../../module_basic/shared/protocols/public/game/GameTypeDef";
import { IRoomData } from "../../module_basic/shared/types/RoomData";
import { GameLocalScene } from "./GameSceneLocal";

export class SinglePlayerData implements IGamePlayer {
  uid: string;
  color: number;
  playerId: number;
  roleName: string;
  location: string;
  transform: number[];
  reviveTime: number;
  protectedTime: number;
  weight: number;
  radius: number;
  speed: number;
  teamId: number;
  state: "" | "moving";
}

export class GameLocalData {
  createLocalGameData(p: IGamePlayer): IGameData {
    let gameData: IGameData = {
      mapWidth: GameLocalScene.MAP_WIDTH,
      mapHeight: GameLocalScene.MAP_HEIGHT,
      defaultPlayerCellRadius: 10,
      gameState: "playing",
      gameStateRemainingTime: 300,
      players: [p],
      foodList: {},
      teamsWeights: {},
    };
    return gameData;
  }

  mockIGamePlayer(): IGamePlayer {
    // UserMgr.inst.uid = "local-me-uid-xxx-001";

    let a: SinglePlayerData = new SinglePlayerData();
    a.uid = UserMgr.inst.uid;
    a.color = 3;
    a.playerId = 1;
    a.roleName = "Local";
    a.transform = [550, 500, 0, 0];
    a.reviveTime = 0;
    a.protectedTime = 0;
    a.weight = 30;
    a.radius = 200;
    a.speed = 500;
    a.teamId = 1;
    a.state = "moving";
    return a;
  }

  private _autoFoodId = 1;

  newFoodData(): IFood {
    return {
      id: this._autoFoodId++,
      x:
        Math.random() * GameLocalScene.MAP_WIDTH - GameLocalScene.MAP_WIDTH / 2,
      y:
        Math.random() * GameLocalScene.MAP_HEIGHT -
        GameLocalScene.MAP_HEIGHT / 2,
      type: Math.floor(Math.random() * 4),
      color: this.getRandomColor(),
      radius: Math.random() * 40 + 10,
    };
  }
  private static _ins: GameLocalData = null;
  public static get ins(): GameLocalData {
    if (!this._ins) {
      this._ins = new GameLocalData();
    }
    return this._ins;
  }

  private getRandomColor(): number {
    // 方法1：返回随机数字
    return Math.floor(Math.random() * 0xffffff);

    // 方法2：从预定义颜色中选择
    const colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
    return colors[Math.floor(Math.random() * colors.length)];
  }

  createLocalRoomData(uid: string): IRoomData {
    let a = {
      id: "local-room-id-xxx-001",
      displayId: "local-room-id-xxx-001",
      gameType: "local",
      name: "Local",
      maxUser: 10,
      userList: [
        {
          uid: uid,
          name: "Local",
          visualId: 1,
          gender: 1,
          introduction: "Local",
          isOnline: true,
          ready: true,
        },
      ],
      maxPlayerNum: 2,
      playerNum: 1,
      isPlaying: true,
      messages: [],
    };
    return a;
  }
}
