import typer
from enum import Enum

from acemist.print_utils.print_device import print_device_list,print_device_config,print_dhcp_scopes,print_vrrp_gropus,print_access
class ShowConfig(str, Enum):
    ospf="ospf"
    dhcp_scopes="dhcp"
    vrrp="vrrp"
    access="access"
app = typer.Typer(help="Manage devices.")


@app.command("list_devices")
def list_devices(
    ctx: typer.Context,
    ):
    """List devices in site"""
    mist = ctx.obj.mist
    
    device_list = mist.site_list_devices()
    
    devices= []
    for device in device_list:
        device_name_id= {
            "name": device['name'],
            "id": device['id']
        }
        devices.append(device_name_id)
    print_device_list(devices)

@app.command("show_config")
def show_config(
    ctx: typer.Context,
    device_id: str = typer.Argument(..., help="Device ID"),
    section: ShowConfig =None
    ):
    """Show device config"""
    mist = ctx.obj.mist
    

    device = mist.site_devices_get(device_id)

    if section =="ospf":

        """Show device config"""
        
        
        try:
            ospf_data = {
            "device_name": device["name"],
            "networks": {}
            }

            for network, net_data in device["ospf_areas"]["0"]["networks"].items():
                ospf_data["networks"][network] = {
                    "interface_type": net_data["interface_type"] if not net_data["passive"] else "-",
                    "passive": net_data["passive"],
                }
            print_device_config(ospf_data)
        except:
            "No OSPF configured"
    if section == "dhcp":
        """list DHCP scopes configured on device"""
        try:
            scope_data = {
            "device_name": device["name"],
            "networks": {}
            }
            
            for network, net_data in device["dhcpd_config"].items():
                
                if "enabled" not in network:
                    scope_data["networks"][network] = {
                        "Start IP": net_data["ip_start"] ,
                        "End IP": net_data["ip_end"] ,
                        "Gateway": net_data["gateway"],
                        "DNS":net_data["dns_servers"]
                    }
                
            print_dhcp_scopes(scope_data)
        except:
            print("no dhcp scopes found")
    if section == "vrrp":
        try:
            vrrp_data = {
            "device_name": device["name"],
            "vrrp_groups": []
            }
            
            for group_id, group_data in device["vrrp_groups"].items():
                for network_name, net_data in group_data["networks"].items():
                    vrrp_data["vrrp_groups"].append({
                        "id": group_id,
                        "network": network_name,
                        "ip": net_data["ip"]
                    })
                     
            print_vrrp_gropus(vrrp_data)
        except:
            print("No vrrp configuration found")
    if section == "access":
        try:
            access_data = {
            "device_name": device["name"],
            "port_config": []
            }
            
            for port_id, port_data in device["port_config"].items():
                
                access_data["port_config"].append({
                    "port":port_id,
                    "usage":port_data["usage"]
                })

            print_access(access_data)
        except:
            print("No accessport configuration found")





