"""Projection du deplacement d'un pion"""

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from Forme import Forme


class ProjectionDep():
    def __init__(self, plateau):
        self.plateau = plateau
        self._pion = None
        self._active = False
        
        self._coord = (-1,-1)
        self._forme = Forme(self.plateau.formeCases)
        self._nbRotations = 0

    def creer(self, pion):
        self_pion = pion
        self._forme.definirForme(pion.formeDef[self.plateau.formeCases])
        self._coord = pion.position
        self._nbRotations = pion.nbRotations
        self._active = True

    def majCoord(self, coord):
        self.desactiver()
        self._coord = coord
        self.maj()

    def majRotation(self, modRotation):
        self.desactiver()
        self._nbRotations += modRotation
        self.maj()

    def coord(self):
        return self._coord

    def nbRotations(self):
        return self._nbRotations

    def desactiver(self):
        for coord in self._forme.listeCases(self._coord, self._nbRotations):
            if coord in self.plateau.cases:
                self.plateau.cases[coord].majEstCibleCurseur(False)
        self.active = False

    def projectionValide(self):
        """la projection est valide si toutes les cases de la forme existent"""
        valide = True
        for coord in self._forme.listeCases(self._coord, self._nbRotations):
            if not coord in self.plateau.cases:
                valide = False
                break 
        return valide

    def deplacementValide(self):
        """le deplacement est valide si toutes les cases de la forme sont franchissables"""
        valide = False
        compte = 0
        ok = 0
        if self.projectionValide():
            for coord in self._forme.listeCases(self._coord, self._nbRotations):
                compte += 1
                if self.plateau.cases[coord].estFranchissable(self.z()):
                    ok += 1
                else:
                    #si la case est occupee par le pion lui-meme
                    if self.plateau.cases[coord].estOccupeePar(self.z()) == None:
                        ok += 1
                    else:
                        if self.plateau.cases[coord].estOccupeePar(self.z()) == self._pion:
                            ok += 1
        valide = (ok == compte and compte > 0)
        return valide        

    def maj(self):
        self._active = True
        if self.projectionValide():
            depValide = self.deplacementValide()
            i = 0
            for coord in self._forme.listeCases(self._coord, self._nbRotations):
                i += 1
                self.plateau.cases[coord].majEstCibleCurseur(True, depValide)
                
            
            
    def z(self):
        """renvoie l'altitude du pion qu'on deplace"""
        z = 0
        try:
            z = self._pion.z
        except:
            pass
        return z
            


















        
