#!/usr/bin/env python

"""

Test story 7
A rogue ship attacks one of the players ships

"""

import os
import sys
import random
import sqlalchemy
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
import legendary_waffle
from legendary_waffle import models
from legendary_waffle import fixture
from legendary_waffle import actions

# Database setup
db = legendary_waffle.inmem_db()

# Populate the database with our fixtures
legendary_waffle.fixture.preload_database(db, file_list_prefix="../../fixtures/{}")

# Create a universe
universe_config = {
    "name": 'poopiverse',
    "map_size": 1000,
    "max_planets": 5,
    "max_players": 5
}
universe = legendary_waffle.models.model_create(db, legendary_waffle.models.Universe, **universe_config)
print "Universe: {}".format(universe)

# Set the initial buildings for the universe
initial_buildings = [
    "Control Center",
    "Fortified Storage Unit",
    "Spaceport"
]
for i in initial_buildings:
    building_type = legendary_waffle.models.model_read(db, legendary_waffle.models.BuildingType, name=i)
    universe.starter_buildings.append(building_type)
db.commit()

# Initialize the universe planets
universe.create_initial_planets(db)

# Create a player
player = legendary_waffle.models.model_create(db, legendary_waffle.models.User, name='sk4ly')
print "Players: {}".format(player)

# Enrole the player in the universe
legendary_waffle.enroll_player(db, universe.pkid, player.pkid)

# Get the players planet
player_planets = legendary_waffle.get_controlled_planets(db, player.pkid)
print "Planets owned by '{}' ({})".format(player.name, len(player_planets))
for i in player_planets:
    print " - {}".format(i)
planet = player_planets[0]

# Add resources to the storage unit
planet.add_resources(db, 1500)

# Add a ship for the player
ship_type = legendary_waffle.models.model_read(db, legendary_waffle.models.ShipType, pkid=1) # This adds a colony ship
planet.add_ship(db, ship_type)

# Get a list of buildings on the planet
buildings = planet.get_buildings(db)
print "Buildings on planet '{}' ({}):".format(planet.name, len(buildings))
for i in buildings:
    print " - {}".format(i)

# Get a list of ships at the planet
ships = planet.get_ships(db)
print "Ships at planet '{}' ({}):".format(planet.name, len(ships))
for i in ships:
    print " - {}".format(i)


# Create the rogue ship
rogue_ship_type = legendary_waffle.models.model_read(db, legendary_waffle.models.ShipType, pkid=2) # This adds an attack ship
rogue_ship = rogue_ship_type.get_instance(universe_pkid=universe.pkid, planet_pkid=planet.pkid)
db.add(rogue_ship)
db.commit()

print "\nA rogue ship has appeared at '{}'!\n".format(planet.name)

# Get a list of ships at the planet
ships = planet.get_ships(db)
print "Ships at planet '{}' ({}):".format(planet.name, len(ships))
for i in ships:
    print " - {}".format(i)

# Get a list of available actions on the rogue ship
ship_actions = rogue_ship.get_actions(db)
print "\nActions available on the rogue ship '{}':".format(rogue_ship.name)
for i in ship_actions:
    print " - {}".format(i.display_name)

target_ship = None
while target_ship is None or target_ship == rogue_ship:
    target_ship = random.choice(ships)

while target_ship.current_condition > 0:
    print "\nThe rogue ship prepares to fire!"
    action = rogue_ship.initiate_action(db, "attack_ship")
    action.set_argument('target_ship', target_ship.pkid)
    result = action.act()

    if result[0]:
        print " - The ship '{}' was hit for {} damage. (condition: {}/{})".format(target_ship.name, result[1], target_ship.current_condition, target_ship.default_condition)
    else:
        print " - The ship '{}' was shot at, but was not hit.".format(target_ship.name)


# Get a list of ships at the planet
ships = planet.get_ships(db)
print "Ships at planet '{}' ({}):".format(planet.name, len(ships))
for i in ships:
    print " - {}".format(i)
