#include "systems.hpp"
#include <fmt/core.h>
#include <string>
#include <cmath>
#include "rand.hpp"
#include "spatialmap.hpp"
#include "dbc.hpp"
#include "lights.hpp"
#include "events.hpp"
#include "sound.hpp"
#include "ai.hpp"
#include "ai_debug.hpp"
#include "shiterator.hpp"
#include "rituals.hpp"
#include "battle.hpp"
#include <iostream>
#include "shaders.hpp"
#include "inventory.hpp"

using std::string;
using namespace fmt;
using namespace components;
using namespace DinkyECS;
using lighting::LightSource;

void System::lighting(GameLevel &level) {
  auto &light = *level.lights;
  auto &world = *level.world;
  auto &map = *level.map;

  light.reset_light();

  world.query<Position>([&](auto, auto &position) {
    light.set_light_target(position.location);
  });

  light.path_light(map.walls());

  world.query<Position, LightSource>([&](auto ent, auto &position, auto &lightsource) {
    light.render_light(lightsource, position.location);

    if(ent == level.player) {
      light.update_fow(position.location, lightsource);
    }
  });

}

void System::generate_paths(GameLevel &level) {
  auto player = level.world->get_the<Player>();
  const auto &player_position = level.world->get<Position>(player.entity);

  level.map->set_target(player_position.location);
  level.map->make_paths();
}

void System::enemy_ai_initialize(GameLevel &level) {
  auto &world = *level.world;
  auto &map = *level.map;

  world.query<Position, EnemyConfig>([&](const auto ent, auto& pos, auto& config) {
    if(world.has<ai::EntityAI>(ent)) {
      auto& enemy = world.get<ai::EntityAI>(ent);
      auto& personality = world.get<Personality>(ent);

      enemy.set_state("detect_enemy", map.distance(pos.location) < personality.hearing_distance);
      enemy.update();
    } else {
      auto ai_start = ai::load_state(config.ai_start_name);
      auto ai_goal = ai::load_state(config.ai_goal_name);

      ai::EntityAI enemy(config.ai_script, ai_start, ai_goal);
      auto& personality = world.get<Personality>(ent);

      enemy.set_state("tough_personality", personality.tough);
      enemy.set_state("detect_enemy", map.distance(pos.location) < personality.hearing_distance);
      enemy.update();

      world.set<ai::EntityAI>(ent, enemy);
    }
  });
}

void System::enemy_pathing(GameLevel &level) {
  auto &world = *level.world;
  auto &map = *level.map;
  auto player = world.get_the<Player>();

  const auto &player_position = world.get<Position>(player.entity);

  world.query<Position, Motion>([&](auto ent, auto &position, auto &motion) {
    if(ent != player.entity) {
      auto& enemy_ai = world.get<ai::EntityAI>(ent);
      Point out = position.location; // copy

      if(enemy_ai.wants_to("find_enemy")) {
        map.random_walk(out, motion.random, PATHING_TOWARD);
      } else if(enemy_ai.wants_to("run_away")) {
        map.random_walk(out, motion.random, PATHING_AWAY);
      }

      motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
    }
  });

  map.clear_target(player_position.location);
}


inline void move_entity(SpatialMap &collider, Map &game_map, Position &position, Motion &motion, Entity ent) {
  Point move_to = {
    position.location.x + motion.dx,
    position.location.y + motion.dy
  };
  motion = {0,0}; // clear it after getting it

  // it's a wall, skip
  if(!game_map.can_move(move_to)) return;
  // there's collision skip
  if(collider.occupied(move_to)) return;

  // all good, do the move
  collider.move(position.location, move_to, ent);
  position.location = move_to;
}

void System::motion(GameLevel &level) {
  level.world->query<Position, Motion>(
    [&](auto ent, auto &position, auto &motion) {
    // don't process entities that don't move
    if(motion.dx != 0 || motion.dy != 0) {
      move_entity(*level.collision, *level.map, position, motion, ent);
    }
  });
}

void System::distribute_loot(GameLevel &level, Position target_pos) {
  auto& world = *level.world;
  auto& config = world.get_the<GameConfig>();
  int inventory_count = Random::uniform(0, 3);

  if(inventory_count > 0) {
    // do a clone of the things we need, like Position
    auto junk_entity = world.entity();

    ritual::JunkPile pile;
    auto& junk = config.rituals["junk"];

    ritual::JunkPile select_from;
    for(auto& el : junk.items()) {
      select_from.contents.push_back(el.key());
    }

    for(int i = 0; i < inventory_count; i++) {
      size_t max_junk = select_from.contents.size();
      auto& item = select_from.contents.at(Random::uniform(size_t(0), max_junk-1));
      pile.contents.push_back(item);
    }

    auto entity_data = config.devices["GRAVE_STONE"];
    components::configure_entity(world, junk_entity, entity_data["components"]);
    world.set<ritual::JunkPile>(junk_entity, pile);
    // BUG: inventory_count here isn't really used to remove it
    world.set<InventoryItem>(junk_entity, {inventory_count, entity_data});
    world.set<Position>(junk_entity, target_pos);
    level.collision->insert(target_pos.location, junk_entity, false);
    level.world->send<Events::GUI>(Events::GUI::ENTITY_SPAWN, junk_entity, {});
  } else {
    dbc::log("DEAD BODY NOT IMPLEMENTED, for now just removing enemy");
    // BUG: should maybe add a component to the world for "dead thing no loot" that
    // has no collision or goes away after some kind of animation
    // Something like:
    // auto entity_data = config.devices["DEAD_BODY"];
    // components::configure_entity(world, ent, entity_data["components"]);
    // then give it a collision device that makes it go away and make a sound
    // or maybe you can walk over dead bodies and they make a noise
  }
}

void System::death(GameLevel &level) {
  auto &world = *level.world;
  auto player = world.get_the<Player>();
  std::vector<Entity> dead_things;

  world.query<Combat>([&](auto ent, auto &combat) {
    // bring out yer dead
    if(combat.hp <= 0 && !combat.dead) {
      combat.dead = true;
      if(ent != player.entity) {
        // we won't change out the player's components later
        dead_things.push_back(ent);
      }
      // we need to send this event for everything that dies
      world.send<Events::GUI>(Events::GUI::DEATH, ent, {});
    } else if(float(combat.hp) / float(combat.max_hp) < 0.5f) {
      // if enemies are below 50% health they are marked with bad health
      if(world.has<ai::EntityAI>(ent)) {
        auto& enemy_ai = world.get<ai::EntityAI>(ent);
        enemy_ai.set_state("health_good", false);
        enemy_ai.update();
      }
    }
  });

  // this goes through everything that died and changes them to a gravestone
  // NOTE: this could be a separate system but also could be a function in
  // components::
  for(auto ent : dead_things) {
    if(auto snd = world.get_if<Sound>(ent)) {
      sound::stop(snd->attack);
      sound::play(snd->death);
    }

    auto pos = world.get<Position>(ent);

    // need to remove _after_ getting the position
    level.collision->remove(pos.location, ent);

    // distribute_loot is then responsible for putting something there
    System::distribute_loot(level, pos);

    world.destroy(ent);
  }
}

inline void animate_entity(World &world, Entity entity) {
  if(world.has<Animation>(entity)) {
    auto& animation = world.get<Animation>(entity);
    animation.play();
  }

  if(auto snd = world.get_if<Sound>(entity)) {
    sound::play(snd->attack);
  }
}

void System::combat(GameLevel &level, int attack_id) {
  auto &collider = *level.collision;
  auto &world = *level.world;
  auto player = world.get_the<Player>();
  auto& the_belt = world.get_the<ritual::Belt>();

  if(!the_belt.has(attack_id)) return;

  auto& ritual = the_belt.get(attack_id);
  const auto& player_position = world.get<Position>(player.entity);
  auto& player_combat = world.get<Combat>(player.entity);

  // this is guaranteed to not return the given position
  auto [found, nearby] = collider.neighbors(player_position.location);
  combat::BattleEngine battle;

  if(found) {
    for(auto entity : nearby) {
      if(world.has<ai::EntityAI>(entity)) {
        auto& enemy_ai = world.get<ai::EntityAI>(entity);
        auto& enemy_combat = world.get<Combat>(entity);
        battle.add_enemy({entity, enemy_ai, enemy_combat});
      }
    }

    battle.set_all("enemy_found", true);
    battle.set_all("in_combat", true);
    battle.plan();
  }

  while(auto act = battle.next()) {
    auto [enemy, enemy_action] = *act;

    Events::Combat result {
      player_combat.attack(enemy.combat), 0
    };

    if(result.player_did > 0) {
      using enum ritual::Element;

      if(ritual.element == FIRE || ritual.element == LIGHTNING) {
        auto effect = shaders::get(
            ritual.element == FIRE ? "flame" : "lightning");
        world.set<SpriteEffect>(enemy.entity, {100, effect});
      }
    }

    if(enemy_action == combat::BattleAction::ATTACK) {
      result.enemy_did = enemy.combat.attack(player_combat);
      animate_entity(world, enemy.entity);
    }

    world.send<Events::GUI>(Events::GUI::COMBAT, enemy.entity, result);
  }
}


void System::collision(GameLevel &level) {
  auto &collider = *level.collision;
  auto &world = *level.world;
  auto player = world.get_the<Player>();

  const auto& player_position = world.get<Position>(player.entity);

  // this is guaranteed to not return the given position
  auto [found, nearby] = collider.neighbors(player_position.location);
  int combat_count = 0;

  // AI: I think also this would a possible place to run AI decisions
  for(auto entity : nearby) {
    if(world.has<Combat>(entity)) {
      auto combat = world.get<Combat>(entity);
      if(!combat.dead) {
        combat_count++;
        world.send<Events::GUI>(Events::GUI::COMBAT_START, entity, entity);
      }
    } else {
      dbc::log(fmt::format("UNKNOWN COLLISION TYPE {}", entity));
    }
  }

  if(combat_count == 0) {
    // BUG: this is probably how we get stuck in combat
    world.send<Events::GUI>(Events::GUI::NO_NEIGHBORS, player.entity, player.entity);
  }
}

/*
 * This isn't for destroying something, but just removing it
 * from the world for say, putting into a container or inventory.
 */
void System::remove_from_world(GameLevel &level, Entity entity) {
  auto& item_pos = level.world->get<Position>(entity);
  level.collision->remove(item_pos.location, entity);
  level.world->remove<Tile>(entity);
  // if you don't do this you get the bug that you can pickup
  // an item and it'll also be in your inventory
  level.world->remove<Position>(entity);
}

void System::pickup(GameLevel &level, Entity entity) {
  auto &world = *level.world;
  auto player = world.get_the<Player>();

  if(world.has<InventoryItem>(entity)) {
    // NOTE: this might need to be a separate system so that people can leave stuff alone
    auto item = world.get<InventoryItem>(entity);
    remove_from_world(level, entity);

    if(world.has<ritual::JunkPile>(entity)) {
      auto& pile = world.get<ritual::JunkPile>(entity);
      auto& blanket = world.get_the<ritual::Blanket>();

      for(auto& junk : pile.contents) {
        blanket.add(junk);
      }
      // NOTE: have the gui notify them that some new stuff is in the blanket
    } else {
      // NOTE: chests are different from say a torch, maybe 2 events or the
      // GUI figures out which it is, then when you click either pick it up
      // and move it or show the loot container UI
      world.send<Events::GUI>(Events::GUI::LOOT_ITEM, entity, item);
    }
  } else if(world.has<Device>(entity)) {
    System::device(world, player.entity, entity);
  }
}


void System::device(World &world, Entity actor, Entity item) {
  auto& device = world.get<Device>(item);
  dbc::log(fmt::format("entity {} INTERACTED WITH DEVICE {}", actor, item));

  for(auto event : device.events) {
    if(event == "STAIRS_DOWN") {
      world.send<Events::GUI>(Events::GUI::STAIRS_DOWN, actor, device);
    } else if(event == "STAIRS_UP") {
      world.send<Events::GUI>(Events::GUI::STAIRS_UP, actor, device);
    } else if(event == "TRAP") {
      world.send<Events::GUI>(Events::GUI::TRAP, actor, device);
    } else if(event == "LOOT_CONTAINER") {
      world.send<Events::GUI>(Events::GUI::LOOT_CONTAINER, actor, device);
    } else {
      dbc::log(fmt::format(
            "INVALID EVENT {} for device {}",
            event, (std::string)device.config["name"]));
    }
  }
}

void System::plan_motion(World& world, Position move_to) {
  auto& player = world.get_the<Player>();
  auto& player_position = world.get<Position>(player.entity);

  player_position.aiming_at = move_to.aiming_at;

  auto& motion = world.get<Motion>(player.entity);
  motion.dx = move_to.location.x - player_position.location.x;
  motion.dy = move_to.location.y - player_position.location.y;
}


void System::player_status(GameLevel &level) {
  auto& combat = level.world->get<Combat>(level.player);
  float percent = float(combat.hp) / float(combat.max_hp);

  if(percent > 0.8) {
    sound::play("hp_status_80");
  } else if(percent > 0.6) {
    sound::play("hp_status_60");
  } else if(percent > 0.3) {
    sound::play("hp_status_30");
  } else if(percent > 0.1) {
    sound::play("hp_status_10");
  } else {
    sound::play("hp_status_00");
  }
}

std::shared_ptr<sf::Shader> System::sprite_effect(GameLevel &level, Entity entity) {
  if(level.world->has<SpriteEffect>(entity)) {
    auto& se = level.world->get<SpriteEffect>(entity);

    if(se.frames > 0) {
      se.frames--;
      return se.effect;
    } else {
      level.world->remove<SpriteEffect>(entity);
      return nullptr;
    }
  } else {
    return nullptr;
  }
}

Entity System::spawn_item(World& world, const std::string& name) {
  auto& config = world.get_the<GameConfig>().items;
  auto& item_config = config[name];
  auto item_id = world.entity();
  world.set<InventoryItem>(item_id, {1, item_config});
  components::configure_entity(world, item_id, item_config["components"]);

  return item_id;
}

bool System::drop_item(GameLevel& level, Entity item) {
  auto& world = *level.world;
  auto& map = *level.map;
  auto& collision = *level.collision;

  auto player = world.get_the<Player>();
  auto player_pos = world.get<Position>(player.entity);
  auto& player_inv = world.get<inventory::Model>(player.entity);

  // doesn't compass already avoid walls?
  for(matrix::box it{map.walls(), player_pos.location.x, player_pos.location.y, 1}; it.next();)
  {
    Position pos{it.x, it.y};

    if(map.can_move(pos.location) && !collision.occupied(pos.location)) {
      world.set<Position>(item, pos);
      collision.insert(pos.location, item, false);
      // BUG: really there should be another system that handles loot->inv moves
      if(player_inv.has(item)) player_inv.remove(item);
      level.world->send<Events::GUI>(Events::GUI::ENTITY_SPAWN, item, {});
      level.world->not_constant(item);
      return true;
    }
  }

  return false;
}

// NOTE: I tink pickup and this need to be different
bool System::place_in_container(World& world, Entity cont_id, const std::string& name, Entity world_entity) {
  auto& container = world.get<inventory::Model>(cont_id);

  if(container.has(world_entity)) {
    fmt::println("container {} already has entity {}, skip", cont_id, world_entity);
    // NOTE: I think this would be a move?!
    return false;
  } else if(container.has(name)) {
    // this is an already occupied slot
    fmt::println("container {} already has SLOT {}, skip", cont_id, name);
    return false;
  } else {
    // this should only apply to the player's inventory
    fmt::println("adding {} entity to loot with name {}", world_entity, name);
    container.add(name, world_entity);
    return true;
  }
}

void System::remove_from_container(World& world, Entity cont_id, const std::string& slot_id) {
  auto& container = world.get<inventory::Model>(cont_id);
  auto entity = container.get(slot_id);
  container.remove(entity);
}

Position& System::player_position(GameLevel& level) {
  auto& player = level.world->get_the<components::Player>();
  return level.world->get<components::Position>(player.entity);
}

void System::inventory_swap(GameLevel &level, Entity container_id, const std::string& a_name, const std::string &b_name) {
  dbc::check(a_name != b_name, "Attempt to inventory swap the same slot, you should check this and avoid calling me.");

  auto& inventory = level.world->get<inventory::Model>(container_id);

  auto a_ent = inventory.get(a_name);
  auto b_ent = inventory.get(b_name);
  inventory.swap(a_ent, b_ent);
}

bool System::inventory_occupied(GameLevel& level, Entity container_id, const std::string& name) {
  auto& inventory = level.world->get<inventory::Model>(container_id);
  return inventory.has(name);
}


void System::draw_map(GameLevel& level, Matrix& grid, EntityGrid& entity_map) {
  World &world = *level.world;
  Map &map = *level.map;
  Matrix &fow = level.lights->$fow;
  size_t view_x = matrix::width(grid) - 1;
  size_t view_y = matrix::height(grid) - 1;

  entity_map.clear();

  auto player_pos = world.get<Position>(level.player).location;
  Point cam_orig = map.center_camera(player_pos, view_x, view_y);
  auto &tiles = map.tiles();
  auto &tile_set = textures::get_map_tile_set();

  /* I'm doing double tid->wchar_t conversion here, maybe just
   * render the tids into the grid then let someone else do this. */

  // first fill it with the map cells
  for(shiterator::each_cell_t it{grid}; it.next();) {
    size_t tile_y = size_t(it.y) + cam_orig.y;
    size_t tile_x = size_t(it.x) + cam_orig.x;

    if(matrix::inbounds(tiles, tile_x, tile_y) && fow[tile_y][tile_x]) {
      size_t tid = tiles[tile_y][tile_x];
      grid[it.y][it.x] = tile_set[tid];
    } else {
      grid[it.y][it.x] = L' ';
    }
  }

  // then get the enemy/item/device tiles and fill those in
  world.query<Position, Tile>([&](auto, auto &pos, auto &entity_glyph) {
    // BUG: don't I have a within bounds macro somewhere?
    if(pos.location.x >= cam_orig.x
        && pos.location.x <= cam_orig.x + view_x
        && pos.location.y >= cam_orig.y
        && pos.location.y <= cam_orig.y + view_y)
    {
        if(fow[pos.location.y][pos.location.x]) {
          Point view_pos = map.map_to_camera(pos.location, cam_orig);
          entity_map.insert_or_assign(view_pos, entity_glyph.display);
        }
    }
  });
}

void System::render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display) {
  sf::Vector2i tile_sprite_dim{MAP_TILE_DIM,MAP_TILE_DIM};
  unsigned int width = matrix::width(tiles);
  unsigned int height = matrix::height(tiles);
  sf::Vector2u dim{width * tile_sprite_dim.x, height * tile_sprite_dim.y};
  auto render_size = render.getSize();

  if(render_size.x != width || render_size.y != height) {
    bool worked = render.resize(dim);
    dbc::check(worked, "Failed to resize map render target.");
  }

  render.clear({0,0,0,255});

  for(matrix::each_row it{tiles}; it.next();) {
    wchar_t display = tiles[it.y][it.x];
    if(display == L' ') continue; // skip for now
    auto& sprite = textures::get_map_sprite(display);
    sprite.setPosition({float(it.x * tile_sprite_dim.x), float(it.y * tile_sprite_dim.y)});
    render.draw(sprite);
  }

  for(auto [point, display] : entity_map) {
    auto& sprite = textures::get_map_sprite(display);

    if(display == player_display) {
      sf::Vector2f center{float(tile_sprite_dim.x / 2), float(tile_sprite_dim.y / 2)};
      float degrees = (((compass_dir * 45) + PLAYER_SPRITE_DIR_CORRECTION) % 360);

      sprite.setOrigin(center);
      sprite.setRotation(sf::degrees(degrees));
      sprite.setPosition({float(point.x * tile_sprite_dim.x) + center.x, float(point.y * tile_sprite_dim.y) + center.y});
    } else {
      sprite.setPosition({float(point.x * tile_sprite_dim.x), float(point.y * tile_sprite_dim.y)});
    }


    render.draw(sprite);
  }

  render.display();
}
