from django.shortcuts import render, get_object_or_404
from django.http import Http404
import random
import math
from .services.presets import get_environment_preset
import networkx as nx
from .services.layouts import (
    rectangular_layout,
    circular_layout,
    diagonal_layout,
    triangular_layout,
)
from .services.random_city import generate_random_city_data
from .services.com_con_city import generate_com_con_city_data
from .services.osm_city import generate_osm_city_data



def city_digital_twin(request, city_id, innovation_pct=None, technology_pct=None, science_pct=None):
    try:
        lat = float(request.GET.get('lat', 0))
        long = float(request.GET.get('long', 0))
        scale = float(request.GET.get('scale', 1.0))  # default to 1.0 (normal scale)

        if city_id == "osm_city":
            city_data = generate_osm_city_data(lat, long,scale=scale)
        elif city_id == "com_con":
            city_data = generate_com_con_city_data(lat, long)
        elif city_id == "random_city":
            city_data = generate_random_city_data()
        elif city_id == "dream":
            innovation_pct = innovation_pct or request.GET.get('innovation', 0)
            technology_pct = technology_pct or request.GET.get('technology', 0)
            science_pct = science_pct or request.GET.get('science', 0)

            innovation_pct = int(innovation_pct)
            technology_pct = int(technology_pct)
            science_pct = int(science_pct)

            city_data = generate_random_city_data(innovation_pct, technology_pct, science_pct)
        else:
            city_data = get_city_data(city_id)

        if not city_data:
            city_data = get_example_data()

        preset = get_environment_preset(lat, long)

        context = {
            'city_data': city_data,
            'environment_preset': preset,
            'lat': lat,
            'long': long,
        }
        return render(request, 'pxy_city_digital_twins/city_digital_twin.html', context)
    except (ValueError, TypeError):
        raise Http404("Invalid data provided.")


def get_city_data(city_id):
    # Implement fetching logic here
    # This is a mock function to demonstrate fetching logic
    if str(city_id) == "1" or str(city_id) == "123e4567-e89b-12d3-a456-426614174000":
        return {
            # Real data retrieval logic goes here
        }
    return None

def get_example_data():
    return {
        'buildings': [
            {
                'id': 1,
                'status': 'Occupied',
                'position_x': 0,
                'height': 10,
                'position_z': 0,
                'width': 5,
                'depth': 5,
                'color': '#8a2be2',
                'file': '',  # No file for a simple box representation
            },
            {
                'id': 2,
                'status': 'Vacant',
                'position_x': 10,
                'height': 15,
                'position_z': 10,
                'width': 7,
                'depth': 7,
                'color': '#5f9ea0',
                'file': '',  # No file for a simple box representation
            }
        ],
        'lamps': [
            {
                'id': 1,
                'status': 'Functional',
                'position_x': 3,
                'position_z': 3,
                'height': 4,
                'color': '#ffff00',
            },
            {
                'id': 2,
                'status': 'Broken',
                'position_x': 8,
                'position_z': 8,
                'height': 4,
                'color': '#ff0000',
            }
        ],
        'trees': [
            {
                'id': 1,
                'status': 'Healthy',
                'position_x': 5,
                'position_z': 5,
                'height': 6,
                'radius_bottom': 0.2,
                'radius_top': 1,
                'color_trunk': '#8b4513',
                'color_leaves': '#228b22',
            },
            {
                'id': 2,
                'status': 'Diseased',
                'position_x': 15,
                'position_z': 15,
                'height': 6,
                'radius_bottom': 0.2,
                'radius_top': 1,
                'color_trunk': '#a0522d',
                'color_leaves': '#6b8e23',
            }
        ]
    }
