extern crate rltk;
use rltk::{Point, Rltk, VirtualKeyCode};
extern crate specs;
use super::{CombatStats, Map, Player, Position, RunState, State, Viewshed, WantsToMelee};
use specs::prelude::*;
use std::cmp::{max, min};


pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
    let mut positions = ecs.write_storage::<Position>();
    let players = ecs.read_storage::<Player>();
    let mut viewsheds = ecs.write_storage::<Viewshed>();
    let entities = ecs.entities();
    let combat_stats = ecs.read_storage::<CombatStats>();
    let map = ecs.fetch::<Map>();
    let mut wants_to_melee = ecs.write_storage::<WantsToMelee>();

    for (entity, _player, pos, viewshed) in
        (&entities, &players, &mut positions, &mut viewsheds).join()
    {
        let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);

        for potential_target in map.tile_content[destination_idx].iter() {
            let target = combat_stats.get(*potential_target);
            if let Some(_target) = target {
                wants_to_melee
                    .insert(
                        entity,
                        WantsToMelee {
                            target: *potential_target,
                        },
                    )
                    .expect("Add target failed");
                return;
            }
        }

        if !map.blocked[destination_idx] {
            pos.x = min(79, max(0, pos.x + delta_x));
            pos.y = min(49, max(0, pos.y + delta_y));

            viewshed.dirty = true;
            let mut ppos = ecs.write_resource::<Point>();
            ppos.x = pos.x;
            ppos.y = pos.y;
        }
    }
}

#[derive(Component)]
enum GameEvent {
    MoveTo { destination: usize },
    Attack { target: Entity },
}

struct HandleInputEvent;

impl<'a> System<'a> for HandleInputEvent {
    type SystemData = (
        Write<'a, InputEvent>,
        ReadExpect<'a, Map>,
        Entities<'a>,
        ReadStorage<'a, Player>,
        ReadStorage<'a, Position>,
        ReadStorage<'a, CombatStats>,
        WriteStorage<'a, GameEvent>,
    );

    fn run(&mut self, (mut event, map, entities, player, position, stats, mut game_events): Self::SystemData) {
        match *event {
            InputEvent::PlayerMovement { delta_x, delta_y } => {
                for (e, _, pos) in (&entities, &player, &position).join() {
                    // depending on what's in the target cell, we
                    // either want to attack or move
                    let destination = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
                    if let Some(target) = map.get_attackable(destination, &stats) {
                        game_events.insert(e, GameEvent::Attack { target }).expect("Unable to add GameEvent");
                    } else {
                        game_events.insert(e, GameEvent::MoveTo { destination }).expect("Unable to add GameEvent");
                    }
                }
            },
            InputEvent::NoAction => {}
        }
        *event = InputEvent::NoAction;
    }
}


/// This type represents all the "low-level" actions we have available
/// to us, corresponding directly to the set of actions we have bound
/// on the keys
enum InputEvent {
    // attempt to move the player some amount in the given direction
    // (delta_x and delta_y should be -1, 0, or 1)
    PlayerMovement {
        delta_x: i32,
        delta_y: i32,
    },
    // do not take any action at all
    NoAction,
}

impl Default for InputEvent {
    // the default for an InputEvent is of course NoAction
    fn default() -> InputEvent {
        InputEvent::NoAction
    }
}

pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
    // Player movement
    match ctx.key {
        None => return RunState::AwaitingInput, // Nothing happened
        Some(key) => match key {
            VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::H =>
                gs.ecs.insert(InputEvent::PlayerMovement { delta_x: -1, delta_y: 0 }),

            VirtualKeyCode::Right | VirtualKeyCode::Numpad6 | VirtualKeyCode::L =>
                gs.ecs.insert(InputEvent::PlayerMovement { delta_x: 1, delta_y: 0 }),

            VirtualKeyCode::Up | VirtualKeyCode::Numpad8 | VirtualKeyCode::K =>
                gs.ecs.insert(InputEvent::PlayerMovement { delta_x: 0, delta_y: -1 }),

            VirtualKeyCode::Down | VirtualKeyCode::Numpad2 | VirtualKeyCode::J =>
                gs.ecs.insert(InputEvent::PlayerMovement { delta_x: 0, delta_y: 1 }),

            // Diagonals
            VirtualKeyCode::Numpad9 | VirtualKeyCode::U =>
                gs.ecs.insert(InputEvent::PlayerMovement { delta_x: 1, delta_y: -1 }),

            VirtualKeyCode::Numpad7 | VirtualKeyCode::Y =>
                gs.ecs.insert(InputEvent::PlayerMovement { delta_x: -1, delta_y: -1 }),

            VirtualKeyCode::Numpad3 | VirtualKeyCode::N =>
                gs.ecs.insert(InputEvent::PlayerMovement { delta_x: 1, delta_y: 1 }),

            VirtualKeyCode::Numpad1 | VirtualKeyCode::B =>
                gs.ecs.insert(InputEvent::PlayerMovement { delta_x: -1, delta_y: 1 }),

            _ => return RunState::AwaitingInput,
        },
    }
    RunState::PlayerTurn
}
