package biduledres;

import java.util.*;

public class Polyedre {
    private ListeSommet listeSommet;
    private ListeFace listeFace;
    public Polyedre() {
        this(new ListeSommet(), new ListeFace());
    }
    
    public Polyedre(ListeFace listeFace) {
        this(listeFace.getListeSommet(), listeFace);
    }
    public Polyedre(ListeSommet listeSommet, ListeFace listeFace) {
        this.listeSommet = listeSommet;
        this.listeFace = listeFace;
        listeFace.supprimeDoublonSommet();
    }
    
    public void addSommet(Sommet s) {
        listeSommet.add(s);
    }
    
    public void addFace(Face f) {
        listeFace.add(f);
    }
    
    public String toXML(int tab) {
        StringBuffer b = new StringBuffer();
        b.append(Util.printTab(tab, "<Polyedre nombreSommets=\"" + getNombreSommets() + "\" nombreAretes=\"" + getNombreAretes() + "\" nombreFaces=\"" + getNombreFaces()+ ">"));
        b.append(Util.toXMLListe(tab + 1, "ListeSommet", listeSommet.iterator()));
        b.append(Util.toXMLListe(tab + 1, "ListeFace", listeFace.iterator()));
        b.append(Util.printTab(tab, "</Polyedre>"));
        return b.toString();
    }
    
    public String toXML() {
        return toXML(0);
    }
    
    public ListeSommet getListeSommet() {
        return listeSommet;
    }
    
    public int getNombreSommets() {
        return getListeSommet().size();
    }
    
    public ListeFace getListeFace() {
        return listeFace;
    }
    
    public int getNombreFaces() {
        return getListeFace().size();
    }
    
    public ListeArete getListeArete() {
        ListeArete retour = new ListeArete();
        Iterator i = getListeFace().iterator();
        while(i.hasNext()) {
            Face f = (Face) i.next();
            Iterator j = f.getListeArete().iterator();
            while(j.hasNext())
                retour.add((Arete) j.next());
        }
        return retour;
    }
    
    public int getNombreAretes() {
        return getListeArete().size();
    }
    
    public Face getFaceVoisine(Face face, Arete arete) {
        ListeFace faces = getListeFace(arete);
        if (faces.size() != 2) return null;
        Iterator i = faces.iterator();
        Face face1 = (Face) i.next();
        if (face1.equals(face)) return (Face) i.next();
        else return face1;
    }
    
    // revoie les deux faces frontalières
    public ListeFace getListeFace(Arete arete) {
        ListeFace retour = new ListeFace();
        Iterator iterator = getListeFace().iterator();
        while(iterator.hasNext()) {
            Face face = (Face) iterator.next();
            if (face.contains(arete)) retour.add(face);
        }
        return retour;
    }
    
    // on cherche une seule arete:
    private Arete get1Arete(Sommet sommet) {
        Iterator i = getListeArete().iterator();
        while(i.hasNext()) {
            Arete arete = (Arete) i.next();
            if (arete.contains(sommet)) return arete;
        }
        return null;
    }
    
    
    // on cherche une seule face
    public Face get1Face(Arete arete) {
        Iterator iterator = getListeFace().iterator();
        while(iterator.hasNext()) {
            Face face = (Face) iterator.next();
            if (face.contains(arete)) return face;
        }
        return null;
    }
    
    // segments triés
    public ListeArete getListeArete(Sommet sommet) {
        ListeArete retour = new ListeArete();
        Arete arete0 = get1Arete(sommet);
        retour.add(arete0);
        Face face = get1Face(arete0);
        Arete arete = face.getAreteVoisine(arete0, sommet);
        while(!arete0.equals(arete)) {
            retour.add(arete);
            face = getFaceVoisine(face, arete);
            arete = face.getAreteVoisine(arete, sommet);
        }
        return retour;
    }
    
    public String getNombres() {
        return "s = " + getNombreSommets() + ",  a = " + getNombreAretes() + ",  f = " + getNombreFaces();
    }
}