import os

from typing import List
from ..interfaces import ClassInfo, FileInfo, DtoGenArgs, Info


class FileWriter:
    args: DtoGenArgs
    classes: List[ClassInfo]
    dto_gen_info: Info

    def __init__(self, args: DtoGenArgs, classes: List[ClassInfo], dto_gen_info: Info):
        self.args = args
        self.classes = classes
        self.dto_gen_info = dto_gen_info

    def get_extension(self) -> str:
        raise NotImplementedError("get_extension not implemented")

    def generate_files(self) -> List[FileInfo]:
        raise NotImplementedError("generate_files not implemented")

    def generate_comment(self, comment: str) -> str:
        raise NotImplementedError("generate_comment not implemented")

    def write(self):
        raise NotImplementedError("write not implemented")


class __FileWriter(FileWriter):
    def get_extension(self) -> str:
        raise NotImplementedError("get_extension not implemented")

    def generate_files(self) -> List[FileInfo]:
        raise NotImplementedError("generate_files not implemented")

    def generate_comment(self, comment: str) -> str:
        raise NotImplementedError("generate_comment not implemented")

    def write(self):
        if os.path.exists(self.args.output_dir):
            raise Exception("folder already exists")

        os.mkdir(self.args.output_dir)

        files_to_write = self.generate_files()
        for file in files_to_write:
            comment_lines = "\n\n" + "\n".join(
                [
                    self.generate_comment("DO NOT MAKE CHANGES ON THIS FILE"),
                    self.generate_comment("IT IS AUTO GENERATED BY dtogen"),
                    self.generate_comment(
                        f"source > {self.dto_gen_info.name}:{self.dto_gen_info.version}"
                    ),
                ]
            )
            with open(
                f"{self.args.output_dir}/{file.file_name}.{self.get_extension()}", "w"
            ) as f:
                f.write(file.file_text + comment_lines)
