"""
https://nuitducode.github.io/DOCUMENTATION/PYTHON/01-presentation/
https://github.com/kitao/pyxel/blob/main/docs/README.fr.md
"""

import pyxel,random

SIZE = 8
W,H = 32,16
WIDTH = SIZE * W
HEIGHT = SIZE * H

class Cellule:
    def __init__(self, murNord, murEst, murSud, murOuest):
        self.murs={'N':murNord,'E':murEst,'S':murSud,'O':murOuest}


class App:
    def __init__(self):
        pyxel.init(WIDTH+1,HEIGHT+1)
        self.grille = self.construire_grille(H, W)
        self.pile = list()
        pyxel.rectb(0, 0, WIDTH+1, HEIGHT+1, pyxel.COLOR_NAVY)
        for lig in range(H):
            for col in range(W):
                pyxel.rect((col+1)*SIZE, lig*SIZE, 1, SIZE, pyxel.COLOR_NAVY)
                pyxel.rect(col*SIZE, (lig+1)*SIZE, SIZE+1, 1, pyxel.COLOR_NAVY)
        self.creer_labyrinthe_alea()
        pyxel.run(self.update, self.draw)

    def construire_grille(self, hauteur, longueur):
        grille = []
        for i in range(hauteur):
            ligne = []
            for j in range(longueur):
                cellule = Cellule(True, True, True, True)
                ligne.append(cellule)
            grille.append(ligne)
        return grille

    def dessine_cas_de_base(self,ligne, colonne, haut, long):
        if haut == 1 : # Cas de base
            for k in range(colonne,colonne+long-1):
                self.creer_passage(ligne, k+1, ligne, k)
        elif long == 1: # Cas de base
            for k in range(ligne,ligne + haut-1):
                self.creer_passage(k+1, colonne, k, colonne)

    def creer_passage(self, c1_lig, c1_col, c2_lig, c2_col):
        cellule1 = self.grille[c1_lig][c1_col]
        cellule2 = self.grille[c2_lig][c2_col]
        # cellule2 au Nord de cellule1
        if c1_lig - c2_lig == 1 and c1_col == c2_col:
            cellule1.murs['N'] = False
            cellule2.murs['S'] = False
            pyxel.rect(c1_col*SIZE+1, c1_lig*SIZE, SIZE-1, 1, pyxel.COLOR_BLACK)
        # cellule2 à l’Ouest de cellule1
        elif c1_lig == c2_lig and c1_col - c2_col == 1:
            cellule1.murs['O'] = False
            cellule2.murs['E'] = False
            pyxel.rect(c1_col*SIZE, c1_lig*SIZE+1, 1, SIZE-1, pyxel.COLOR_BLACK)
        

    def creer_labyrinthe_alea(self, ligne=0, colonne=0, haut=None, long=None):
        if haut is None: haut = len(self.grille)
        if long is None: long= len(self.grille[0])
        if haut == 1 or long == 1 : # Cas de base
            self.pile.append(("dessine_cas_de_base",ligne,colonne,haut,long))
        else: # Appels récursifs
            if haut >= long: # on coupe horizontalement
                coupe = haut//2    
                lig = ligne + coupe #milieu
                self.pile.append(("trace_ligneH",lig,colonne,1,long))
                #passage aléatoire 
                col = random.randint(colonne,colonne+long-1)
                #self.creer_passage(lig, col, lig-1, col)
                self.pile.append(("coupe_horizontale",lig,col,lig-1,col))
                self.creer_labyrinthe_alea(ligne, colonne, coupe, long)
                self.creer_labyrinthe_alea(lig, colonne, coupe, long) 
            else: #haut<long coupe verticale
                coupe = long//2
                col = colonne + coupe #milieu
                self.pile.append(("trace_ligneV",ligne,col,haut,1))
                #passage aléatoire
                lig = random.randint(ligne,ligne+haut-1)
                #self.creer_passage(lig, col, lig, col-1)
                self.pile.append(("coupe_verticale",lig,col,lig,col-1))
                self.creer_labyrinthe_alea(ligne, colonne, haut, coupe)
                self.creer_labyrinthe_alea(ligne, col, haut, coupe)


    def update(self):
        """mise à jour des variables (30 fois par seconde)"""

    def draw(self):
        if pyxel.frame_count % 1  == 0 and len(self.pile)>0 :
            action,ligne,colonne,haut,long = self.pile.pop(0)
            if action=="dessine_cas_de_base":
                self.dessine_cas_de_base(ligne,colonne, haut, long)
            elif action=="trace_ligneH":
                pyxel.rect(colonne*SIZE, ligne*SIZE, long*SIZE, 1, pyxel.COLOR_CYAN)
            elif action=="trace_ligneV":
                pyxel.rect(colonne*SIZE, ligne*SIZE, 1, haut*SIZE, pyxel.COLOR_PINK)
            elif action=="coupe_horizontale":
                self.creer_passage(ligne, colonne, ligne-1, colonne)
            else:
                self.creer_passage(ligne, colonne, ligne, colonne-1)

App()