#!/usr/bin/env python

"""

This pretty much just tests creating a user, a universe, a planet, a building type name, a building
type, and a building.

"""

import os
import sys
import sqlalchemy
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import legendary_waffle

# Database setup
db_engine = sqlalchemy.create_engine("sqlite://")
legendary_waffle.models.MODELBASE.metadata.create_all(db_engine)
legendary_waffle.models.MODELBASE.metadata.bind = db_engine
db_session = sqlalchemy.orm.sessionmaker(bind=db_engine)
db = db_session()

# Create the user
legendary_waffle.model_create(db, legendary_waffle.models.User, name='sk4ly')
print "Users: {}".format(legendary_waffle.model_read(db, legendary_waffle.models.User))

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

# Create the planet
planet_config = {
    "universe": 1, # The pkid of the universe 'poopiverse'
    "coordinate_x": 1,
    "coordinate_y": 1,
    "name": 'bloth',
    "habitable": True,
    "player_control": 1, # The pkid of user 'sk4ly'
    "default_condition": 1000,
    "default_resources": 1000,
    "current_condition": 1000,
    "current_resources": 1000
}
legendary_waffle.model_create(db, legendary_waffle.models.Planet, **planet_config)
print "Planet: {}".format(legendary_waffle.model_read(db, legendary_waffle.models.Planet))

# Create building type name
legendary_waffle.model_create(db, legendary_waffle.models.BuildingTypeName, name="Control Center")
print "Building Type Name: {}".format(legendary_waffle.model_read(db, legendary_waffle.models.BuildingTypeName))

# Create building type
building_type_config = {
    "typename": 1, # The pkid of the building type name 'Control Center'
    "description": "This is the control center",
    "default_condition": 100,
    "default_firepower": 0,
    "default_storage": 100,
    "rhr_passive": 0,
    "rhr_active": 0,
    "rhr_destructive": 0,
    "build_resource_reqs": 500,
}
legendary_waffle.model_create(db, legendary_waffle.models.BuildingType, **building_type_config)
print "Building Type: {}".format(legendary_waffle.model_read(db, legendary_waffle.models.BuildingType))

# Now create our new building
building_config = {
    "building_type": 1, # The pkid of the building type with the name 'Control Center'
    "universe": 1, # The pkid of the universe 'poopiverse'
    "planet": 1, # The pkid of the planet 'bloth'
    "player_control": 1, # The pkid of the user 'sk4ly'
}
legendary_waffle.model_create(db, legendary_waffle.models.Building, **building_config)
print "Building: {}".format(legendary_waffle.model_read(db, legendary_waffle.models.Building))
