//    Copyright 2025 Bill Rossi
//    
//    This file is part of Starship Futuretime
//
//    Starship Futuretime is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
//    Starship Futuretime is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License along with Starship Futuretime. If not, see <https://www.gnu.org/licenses/>. 
#include <stdio.h>
#include <raylib.h>

#include "player.h"

void initialize_player(Game *g, Player *p) {
  p->position = (Vector2) { 400, 300 };
  p->velocity = (Vector2) { 0, 0 };
  p->acceleration = (Vector2) { 0, 0 };
  p->pitch = PITCH_NEUTRAL;
  p->roll = ROLL_NEUTRAL;
  Image sprite_img = LoadImage("img/player.png");
  p->spritesheet = LoadTextureFromImage(sprite_img);
  UnloadImage(sprite_img);
  p->game = g;
}

void handle_player_input(Player *p) {
  if (IsKeyDown(KEY_DOWN)) p->pitch = PITCH_DESCENDING;
  else if (IsKeyDown(KEY_UP)) p->pitch = PITCH_ASCENDING;
  else p->pitch = PITCH_NEUTRAL;

  if (IsKeyDown(KEY_LEFT)) p->roll = ROLL_LEFT;
  else if (IsKeyDown(KEY_RIGHT)) p->roll = ROLL_RIGHT;
  else p->roll = ROLL_NEUTRAL;
}

void draw_player(Player *p) {
  DrawTextureRec(p->spritesheet, (Rectangle) { 128, 128, 128, 128 }, (Vector2) { p->position.x - 64, p->position.y - 29 }, WHITE);
}

void move_player(Player *p) {
  switch (p->roll) {
  case ROLL_LEFT: p->acceleration.x = -ACCEL_X; break;
  case ROLL_NEUTRAL: p->acceleration.x = 0; break;
  case ROLL_RIGHT: p->acceleration.x = ACCEL_X; break;
  }

  switch (p->pitch) {
  case PITCH_DESCENDING: p->acceleration.y = .4; break;
  case PITCH_NEUTRAL: p->acceleration.y = 0; break;
  case PITCH_ASCENDING: p->acceleration.y = -.4; break;
  }

  p->velocity.x += p->acceleration.x;
  p->velocity.x = CLAMP(p->velocity.x, MAX_VEL_X);
  p->velocity.x *= DAMPING_X;
  
  p->velocity.y += p->acceleration.y;
  p->velocity.y = MAX(MIN(p->velocity.y, -MIN_VEL_Y), -MAX_VEL_Y);

  p->position.x += p->velocity.x;
  p->position.y += p->velocity.y;

  p->game->camera->target = p->position;
  int level_width_in_pixels = p->game->level->width * 32;
  bool too_far_left = p->position.x < 400;
  bool too_far_right = level_width_in_pixels - p->position.x < 400;
  if (too_far_left) {
    p->game->camera->offset.x = p->position.x;
  } else if (too_far_right) {
    p->game->camera->offset.x = p->position.x - 800;
  } else {
    p->game->camera->offset.x = 400;
    p->game->camera->offset.y = 275;
  }
}