#include "math.h"
#include "Strategie.h"
#include "Geometrie_robot.h"
#include "Commande_vitesse.h"
#include "Strategie_2024_pots.h"
#include "i2c_annexe.h"
#include "Localisation.h"
#include "Score.h"

enum etat_action_t active_panneau_solaire(float pos_x, uint32_t step_ms);

/// @brief Fonction demande au robot d'aller activer les panneaux solaires
enum etat_action_t Strat_2024_tourner_panneaux(enum couleur_t couleur, uint32_t step_ms){
    enum etat_action_t etat_action;
    struct trajectoire_t trajectoire;
    static int tempo_ms;

    static enum {
        TP_APPROCHE_PANNEAU_1, // Position et orientation pour attaquer le panneau 1, bras 2 qui pousse
        TP_TOURNE_PANNEAU_1,
        TP_TOURNE_PANNEAU_2,
        TP_TOURNE_PANNEAU_3,
        TP_TOURNE_PANNEAU_4,
        TP_TOURNE_PANNEAU_5,
        TP_TOURNE_PANNEAU_6,
        TP_DEGAGEMENT,
        TP_DEGAGEMENT_ACTIF
    } etat_tourne_panneaux = TP_APPROCHE_PANNEAU_1;


    switch (etat_tourne_panneaux)
    {
        case TP_APPROCHE_PANNEAU_1:
            Trajet_config(TRAJECT_CONFIG_AVANCE_ET_TOURNE);
            if(couleur == COULEUR_BLEU){
                etat_action = Strategie_tourner_et_aller_a(
                    333, 300, (150. *DEGRE_EN_RADIAN),
                    EVITEMENT_ARRET_DEVANT_OBSTACLE, step_ms);
            }else{
                etat_action = Strategie_tourner_et_aller_a(
                    3000 - 333, 300, (150. *DEGRE_EN_RADIAN),
                    EVITEMENT_ARRET_DEVANT_OBSTACLE, step_ms);
            }
            if (etat_action == ACTION_TERMINEE){
                etat_tourne_panneaux = TP_TOURNE_PANNEAU_1;
            }else if(etat_action == ACTION_ECHEC){
                return ACTION_TERMINEE;
            }
            break;

        case TP_TOURNE_PANNEAU_1:
            if(couleur == COULEUR_BLEU){
                etat_action = Strategie_tourner_et_aller_a(
                    333, 180, (150. *DEGRE_EN_RADIAN), EVITEMENT_PAUSE_DEVANT_OBSTACLE, step_ms);
            }else{
                etat_action = Strategie_tourner_et_aller_a(
                    3000 - 333, 180, (150. *DEGRE_EN_RADIAN), EVITEMENT_PAUSE_DEVANT_OBSTACLE, step_ms);
            }
            if (etat_action == ACTION_TERMINEE){
                Score_ajout_panneau(1);
                etat_tourne_panneaux = TP_TOURNE_PANNEAU_2;
            }
            break;

        case TP_TOURNE_PANNEAU_2:
            if(couleur == COULEUR_BLEU){
                etat_action = active_panneau_solaire(568, step_ms);
            }else{
                etat_action = active_panneau_solaire(3000 - 543, step_ms);
            }
            if (etat_action == ACTION_TERMINEE){
                Score_ajout_panneau(1);
                etat_tourne_panneaux = TP_TOURNE_PANNEAU_3;
            }else if(etat_action == ACTION_ECHEC){
                etat_tourne_panneaux = TP_DEGAGEMENT;
            }
            break;

        case TP_TOURNE_PANNEAU_3:
            if(couleur == COULEUR_BLEU){
                etat_action = active_panneau_solaire(813, step_ms);
            }else{
                etat_action = active_panneau_solaire(3000 - 768, step_ms);
            }
            if (etat_action == ACTION_TERMINEE){
                Score_ajout_panneau(1);
                etat_tourne_panneaux = TP_DEGAGEMENT;
            }else if(etat_action == ACTION_ECHEC){
                etat_tourne_panneaux = TP_DEGAGEMENT;
            }
            break;

        case TP_TOURNE_PANNEAU_4:
            if(couleur == COULEUR_BLEU){
                Trajectoire_bezier(&trajectoire, 773, 200, 855, 310, 1323, 400, 1323, 200, (150. *DEGRE_EN_RADIAN), (150. *DEGRE_EN_RADIAN));
            }else{
                Trajectoire_bezier(&trajectoire, 3000-773, 200, 3000-855, 310, 3000-1323, 400, 3000-1323, 200, (150. *DEGRE_EN_RADIAN), (150. *DEGRE_EN_RADIAN));
            }
            etat_action =Strategie_parcourir_trajet(trajectoire, step_ms, EVITEMENT_ARRET_DEVANT_OBSTACLE);
            if (etat_action == ACTION_TERMINEE){
                Score_ajout_panneau(1);
                etat_tourne_panneaux = TP_TOURNE_PANNEAU_5;
            }else if(etat_action == ACTION_ECHEC){
                etat_tourne_panneaux = TP_DEGAGEMENT;
            }
            break;

        case TP_TOURNE_PANNEAU_5:
            if(couleur == COULEUR_BLEU){
                Trajectoire_bezier(&trajectoire, 1323, 200, 1410, 310, 1547, 400, 1547, 200, (150. *DEGRE_EN_RADIAN), (150. *DEGRE_EN_RADIAN));
            }else{
                Trajectoire_bezier(&trajectoire, 3000-1323, 200, 3000-1410, 310, 3000-1547, 400, 3000-1547, 200, (150. *DEGRE_EN_RADIAN), (150. *DEGRE_EN_RADIAN));
            }
            etat_action =Strategie_parcourir_trajet(trajectoire, step_ms, EVITEMENT_ARRET_DEVANT_OBSTACLE);
            if (etat_action == ACTION_TERMINEE){
                Score_ajout_panneau(1);
                etat_tourne_panneaux = TP_TOURNE_PANNEAU_6;
            }else if(etat_action == ACTION_ECHEC){
                etat_tourne_panneaux = TP_DEGAGEMENT;
            }
            break;
        
        case TP_TOURNE_PANNEAU_6:
            if(couleur == COULEUR_BLEU){
                Trajectoire_bezier(&trajectoire, 1547, 200, 1630, 310, 1771, 400, 1771, 200, (150. *DEGRE_EN_RADIAN), (150. *DEGRE_EN_RADIAN));
            }else{
                Trajectoire_bezier(&trajectoire, 3000-1547, 200, 3000-1630, 310, 3000-1771, 400, 3000-1771, 200, (150. *DEGRE_EN_RADIAN), (150. *DEGRE_EN_RADIAN));
            }
            etat_action =Strategie_parcourir_trajet(trajectoire, step_ms, EVITEMENT_ARRET_DEVANT_OBSTACLE);
            if (etat_action == ACTION_TERMINEE){
                Score_ajout_panneau(1);
                etat_tourne_panneaux = TP_TOURNE_PANNEAU_6;
            }else if(etat_action == ACTION_ECHEC){
                etat_tourne_panneaux = TP_DEGAGEMENT;
            }
            break;

        case TP_DEGAGEMENT:
            if(Localisation_get().y_mm < 250){
                etat_tourne_panneaux = TP_DEGAGEMENT_ACTIF;
            }else{
                commande_vitesse_stop();
                return ACTION_TERMINEE;
            }
            break;

        case TP_DEGAGEMENT_ACTIF:
                return Strategie_aller_a(Localisation_get().x_mm, 250, EVITEMENT_PAUSE_DEVANT_OBSTACLE, step_ms);
            break;
    }
    return ACTION_EN_COURS;
}


enum etat_action_t active_panneau_solaire(float pos_x, uint32_t step_ms){
    struct trajectoire_t trajectoire;
    enum etat_action_t etat_action;
    Trajectoire_bezier(&trajectoire, Localisation_get().x_mm, Localisation_get().y_mm,
         Localisation_get().x_mm, 400, pos_x, 400, pos_x, 180, 
         Geometrie_get_angle_optimal(Localisation_get().angle_radian, 150. *DEGRE_EN_RADIAN), 
         Geometrie_get_angle_optimal(Localisation_get().angle_radian, 150. *DEGRE_EN_RADIAN));

    etat_action = Strategie_parcourir_trajet(trajectoire, step_ms, EVITEMENT_ARRET_DEVANT_OBSTACLE);
    return etat_action;
}