import { RandomInt } from "../script/util";

export class GameServer {
  playerList = [];
  playerMaxNumber = 0;
  networkHelper = null;
  nameList = [
    'Chris', 'Benny', 'Leslie', 'Devin', 'Sam',
    'Kerry', 'Joey', 'Hailey', 'Fred', 'Arrow',
    'Tony', 'Michael',
  ];
  headUrls = [
    'http://staging-teach.cdn.ireadabc.com/0751c28419a0e8ffb1f0e84435b081ce.png',
    'http://staging-teach.cdn.ireadabc.com/f596e586a31ac2813b1702557941c022.png',
    'http://staging-teach.cdn.ireadabc.com/f0f396c52c42239913fd94523db979fa.png',
    'http://staging-teach.cdn.ireadabc.com/9f2d2ef622fa33d4e2abcbdc4b40ab6a.png',
    'http://staging-teach.cdn.ireadabc.com/f40ce4a8630039c4cf48a63fc1399c52.png'
  ];

  constructor(playerMaxNumber, networkHelper) {
    this.playerMaxNumber = playerMaxNumber;
    this.networkHelper = networkHelper;

    this._timeoutIds = [];
    this._intervalIds = [];

    this.headUrls = this.headUrls.sort((a, b) => RandomInt(2) - 1);
    this.nameList = this.nameList.sort((a, b) => RandomInt(2) - 1);

    this.networkHelper.startFrameSync();
  }

  addPlayer(player) {
    this.playerList.push({
      name: this.nameList.splice(0, 1)[0],
      uuid: player.uuid,
      isAI: player.isAI,
      headUrl: this.headUrls.splice(0, 1)[0],
      complete: false,
      duration: null
    });

    if (player.isAI) {
      if (this.playerList.length == this.playerMaxNumber) {
        this.networkHelper.closeRoom();
        this.startAIDuration();
      }

      this.networkHelper.sendFrame({
        type: 'all_player_info',
        playerData: this.playerList
      });
    }
  }

  onFrameEvent(event) {
    if (event.type == "complete") {
      this.onPlayerComplete(event.uuid, event.duration);
    } else if (event.type == "playerInfo") {
      const player = this.playerList.find(p => p.uuid == event.playerId);
      player.name = event.playerName;
      player.headUrl = event.playerHeadUrl;
      if (this.playerList.length == this.playerMaxNumber) {
        this.networkHelper.closeRoom();
        this.startAIDuration();
      }
      this.networkHelper.sendFrame({
        type: 'all_player_info',
        playerData: this.playerList
      });
    }
  }

  onPlayerComplete(uuid: string, duration: number) {
    const player = this.playerList.find(player => player.uuid == uuid);
    player.complete = true;
    player.duration = duration;

    this.networkHelper.sendFrame({
      type: 'player_complete',
      playerData: this.playerList
    });
  }

  onPlayerJoin(event) {
    this.addPlayer({
      uuid: event.data.joinPlayerId,
      isAI: false
    });
  }

  startMatch() {
    for (let i = 0; i < this.playerMaxNumber; i++) {
      this._timeoutIds.push(setTimeout(() => {
        const count = this.playerMaxNumber - this.playerList.length;
        if (count > 0) {
          this.addPlayer({
            uuid: 'ai_' + i,
            isAI: true
          });
        }
      }, RandomInt(10000, 15000)));
    }

  }

  _timeoutIds = null;
  _intervalIds = null;
  // 生命周期
  onDestroy() {
    this._timeoutIds.forEach(id => {
      clearTimeout(id);
    });
    this._intervalIds.forEach(id => {
      clearInterval(id);
    });
  }

  startAIDuration() {
    this.playerList
      .filter(player => player.isAI == true)
      .forEach(player => {
        const duration = RandomInt(15000, 20000);
        this._timeoutIds.push(setTimeout(() => {
          player.complete = true;
          player.duration = duration;

          this.networkHelper.sendFrame({
            type: 'player_complete',
            playerData: this.playerList
          });
          console.log(JSON.stringify(this.playerList));
        }, duration + 3000));
      });
  }
}