from Outils.Directory import GenerateDirectory
from pathlib import Path

def createFileController(nom, specifique = ''):

    generateDirectory = GenerateDirectory(nom, "/Modules/"+nom+"/Controller", specifique)
    directory = generateDirectory.getDirectory()
    namespace = generateDirectory.getNamespace()
    nom = generateDirectory.getNom()

    Path(directory).mkdir(parents=True, exist_ok=True)


    with open(directory+"/IndexController.php","x", encoding='utf-8') as file:
        file.write("<?php\n\n")
        file.write("namespace "+namespace+";\n\n")

        file.write("use Skeleton\Controller;\n\n")
        
        file.write("class IndexController extends Controller\n")
        file.write("{\n\n")
        
        file.write("    // Function qui s'execute avant les actions\n")
        file.write("    public function init()\n")
        file.write("    {\n")
        file.write("    }\n\n")
        file.write("    public function indexAction()\n")
        file.write("    {\n\n")

      
        file.write("        // recuperation des donnée en POST\n")
        file.write("        $post = $this->request->getParsedBody();\n\n")
        file.write("        // recuperation du parametre id en GET (envoyé via l'url)\n")
        file.write("        $id   = $this->request->getAttribute('id');\n\n")

        file.write("        return $this->renderer->render('@Index"+nom+"/index', [\n")
        file.write("            'id'        => $id,\n")
        file.write("            'post'        => $post\n")
        file.write("        ]);\n")
        
        file.write("    }\n\n")

        file.write("    // Function qui s'execute apres les actions\n")
        file.write("    public function pageInit()\n")
        file.write("    {\n")
        file.write("    }\n")
        file.write("}\n")
        file.close()
