from fastapi import APIRouter, Request
from typing import Any, List, Dict, Union
from app.services.tjnetwork import (
    Any,
    ChangeSet,
    add_tank,
    delete_tank,
    get_all_tanks,
    get_tank,
    get_tank_schema,
    set_tank,
)

router = APIRouter()

@router.get("/gettankschema")
async def fast_get_tank_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_tank_schema(network)

@router.post("/addtank/", response_model=None)
async def fastapi_add_tank(
    network: str,
    tank: str,
    x: float,
    y: float,
    elevation: float,
    init_level: float = 0,
    min_level: float = 0,
    max_level: float = 0,
    diameter: float = 0,
    min_vol: float = 0,
) -> ChangeSet:
    ps = {
        "id": tank,
        "x": x,
        "y": y,
        "elevation": elevation,
        "init_level": init_level,
        "min_level": min_level,
        "max_level": max_level,
        "diameter": diameter,
        "min_vol": min_vol,
    }
    return add_tank(network, ChangeSet(ps))

@router.post("/deletetank/", response_model=None)
async def fastapi_delete_tank(network: str, tank: str) -> ChangeSet:
    ps = {"id": tank}
    return delete_tank(network, ChangeSet(ps))

@router.get("/gettankelevation/")
async def fastapi_get_tank_elevation(network: str, tank: str) -> float | None:
    ps = get_tank(network, tank)
    return ps["elevation"]

@router.get("/gettankinitlevel/")
async def fastapi_get_tank_init_level(network: str, tank: str) -> float | None:
    ps = get_tank(network, tank)
    return ps["init_level"]

@router.get("/gettankminlevel/")
async def fastapi_get_tank_min_level(network: str, tank: str) -> float | None:
    ps = get_tank(network, tank)
    return ps["min_level"]

@router.get("/gettankmaxlevel/")
async def fastapi_get_tank_max_level(network: str, tank: str) -> float | None:
    ps = get_tank(network, tank)
    return ps["max_level"]

@router.get("/gettankdiameter/")
async def fastapi_get_tank_diameter(network: str, tank: str) -> float | None:
    ps = get_tank(network, tank)
    return ps["diameter"]

@router.get("/gettankminvol/")
async def fastapi_get_tank_min_vol(network: str, tank: str) -> float | None:
    ps = get_tank(network, tank)
    return ps["min_vol"]

@router.get("/gettankvolcurve/")
async def fastapi_get_tank_vol_curve(network: str, tank: str) -> str | None:
    ps = get_tank(network, tank)
    return ps["vol_curve"]

@router.get("/gettankoverflow/")
async def fastapi_get_tank_overflow(network: str, tank: str) -> str | None:
    ps = get_tank(network, tank)
    return ps["overflow"]

@router.get("/gettankx/")
async def fastapi_get_tank_x(network: str, tank: str) -> float:
    ps = get_tank(network, tank)
    return ps["x"]

@router.get("/gettanky/")
async def fastapi_get_tank_y(network: str, tank: str) -> float:
    ps = get_tank(network, tank)
    return ps["y"]

@router.get("/gettankcoord/")
async def fastapi_get_tank_coord(network: str, tank: str) -> dict[str, float]:
    ps = get_tank(network, tank)
    coord = {"x": ps["x"], "y": ps["y"]}
    return coord

@router.post("/settankelevation/", response_model=None)
async def fastapi_set_tank_elevation(
    network: str, tank: str, elevation: float
) -> ChangeSet:
    ps = {"id": tank, "elevation": elevation}
    return set_tank(network, ChangeSet(ps))

@router.post("/settankinitlevel/", response_model=None)
async def fastapi_set_tank_init_level(
    network: str, tank: str, init_level: float
) -> ChangeSet:
    ps = {"id": tank, "init_level": init_level}
    return set_tank(network, ChangeSet(ps))

@router.post("/settankminlevel/", response_model=None)
async def fastapi_set_tank_min_level(
    network: str, tank: str, min_level: float
) -> ChangeSet:
    ps = {"id": tank, "min_level": min_level}
    return set_tank(network, ChangeSet(ps))

@router.post("/settankmaxlevel/", response_model=None)
async def fastapi_set_tank_max_level(
    network: str, tank: str, max_level: float
) -> ChangeSet:
    ps = {"id": tank, "max_level": max_level}
    return set_tank(network, ChangeSet(ps))

@router.post("settankdiameter//", response_model=None)
async def fastapi_set_tank_diameter(
    network: str, tank: str, diameter: float
) -> ChangeSet:
    ps = {"id": tank, "diameter": diameter}
    return set_tank(network, ChangeSet(ps))

@router.post("/settankminvol/", response_model=None)
async def fastapi_set_tank_min_vol(
    network: str, tank: str, min_vol: float
) -> ChangeSet:
    ps = {"id": tank, "min_vol": min_vol}
    return set_tank(network, ChangeSet(ps))

@router.post("/settankvolcurve/", response_model=None)
async def fastapi_set_tank_vol_curve(
    network: str, tank: str, vol_curve: str
) -> ChangeSet:
    ps = {"id": tank, "vol_curve": vol_curve}
    return set_tank(network, ChangeSet(ps))

@router.post("/settankoverflow/", response_model=None)
async def fastapi_set_tank_overflow(
    network: str, tank: str, overflow: str
) -> ChangeSet:
    ps = {"id": tank, "overflow": overflow}
    return set_tank(network, ChangeSet(ps))

@router.post("/settankx/", response_model=None)
async def fastapi_set_tank_x(network: str, tank: str, x: float) -> ChangeSet:
    ps = {"id": tank, "x": x}
    return set_tank(network, ChangeSet(ps))

@router.post("/settanky/", response_model=None)
async def fastapi_set_tank_y(network: str, tank: str, y: float) -> ChangeSet:
    ps = {"id": tank, "y": y}
    return set_tank(network, ChangeSet(ps))

@router.post("/settankcoord/", response_model=None)
async def fastapi_set_tank_coord(
    network: str, tank: str, x: float, y: float
) -> ChangeSet:
    ps = {"id": tank, "x": x, "y": y}
    return set_tank(network, ChangeSet(ps))

@router.get("/gettankproperties/")
async def fastapi_get_tank_properties(network: str, tank: str) -> dict[str, Any]:
    return get_tank(network, tank)

@router.get("/getalltankproperties/")
async def fastapi_get_all_tank_properties(network: str) -> list[dict[str, Any]]:
    # 缓存查询结果提高性能
    # global redis_client
    results = get_all_tanks(network)
    return results

@router.post("/settankproperties/", response_model=None)
async def fastapi_set_tank_properties(
    network: str, tank: str, req: Request
) -> ChangeSet:
    props = await req.json()
    ps = {"id": tank} | props
    return set_tank(network, ChangeSet(ps))
