use networking::serverstate::{ClientId, ServerState};
use entity::gateway::{EntityGateway, InMemoryGateway};
use elseware::ship::ship::{ShipServerState, RecvShipPacket};

use libpso::character::settings::{DEFAULT_KEYBOARD_CONFIG1, DEFAULT_KEYBOARD_CONFIG2, DEFAULT_KEYBOARD_CONFIG3, DEFAULT_KEYBOARD_CONFIG4};
use libpso::packet::ship::*;

#[path = "common.rs"]
mod common;
use common::*;

#[async_std::test]
async fn test_save_options() {
    let mut entity_gateway = InMemoryGateway::default();

    let (user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    join_lobby(&mut ship, ClientId(1)).await;

    ship.handle(ClientId(1), RecvShipPacket::SaveOptions(SaveOptions{
        options: 12345,
    })).await.unwrap();

    let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
    let char = characters[0].as_ref().unwrap();

    assert!(char.option_flags == 12345);
}

#[async_std::test]
async fn test_change_keyboard_mappings() {
    let mut entity_gateway = InMemoryGateway::default();

    let (user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 2).await;

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    join_lobby(&mut ship, ClientId(1)).await;

    let settings = entity_gateway.get_user_settings_by_user(&user1).await.unwrap();
    assert!(settings.settings.keyboard_config == DEFAULT_KEYBOARD_CONFIG1);

    // update from default2 to default4
    // the client simply sends the full 364 bytes...
    ship.handle(ClientId(1), RecvShipPacket::KeyboardConfig(KeyboardConfig{
        keyboard_config: [
            0, 0, 0, 0, 117, 0, 0, 0, 0, 0, 0, 0, 105, 0, 0, 0,
            0, 0, 0, 0, 113, 0, 0, 0, 0, 0, 0, 0, 115, 0, 0, 0,
            0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0,
            0, 0, 0, 0,  97, 0, 0, 0, 0, 0, 0, 0,  80, 0, 0, 0,
            0, 0, 0, 0,   6, 0, 0, 0, 0, 0, 0, 0,  89, 0, 0, 0,
            0, 0, 0, 0,  94, 0, 0, 0, 0, 0, 0, 0,  93, 0, 0, 0,
            0, 0, 0, 0,  92, 0, 0, 0, 0, 0, 0, 0,  95, 0, 0, 0,
            0, 0, 0, 0,  7,  0, 0, 0, 0, 0, 0, 0,  93, 0, 0, 0,
            0, 0, 0, 0,  92, 0, 0, 0, 0, 0, 0, 0,  95, 0, 0, 0,
            0, 0, 0, 0,  86, 0, 0, 0, 0, 0, 0, 0,  94, 0, 0, 0,
            0, 0, 0, 0,  64, 0, 0, 0, 0, 0, 0, 0,  65, 0, 0, 0,
            0, 0, 0, 0,  66, 0, 0, 0, 0, 0, 0, 0,  67, 0, 0, 0,
            0, 0, 0, 0,  68, 0, 0, 0, 0, 0, 0, 0,  69, 0, 0, 0,
            0, 0, 0, 0,  70, 0, 0, 0, 0, 0, 0, 0,  71, 0, 0, 0,
            0, 0, 0, 0,  72, 0, 0, 0, 0, 0, 0, 0,  73, 0, 0, 0,
            0, 0, 0, 0,  74, 0, 0, 0, 0, 0, 0, 0,  75, 0, 0, 0,
            0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0,
            0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  42, 0, 0, 0,
            0, 0, 0, 0,  43, 0, 0, 0, 0, 0, 0, 0,  44, 0, 0, 0,
            0, 0, 0, 0,  45, 0, 0, 0, 0, 0, 0, 0,  46, 0, 0, 0,
            0, 0, 0, 0,  47, 0, 0, 0, 0, 0, 0, 0,  48, 0, 0, 0,
            0, 0, 0, 0,  49, 0, 0, 0, 0, 0, 0, 0,  50, 0, 0, 0,
            0, 0, 0, 0,  51, 0, 0, 0, 1, 0, 0, 0
        ],
    })).await.unwrap();

    let settings = entity_gateway.get_user_settings_by_user(&user1).await.unwrap();
    assert!(settings.settings.keyboard_config == DEFAULT_KEYBOARD_CONFIG4);
}