import os
from experiment_runs import load_pickled_class
from evolution_gradient import GRADIENT_BLOCKS, GradientExecution, setup_block_color_dict
from evolution_layout import LayoutConfig, LayoutExecution
from evolution_roof import BuildingExecution
from utils_block_color import BlockColors
from utils_coordinates import Coordinates
from utils_server_client import ServerClient
from utils_workspace_manger import Margin, WorkspaceManager

SHOWCASE_DATA_FOLDER = os.path.join("py_scripts","showcase_data")
DEFAULT_COOR = Coordinates( 0, 0, 0)

if __name__ == "__main__":
    
    # Setup
    
    sc = ServerClient()
    dim = Coordinates(10,1,1)
    shape = Coordinates(16,100,16)
    spc = Coordinates(14,16,14)
    cp = True
    mrg = Margin(1,1,1,1)
    
    layouts = load_pickled_class(os.path.join(SHOWCASE_DATA_FOLDER, "layouts"))
    layouts_dim = Coordinates(len(layouts),1,1)
    buildings = load_pickled_class(os.path.join(SHOWCASE_DATA_FOLDER, "buildings"))
    buildings_dim = Coordinates(len(buildings),1,1)
    buildings, gradients = load_pickled_class(os.path.join(SHOWCASE_DATA_FOLDER, "gradients"))
    gradients_dim = Coordinates(len(gradients),1,1)
    
    full_shape = spc + shape + mrg.get_lower_margin() + mrg.get_upper_margin()
    offset = Coordinates(0, 0, full_shape.z)
    space_shape = Coordinates(
        full_shape.x * max(layouts_dim.x, buildings_dim.x, gradients_dim.x),
        full_shape.y + 2,
        full_shape.z * 3
    )
    sc.create_cube_from_loc(DEFAULT_COOR-Coordinates.cardinal_down()*2, space_shape)
    
    # Layouts
    
    layouts_wm = WorkspaceManager(sc)
    layouts_wm.create_grid_workspace(
        DEFAULT_COOR,
        layouts_dim, shape, spc,
        create_platform = cp, margin = mrg
    )
    
    layout_config = LayoutConfig(layouts_wm.get_shape())
    layout_execution = LayoutExecution(layouts_wm, layout_config)
    layout_execution.set_debug(False)
    layout_execution.simulate(layouts ,fill=False)
    
    # Buildings
    
    buildings_wm = WorkspaceManager(sc)
    buildings_wm.create_grid_workspace(
        DEFAULT_COOR + offset,
        buildings_dim, shape, spc,
        create_platform = cp, margin = mrg
    )
    
    building_execution = BuildingExecution(buildings_wm)
    building_execution.set_debug(False)
    building_execution.simulate(buildings)
    
    # Gradients
    
    gradients_wm = WorkspaceManager(sc)
    gradients_wm.create_grid_workspace(
        DEFAULT_COOR + offset*2,
        gradients_dim, shape, spc,
        create_platform = cp, margin = mrg
    )
    
    block_color = BlockColors.load_from_default_file()
    block_color.set_block_pallete(GRADIENT_BLOCKS)
    block_colors = setup_block_color_dict(block_color)
    gradient_execution = GradientExecution(gradients_wm, block_colors, buildings[0])
    gradient_execution.set_debug(False)
    gradient_execution.showcase( buildings, gradients)