from pathlib import Path
from dataclasses import dataclass, field
from typing import List, Dict, Any, Type
from codegen.constants import WAPP_SRC_ROOT
from codegen.utils import load_func_body_from_file, convert_to_relative
from codegen.datatypes import (
    CDataType,
    CMacro,
    CStruct,
    CFunc,
    CHeader,
    CSource,
    CArg,
    CType,
    CPointer,
    CPointerType,
    CQualifier,
    CInclude,
    SerialisableDataclass,
    get_datatype_string,
)


@dataclass
class DblListData(SerialisableDataclass):
    node_typename: str
    list_typename: str
    hdr_decl_types: List[CStruct] = field(default_factory=list)
    src_decl_types: List[CStruct] = field(default_factory=list)

    @classmethod
    def from_dict(cls: Type["DblListData"], d: Dict[str, Any]) -> "DblListData":
        data = DblListData(**d)
        data.hdr_decl_types = [CStruct.from_dict(v) for v in data.hdr_decl_types if isinstance(v, dict)]
        data.src_decl_types = [CStruct.from_dict(v) for v in data.src_decl_types if isinstance(v, dict)]
        return data


def make_dbl_list(user_datatypes: Dict[CDataType, DblListData] = {}):
    def __format_func_body(
        filename: Path,
        type_string: str,
        type_string_upper: str,
        type_string_lower: str,
        node_typename: str,
        list_typename: str
    ):
        return load_func_body_from_file(filename).format(
            T=type_string,
            NodeType=node_typename,
            ListType=list_typename,
            Tupper=type_string_upper,
            Tlower=type_string_lower,
        )

    out_dir = WAPP_SRC_ROOT / "primitives" / "dbl_list"
    out_dir.mkdir(parents=True, exist_ok=True)

    common_decl_types: List[CStruct] = []

    datatypes: dict[CDataType, DblListData] = {
        CType.VOID: DblListData(node_typename="GenericNode", list_typename="GenericList"),
        "void *": DblListData(node_typename="VoidPNode", list_typename="VoidPList"),
        "Str8": DblListData(
            node_typename="Str8Node",
            list_typename="Str8List",
            hdr_decl_types=[
                CStruct(name="str8", cargs=[], typedef_name="Str8"),
            ],
        ),
    }

    for _type in CType:
        if _type == CType.VOID:
            continue

        type_title = _type.value.title()
        datatypes[_type] = DblListData(
            node_typename=f"{type_title}Node",
            list_typename=f"{type_title}List",
        )

    datatypes.update(user_datatypes)

    snippets_dir = Path(__file__).parent / "snippets"

    header = CHeader(
        name="dbl_list",
        decl_types=[*common_decl_types],
        includes=[],
        types=[],
        funcs=[]
    )

    source = CSource(
        name=header.name,
        decl_types=[*common_decl_types],
        includes=[
            CInclude(header, local=True, same_dir=True),
            CInclude(
                header=str(convert_to_relative(WAPP_SRC_ROOT / "common" / "assert" / "assert.h", out_dir)).replace("\\", "/"),
                local=True
            ),
            CInclude(header="stddef.h"),
        ],
        internal_funcs=[],
        funcs=header.funcs
    )

    for _type, dbl_list_data in datatypes.items():
        type_string = get_datatype_string(_type)
        clean_type_string = type_string.replace(" ", "").replace("*", "_ptr")
        type_string_upper = clean_type_string.upper()
        type_string_lower = clean_type_string.lower()

        node = CStruct(
            name=dbl_list_data.node_typename,
            cargs=[
                CArg(name="item", _type=type_string, pointer=CPointer(_type=CPointerType.SINGLE)),
            ],
        )
        node.cargs.extend([
            CArg(name="prev", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
            CArg(name="next", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
        ])
        
        dl_list = CStruct(
            name=dbl_list_data.list_typename,
            cargs=[
                CArg(name="first", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
                CArg(name="last", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
                CArg(name="node_count", _type=CType.U64),
            ],
        )

        header.types.extend([node, dl_list])
        header.decl_types.extend(dbl_list_data.hdr_decl_types)
        source.decl_types.extend(dbl_list_data.src_decl_types)
        if isinstance(_type, CType) and _type == CType.VOID:
            # Don't define any functions for the generic node and list
            continue

        node_cmacro = CMacro(
            name=f"wapp_{type_string_lower}_list_node(ITEM_PTR)",
            value=__format_func_body(
                filename=snippets_dir / "list_node",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
        )

        node_cppmacro = CMacro(
            name=f"wapp_{type_string_lower}_list_node(ITEM_PTR)",
            value=__format_func_body(
                filename=snippets_dir / "list_node_cpp",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
        )

        get_func = CFunc(
            name=f"wapp_{type_string_lower}_list_get",
            ret_type=node,
            args=[
                CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST),
                CArg(name="index", _type=CType.U64),
            ],
            body=__format_func_body(
                filename=snippets_dir / "list_get",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
            pointer=CPointer(CPointerType.SINGLE),
        )

        push_front_func = CFunc(
            name=f"wapp_{type_string_lower}_list_push_front",
            ret_type=CType.VOID,
            args=[
                CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
                CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
            ],
            body=__format_func_body(
                filename=snippets_dir / "list_push_front",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
        )

        push_back_func = CFunc(
            name=f"wapp_{type_string_lower}_list_push_back",
            ret_type=CType.VOID,
            args=[
                CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
                CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
            ],
            body=__format_func_body(
                filename=snippets_dir / "list_push_back",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
        )

        insert_func = CFunc(
            name=f"wapp_{type_string_lower}_list_insert",
            ret_type=CType.VOID,
            args=[
                CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
                CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
                CArg(name="index", _type=CType.U64),
            ],
            body=__format_func_body(
                filename=snippets_dir / "list_insert",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
        )

        pop_front_func = CFunc(
            name=f"wapp_{type_string_lower}_list_pop_front",
            ret_type=node,
            args=[
                CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
            ],
            body=__format_func_body(
                filename=snippets_dir / "list_pop_front",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
            pointer=CPointer(CPointerType.SINGLE),
        )

        pop_back_func = CFunc(
            name=f"wapp_{type_string_lower}_list_pop_back",
            ret_type=node,
            args=[
                CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
            ],
            body=__format_func_body(
                filename=snippets_dir / "list_pop_back",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
            pointer=CPointer(CPointerType.SINGLE),
        )

        remove_func = CFunc(
            name=f"wapp_{type_string_lower}_list_remove",
            ret_type=node,
            args=[
                CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
                CArg(name="index", _type=CType.U64),
            ],
            body=__format_func_body(
                filename=snippets_dir / "list_remove",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
            pointer=CPointer(CPointerType.SINGLE),
        )

        empty_func = CFunc(
            name=f"wapp_{type_string_lower}_list_empty",
            ret_type=CType.VOID,
            args=[
                CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
            ],
            body=__format_func_body(
                filename=snippets_dir / "list_empty",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
        )

        node_to_list_func = CFunc(
            name=f"{type_string_lower}_node_to_list",
            ret_type=dl_list,
            args=[
                CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
            ],
            body=__format_func_body(
                filename=snippets_dir / "node_to_list",
                type_string=type_string,
                type_string_upper=type_string_upper,
                type_string_lower=type_string_lower,
                node_typename=dbl_list_data.node_typename,
                list_typename=dbl_list_data.list_typename
            ),
            qualifiers=[CQualifier.INTERNAL],
        )

        header.c_macros.append(node_cmacro)
        header.cpp_macros.append(node_cppmacro)
        header.funcs.extend([
            get_func,
            push_front_func,
            push_back_func,
            insert_func,
            pop_front_func,
            pop_back_func,
            remove_func,
            empty_func,
        ])

        source.internal_funcs.append(node_to_list_func)
        source.funcs = header.funcs

    header.save(out_dir)
    source.save(out_dir)
