import { getConnectedCog } from "../../utils/CogManager";
import SimplePlatformerControls from "../plugins/SimplePlatformerControls";

export class Player extends Phaser.Physics.Arcade.Sprite {
  speedMax: number;
  speedChange: number;
  jumpPower: number;
  facing: string;
  idle: boolean;
  jumpTimer: number;
  moveSpeed: number;
  ani: string;
  alive: boolean;

  setFacingAsset() {
    this.setTexture(`marty-${this.facing}`);
    this.setDisplaySize(30, 30);
    if (this.facing === 'left' || this.facing === 'right') {
      this.setSize(45, 80);
    } else if (this.facing === 'up' || this.facing === 'down') {
      this.setSize(65, 55);
    }
  }

  constructor(scene: Phaser.Scene, x: number, y: number, key: string, frame: string, facing?: string) {
    super(scene, x, y, key, frame);
    scene.physics.world.enable(this);
    scene.add.existing(this);

    this.setDepth(2);
    // this.setSize(8, 24);
    // this.setOffset(12, 8);
    this.setCollideWorldBounds(true);

    // tweakable stuff
    this.speedMax = this.scene.game.config.physics.arcade?.debug ? 1000 : 120;
    this.speedChange = this.scene.game.config.physics.arcade?.debug ? 1000 : 120;
    this.jumpPower = 278;

    // not tweakable
    this.facing = facing || 'right';
    this.setFacingAsset();
    this.idle = false;
    this.jumpTimer = 0;
    this.moveSpeed = 0;
    this.ani = 'idle-left';
    this.alive = true;

    // let anims = this.anims.animationManager;
    // if (!anims.get('idle-left')) {
    //   anims.create({
    //     key: 'idle-left',
    //     frames: anims.generateFrameNumbers('player', { start: 5, end: 5 }),
    //     frameRate: 10,
    //     repeat: -1
    //   });
    // }
    // if (!anims.get('idle-right')) {
    //   anims.create({
    //     key: 'idle-right',
    //     frames: anims.generateFrameNumbers('player', { start: 0, end: 0 }),
    //     frameRate: 10,
    //     repeat: -1
    //   });
    // }
    // if (!anims.get('run-left')) {
    //   anims.create({
    //     key: 'run-left',
    //     frames: anims.generateFrameNumbers('player', { start: 6, end: 8 }),
    //     frameRate: 10,
    //     repeat: -1
    //   });
    // }
    // if (!anims.get('run-right')) {
    //   anims.create({
    //     key: 'run-right',
    //     frames: anims.generateFrameNumbers('player', { start: 1, end: 3 }),
    //     frameRate: 10,
    //     repeat: -1
    //   });
    // }
    // if (!anims.get('jump-left')) {
    //   anims.create({
    //     key: 'jump-left',
    //     frames: anims.generateFrameNumbers('player', { start: 9, end: 9 }),
    //     frameRate: 10,
    //     repeat: -1
    //   });
    // }
    // if (!anims.get('jump-right')) {
    //   anims.create({
    //     key: 'jump-right',
    //     frames: anims.generateFrameNumbers('player', { start: 4, end: 4 }),
    //     frameRate: 10,
    //     repeat: -1
    //   });
    // }
    // if (!anims.get('flail-left')) {
    //   anims.create({
    //     key: 'flail-left',
    //     frames: anims.generateFrameNumbers('player', { start: 9, end: 9 }),
    //     frameRate: 10,
    //     repeat: -1
    //   });
    // }
    // if (!anims.get('flail-right')) {
    //   anims.create({
    //     key: 'flail-right',
    //     frames: anims.generateFrameNumbers('player', { start: 4, end: 4 }),
    //     frameRate: 10,
    //     repeat: -1
    //   });
    // }
    // if (!anims.get('slide-left')) {
    //   anims.create({
    //     key: 'slide-left',
    //     frames: anims.generateFrameNumbers('player', { start: 10, end: 10 }),
    //     frameRate: 10,
    //     repeat: -1
    //   });
    // }
    // if (!anims.get('slide-right')) {
    //   anims.create({
    //     key: 'slide-right',
    //     frames: anims.generateFrameNumbers('player', { start: 11, end: 11 }),
    //     frameRate: 10,
    //     repeat: -1
    //   });
    // }
    this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
  }

  update(controls: SimplePlatformerControls, time: number, delta: number) {

    if (!this.alive) {
      return;
    }

    // in a later tutorial i will show you how you can climb and swim
    this.runAndJump(controls, time, delta);

    // don't forget to animate :)
    // this.anims.play(this.ani, true);
  }

  runAndJump(controls: SimplePlatformerControls, time: number, delta: number) {
    this.body = this.body as Phaser.Physics.Arcade.Body;
    this.body.setVelocityX(0);
    this.body.setVelocityY(0);
    this.body.allowGravity = false;

    // const cog = getConnectedCog();
    // if (cog) {
    //   if (cog.raftStateInfo?.deviceManager) {
    //     const accelData = cog.publishedDataAnalyser.PublishedDataGetter.getAccelerometerData(cog.raftStateInfo?.deviceManager);
    //     accelData?.ax && this.setVelocityX(accelData.ax * 100);
    //     accelData?.ay && this.setVelocityY(accelData.ay * 100);
    //   }
    // }

    if (controls.left) {
      this.moveSpeed -= this.speedChange;
      this.moveSpeed = Math.max(this.moveSpeed, -this.speedMax);
      this.body.setVelocityX(this.moveSpeed);

      this.facing = 'left';
      this.idle = false;

    } else if (controls.right) {
      this.moveSpeed += this.speedChange;
      this.moveSpeed = Math.min(this.moveSpeed, this.speedMax);
      this.body.setVelocityX(this.moveSpeed);

      this.facing = 'right';
      this.idle = false;

    } else if (controls.down) {
      this.moveSpeed += this.speedChange;
      this.moveSpeed = Math.max(this.moveSpeed, -this.speedMax);
      this.body.setVelocityY(this.moveSpeed * .5);

      this.facing = 'down';
      this.idle = false;
    } else if (controls.up) {
      this.moveSpeed -= this.speedChange;
      this.moveSpeed = Math.min(this.moveSpeed, this.speedMax);
      this.body.setVelocityY(this.moveSpeed * .5);

      this.facing = 'up';
      this.idle = false;
    } else {
      this.moveSpeed += (0 - this.moveSpeed) / 2;
      this.body.setVelocityX(this.moveSpeed);
      this.body.setVelocityY(this.moveSpeed);
      this.idle = true;
    }

    this.setFacingAsset();
    if (controls.aDown && (this.body.onFloor() || this.body.onWall()) && time > this.jumpTimer) {
      this.body.setVelocityY(-this.jumpPower);
      this.jumpTimer = time + 250;

      if (this.body.blocked.left) {
        this.moveSpeed = this.speedMax;
        this.setVelocityX(this.moveSpeed);
      } else if (this.body.blocked.right) {
        this.moveSpeed = -this.speedMax;
        this.setVelocityX(this.moveSpeed);
      }
    }

    if (this.body.onFloor()) {

      if (this.idle) {

        if (this.facing === 'left') {
          this.ani = 'idle-left';
        } else {
          this.ani = 'idle-right';
        }

      } else {

        if (this.facing === 'left') {
          this.ani = 'run-left';
        } else {
          this.ani = 'run-right';
        }

      }

    } else {

      if (this.body.blocked.left) {

        this.ani = 'slide-left';

      } else if (this.body.blocked.right) {

        this.ani = 'slide-right';

      } else if (this.body.velocity.y < 0) {

        if (this.facing === 'left') {
          this.ani = 'jump-left';
        } else {
          this.ani = 'jump-right';
        }

      } else {

        if (this.facing === 'left') {
          this.ani = 'flail-left';
        } else {
          this.ani = 'flail-right';
        }

      }
    }
  }

  disappear() {
    this.body = this.body as Phaser.Physics.Arcade.Body;
    this.alive = false;
    this.visible = false;
    this.body.enable = false;
  }
}
