use cat_box::{Game, Sprite, SpriteCollection, get_keyboard_state, draw_text,physics};
use std::{time::{Instant}};
use sdl2::keyboard::Scancode;

//use gravitas::{Simulation,Gravity};

/* 
keep track of accel, vel and pos
every tick:
1. pos += vel
2. vel += accel
3. accel = (calculate from arrow keys) + (0, -9.8)
*/
fn main() {
    let game = Game::new("Running", 500, 500);

    //main sprite introductions
    let mut i = 255u8;
    //gravity setup
    //let meter = 13;
    let now = Instant::now();
    
    let mut background = SpriteCollection::new();
    let mut run1 = Sprite::new("runner_0.png", 200, 320).unwrap();
    let sun = Sprite::new("sun.png", 50, 50).unwrap();
    let grass = Sprite::new("grass.png", 250, 450).unwrap();
    /*
    let mut grav = Gravity::new(2.0);
    grav.set(0.0, 3.0);
    */
    
    background.push(sun);
    
    background.push(grass);
    /*
    sprites.push(stand);
    
    let _guy = State {
        running: false,
        jumping: false
    };*/
    
    //sleeper agent
    //let sleep = thread::sleep;
    //main game loop
    game.run(|ctx|{
        let keys = get_keyboard_state(ctx).keys;
        
        
        
        if i<200{
            i = i + 10;
        }else if i >= 255{
            i=200;
        }

        
        let (x,y) = run1.position();
        run1.translate((0,-3));

        if x < 0 {
            run1.translate((x*-1+5,0));
        } else if x > 500 {
            run1.translate((500-x-5,0));
        }
        //- run1.rect.height() as i32
        if y < 0{
            run1.translate((0,y - run1.rect.height() as i32))
        }else if y > 320 {
            run1.translate((0,5));
        }

        
        draw_text(
            ctx, 
            format!("x is {} and y is {}, {:?} Seconds have passed", x, y, now.elapsed().as_secs()), 
            "MesloLGS.ttf", 
            15, 
            (250,100), 
            cat_box::TextMode::Shaded {
                foreground: (255, 255, 255),
                background: (0, 0, 0),
            },
        ).unwrap();

        for key in keys {
            let offset = match key {
                Scancode::Escape | Scancode::Q => {
                    game.terminate();
                    (0, 0)
                 } //Simulation::x(&grav,1.0) as i32
                Scancode::W | Scancode::Up => (0, 3),
                Scancode::S | Scancode::Down => (0, -5),
                Scancode::A | Scancode::Left => (-5, 0),
                Scancode::D | Scancode::Right => (5, 0),
                _ => (0, 0),
            }; 
            
            //if physics::check_for_collision(&run1, background.get_mut(1).unwrap()){
                run1.translate((offset.0,offset.1));
            
            //}

             
            
        }
        
        ctx.set_background_colour(129, 201, i);
        background.draw(ctx).unwrap();
        run1.draw(ctx).unwrap();
        
        
    }).unwrap();
}
