#include "tank.h"
#include "globals.h"
#include "math.h"
#include "game_synchronizer.h"
extern Serial pc;
extern Game_Synchronizer sync;
extern uLCD_4DGL uLCD;
// sx is the x-coord of the bottom left corner of the tank
// sy is the y-coord of the same corner
// width is the width of the tank
// height is the height of the tank
Tank::Tank(int sx, int sy, int startx, int width, int height, int color) {
    x = sx; y = sy;
    w = width; h = height;
    start_x = startx;
    tank_color = color;
    wheel_color = BLACK;
    barrel_theta = PI/4.0;
    barrel_length = w;
    wheel_rad = 2.0;
    original_color = color;
    isvisible = false;
    visible_count = 2;
    draw();
    

}

// Return the minimum x-coord of your tank's bounding box.
int Tank::min_x(void) { 
    if (start_x < 59) {
       return 15; 
    }
    pc.printf("player2 move\n");
       return 69;
}    

// Return the minimum y-coord of your tank's bounding box.
int Tank::min_y(void) {
    return 0;
}

// Return the maximum x-coord of your tank's bounding box.
int Tank::max_x(void) { 
    if (start_x < 59) {
       return 59-barrel_length-w/2; 
    }
       return 101;
}

// Return the maximum y-coord of your tank's bounding box.
int Tank::max_y(void) {  
    return 1;
}

void Tank::barrel_end(int* bx, int* by,float *tx,float*ty) {
    // Set the x and y coords of the end of the barrel.
    *bx = x + w/2.0 + (barrel_length+1) *cos(barrel_theta);
    *by = y + h + (barrel_length+1) *sin(barrel_theta)+wheel_rad;
    *tx = x + w/2.0;
    *ty = y + h+ wheel_rad;
}

void Tank::reposition(int dx, int dy, float dtheta) {
    sync.filled_rectangle(x+w/2-barrel_length,y,x+w/2+barrel_length,y+h+barrel_length,SKY_COLOR);// Blank out the old tank position, and
    if(((x + dx) >= min_x()) &&((x + dx) <= max_x()) ){
        x = x + dx;//      Move the tank dx pixels in the x direction.
    }
    
    if(((y + dy) > min_y()) &&((y + dy) < max_y()) ){
        y = y +dy;//      Move the tank dy pixels in the y direction.
    }
    
    if ((barrel_theta + dtheta < PI)&&(barrel_theta + dtheta > 0)){//      Move the tank barrel by an angle dtheta. Don't allow it to go below parallel.
    barrel_theta = barrel_theta + dtheta;
    }
    draw();
    // Do collision detection to prevent the tank from hitting things. 
    // (obstacles, side of the screen, other tanks, etc.)
}


// Example tank draw function. We expect you to get creative on this one!
void Tank::draw() {
    sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), tank_color);
    sync.filled_rectangle(x, y+wheel_rad, x+w, y+h+wheel_rad, tank_color);
    sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, wheel_color);
    sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, wheel_color);
}

void Tank ::invisible(){
    if (visible_count > 0){
    tank_color = SKY_COLOR;
    wheel_color = SKY_COLOR;
    draw();
    visible_count--;
    isvisible = true;
    }
    
}

void Tank::turnback(){

    if (visible_count < 0){
    tank_color = original_color;
    wheel_color = BLACK;
    draw();
    isvisible = false;
    }
    if (isvisible){
     visible_count--;
    }
   
}