Route Carrefour::findRouteByPosition(Position p) {
    std::list<Route *>::iterator it;
    for (it = this->routes.begin() ; it != this->routes.end() ; it++) {
        Route * route = (Route *) *it;
        if (*(route->getPosition()) == p) {
            return *route;
        }
    }
}
void CarrefourThread::run() {
    Position * p = this->v->getPosition();
    Position * pArrivee = this->v->getPositionArrivee();
    while (!this->v->estArrivee()) {
        Position * oldPos = new Position(*p);
        bool positionHasChanged = false;
         if (p->getPositionY() < pArrivee->getPositionY()) {
             Position * pos = new Position(p->getPositionX(), p->getPositionY());
             pos->avancer();
             if (this->c->hasRoute(pos)) {
                 Route route = this->c->findRouteByPosition(*pos);
                 if (route.getPosition()->getOrientation() == horizontal || route.getPosition()->getOrientation() == all) {
                     v->avancer();
                     positionHasChanged = true;
                 }
             }
        }
        if (p->getPositionY() > pArrivee->getPositionY() && !positionHasChanged) {
            Position * pos = new Position(p->getPositionX(), p->getPositionY());
            pos->reculer();
            if (this->c->hasRoute(pos)) {
                Route route = this->c->findRouteByPosition(*pos);
                if (route.getPosition()->getOrientation() == horizontal || route.getPosition()->getOrientation() == all) {
                    v->reculer();
                    positionHasChanged = true;
                }
            }
        } else if (p->getPositionX() > pArrivee->getPositionX() && !positionHasChanged) {
            Position * pos = new Position(p->getPositionX(), p->getPositionY());
            pos->gauche();
            if (this->c->hasRoute(pos)) {
                Route route = this->c->findRouteByPosition(*pos);
                if (route.getPosition()->getOrientation() == vertical || route.getPosition()->getOrientation() == all) {
                    v->tournerGauche();
                    positionHasChanged = true;
                }
            }
        } else if (p->getPositionX() < pArrivee->getPositionX() && !positionHasChanged) {
            Position * pos = new Position(p->getPositionX(), p->getPositionY());
            pos->droite();
            if (this->c->hasRoute(pos)) {
                Route route = this->c->findRouteByPosition(*pos);
                if (route.getPosition()->getOrientation() == vertical || route.getPosition()->getOrientation() == all) {
                    v->tournerDroite();
                    positionHasChanged = true;
                }
            }
        }
        if (positionHasChanged) {
            emit positionChanged(oldPos, p);
        }
        Sleep(uint(200));
    }
}
bool Carrefour::hasRoute(Position * p) {
    std::list<Route *>::iterator it;
    for (it = this->routes.begin() ; it != this->routes.end() ; it++) {
        Route * route = (Route *) *it;
        if (*(route->getPosition()) == *p) {
            return true;
        }
    }
    return false;
}
void Carrefour::getConfiguration(Position * p) {
    QWidget * configWidget = new QWidget();
    this->config->setQWidget(configWidget);
    int taille = 50;
    std::list<Route *>::iterator itRoutes;
    for (itRoutes = this->routes.begin() ; itRoutes != this->routes.end() ; itRoutes++) {
        Route * route = (Route *) *itRoutes;
        if (*(route->getPosition()) == *p) {
            this->config->setRoute(route);
            QLabel * label = new QLabel("Route", configWidget);
            label->move(200, taille);
            taille += 50;
        }
    }
    std::list<Vehicule *>::iterator itVehicule;
    for (itVehicule = this->objetRoutes.begin() ; itVehicule != this->objetRoutes.end() ; itVehicule++) {
        Vehicule * vehicule = (Vehicule *) *itVehicule;
        if (*(vehicule->getPosition()) == *p) {
            this->config->setVehicule(vehicule);
            QLabel * label = new QLabel(QString::fromStdString(vehicule->getType()), configWidget);
            label->move(200, taille);
            taille += 50;
            QLabel * labelPositionArrivee = new QLabel("Position d'arrivee", configWidget);
            labelPositionArrivee->move(50, taille);
            QLabel * labelX = new QLabel("X : ", configWidget);
            labelX->move(150, taille);
            QTextEdit * xTextEdit = new QTextEdit(configWidget);
            xTextEdit->move(180, taille);
            xTextEdit->setFixedSize(50, 30);
            this->config->setXTextEdit(xTextEdit);
            QLabel * labelY = new QLabel("Y : ", configWidget);
            labelY->move(250, taille);
            QTextEdit * yTextEdit = new QTextEdit(configWidget);
            yTextEdit->move(280, taille);
            yTextEdit->setFixedSize(50, 30);
            this->config->setYTextEdit(yTextEdit);
            taille += 50;
            QLabel * labelNbVehicule = new QLabel("Nb vehicule : ", configWidget);
            labelNbVehicule->move(50, taille);
            QTextEdit * nbVehiculeTextEdit = new QTextEdit(configWidget);
            nbVehiculeTextEdit->move(150, taille);
            nbVehiculeTextEdit->setFixedSize(50, 30);
            taille += 50;
        }
    }
    QPushButton * valideButton = new QPushButton("valide", configWidget);
    QObject::connect(valideButton, SIGNAL(clicked()), this, SLOT(valideConfig()));
    valideButton->move(100, taille);
    QPushButton * annuleButton = new QPushButton("annule", configWidget);
    QObject::connect(annuleButton, SIGNAL(clicked()), this, SLOT(annuleConfig()));
    annuleButton->move(300, taille);
    configWidget->show();
}