from typing import List
from helper.fs import FS
import os
from src.model.robossembler_assets import RobossemblerAssets


def doubleTagGenerate(instances: List[str], predicates: str, tag: str):
    result = ""
    for detail in instances:
        result += "\n        (" + predicates + " " + detail + " " + tag + ")" + "\n"
    return result


def typeGenerator(types: list[str], typeName: str):
    result = ""
    for t in types:
        result += t + " "
    result += "- " + typeName
    return result


def assemblyTagGenerate(sequences: list[str]):
    result = ""
    inc = 0
    for el in sequences:
        result += doubleTagGenerate(
            instances=[el], predicates="part_of", tag="subasm" + str(inc)
        )
        inc += 1

    inc = 0
    for el in sequences[1 : sequences.__len__()]:
        result += doubleTagGenerate(
            instances=["subasm" + str(inc)],
            predicates="assembly_order",
            tag="subasm" + str(inc + 1),
        )
        inc += 1
    return result


class AssemblyToPddlUseCase:
    def call(assembly: List[str], robossemblerDb: RobossemblerAssets):
        result = ""
        robots = robossemblerDb.getAllRobotsName()
        for robot in robots:
            result += (
                FS.readFile(
                    os.path.dirname(os.path.realpath(__file__))
                    + "/../../mocks"
                    + "/problem.txt"
                )
                .replace(
                    "${types}",
                    typeGenerator(assembly, "part")
                    + "\n        "
                    + typeGenerator(
                        list(
                            map(
                                lambda x: "subasm" + str(x),
                                list(range(assembly.__len__())),
                            )
                        ),
                        "assembly",
                        robot,
                    ),
                )
                .replace(
                    "${assembled}",
                    assemblyTagGenerate(assembly),
                )
                .replace(
                    "${part_at}",
                    doubleTagGenerate(
                        instances=assembly, predicates="part_at", tag="workspace1"
                    ),
                )
            ).replace("${target}", "subasm" + str(assembly.__len__() - 1))
        return {
            "problem": result,
            "domain": FS.readFile(
                os.path.dirname(os.path.realpath(__file__))
                + "/../../mocks"
                + "/domain.txt"
            ),
        }
