import random
from .layouts import rectangular_layout, circular_layout, diagonal_layout, triangular_layout


def generate_random_city_data(innovation_pct=100, technology_pct=100, science_pct=100, max_position=100, radius=50):
    num_buildings = random.randint(5, 35)
    num_lamps = random.randint(5, 100)
    num_trees = random.randint(5, 55)

    # Buildings layout distribution
    num_rectangular_buildings = int(num_buildings * innovation_pct / 100)
    num_circular_buildings = (num_buildings - num_rectangular_buildings) // 2
    num_triangular_buildings = num_buildings - num_rectangular_buildings - num_circular_buildings

    building_positions = rectangular_layout(num_rectangular_buildings, max_position) + \
                         circular_layout(num_circular_buildings, radius) + \
                         triangular_layout(num_triangular_buildings)

    # Lamps layout distribution
    num_triangular_lamps = int(num_lamps * technology_pct / 100)
    num_circular_lamps = (num_lamps - num_triangular_lamps) // 2
    num_diagonal_lamps = num_lamps - num_triangular_lamps - num_circular_lamps

    lamp_positions = triangular_layout(num_triangular_lamps) + \
                     circular_layout(num_circular_lamps, radius) + \
                     diagonal_layout(num_diagonal_lamps, max_position)

    # Trees layout distribution
    num_circular_trees = int(num_trees * science_pct / 100)
    num_triangular_trees = (num_trees - num_circular_trees) // 2
    num_diagonal_trees = num_trees - num_circular_trees - num_triangular_trees

    tree_positions = circular_layout(num_circular_trees, radius) + \
                     triangular_layout(num_triangular_trees) + \
                     diagonal_layout(num_diagonal_trees, max_position)

    buildings = [
        {
            'id': i + 1,
            'status': random.choice(['Occupied', 'Vacant', 'Under Construction']),
            'position_x': pos['position_x'],
            'position_z': pos['position_z'],
            'height': random.randint(10, 50),
            'width': random.randint(5, 20),
            'depth': random.randint(5, 20),
            'color': random.choice(['#8a2be2', '#5f9ea0', '#ff6347', '#4682b4']),
            'file': ''
        } for i, pos in enumerate(building_positions)
    ]

    lamps = [
        {
            'id': i + 1,
            'status': random.choice(['Functional', 'Non-functional']),
            'position_x': pos['position_x'],
            'position_z': pos['position_z'],
            'height': random.randint(3, 10),
            'color': random.choice(['#ffff00', '#ff0000', '#00ff00']),
        } for i, pos in enumerate(lamp_positions)
    ]

    trees = [
        {
            'id': i + 1,
            'status': random.choice(['Healthy', 'Diseased', 'Wilting']),
            'position_x': pos['position_x'],
            'position_z': pos['position_z'],
            'height': random.randint(5, 30),
            'radius_bottom': random.uniform(0.1, 0.5),
            'radius_top': random.uniform(0.5, 2.0),
            'color_trunk': '#8b4513',
            'color_leaves': random.choice(['#228b22', '#90ee90', '#8b4513']),
        } for i, pos in enumerate(tree_positions)
    ]

    return {
        'buildings': buildings,
        'lamps': lamps,
        'trees': trees,
    }

