from otree.api import Currency as c, currency_range
from ._builtin import Page, WaitPage
from .models import Constants

import random
import time


class Bienvenue(Page):
    form_model = 'player'
    form_fields = []

    def before_next_page(self):
        self.player.app_sequence_select = self.player.participant.vars['app_sequence_select']
        self.player.app_sequence_select_n =self.player.participant.vars['app_sequence_select_n']
        #self.player.questionnaire_before = self.player.participant.vars['questionnaire_before']

        # self.player.game_num_select = random.randint(1,4)
        #
        # self.player.game_select_n = getattr(Constants, f'app_seq{self.player.app_sequence_select}')[self.player.game_num_select-1]
        #
        #     # result of GP
        # self.player.tirageriskresult = self.participant.vars['tirageriskresult_j1']
        # if self.player.tirageriskresult == "Succès":
        #     self.player.tirageriskresult_en = "Success"
        # elif self.player.tirageriskresult == "Echec":
        #     self.player.tirageriskresult_en = "Failure"
        #
        # self.player.risk11_a = self.participant.vars['risk11_a_j1']
        # self.player.risk11_b = self.participant.vars['risk11_b_j1']
        # self.player.gainrisk = self.participant.vars['gainrisk_j1']
        # self.player.gainrisk_eur = self.player.gainrisk * Constants.exchangerate_gp_bw
        #
        # # result of BW
        # self.player.tirage_color_n = self.participant.vars['tirage_color_n']
        # self.player.gain_bw        = self.participant.vars['gain_bw']
        # self.player.gain_bw_eur    = self.participant.vars['gain_bw_eur']
        #
        # # result of MDG_asso
        # self.player.mdgasso_dgm_selected   = self.player.participant.vars['mdgasso_dgm_selected']
        # self.player.mdgasso_round_selected = self.player.participant.vars['mdgasso_round_selected']
        # self.player.mdgasso_dot1_select = self.player.participant.vars['mdgasso_dot1_select']
        # self.player.mdgasso_s1_select   = self.player.participant.vars['mdgasso_s1_select']
        # self.player.mdgasso_g1_select   = self.player.participant.vars['mdgasso_g1_select']
        # self.player.mdgasso_keep_select = self.player.participant.vars['mdgasso_keep_select']
        # self.player.mdgasso_sent_select = self.player.participant.vars['mdgasso_sent_select']
        # self.player.mdgasso_gain_dgm    = self.player.participant.vars['mdgasso_gain_dgm']
        # self.player.mdgasso_gain_asso  = self.player.participant.vars['mdgasso_gain_asso']
        # self.player.mdgasso_gain_dgm_eur = self.player.participant.vars['mdgasso_gain_dgm_eur']
        # self.player.mdgasso_gain_asso_eur = self.player.participant.vars['mdgasso_gain_asso_eur']
        #
        # # result of MDG_classique
        # self.player.mdgclass_dgm_selected   = self.player.participant.vars['mdgclass_dgm_selected']
        # self.player.mdgclass_round_selected = self.player.participant.vars['mdgclass_round_selected']
        # self.player.mdgclass_dot1_select = self.player.participant.vars['mdgclass_dot1_select']
        # self.player.mdgclass_s1_select = self.player.participant.vars['mdgclass_s1_select']
        # self.player.mdgclass_g1_select = self.player.participant.vars['mdgclass_g1_select']
        # self.player.mdgclass_keep_select = self.player.participant.vars['mdgclass_keep_select']
        # self.player.mdgclass_sent_select = self.player.participant.vars['mdgclass_sent_select']
        # self.player.mdgclass_gain_dgm = self.player.participant.vars['mdgclass_gain_dgm']
        # self.player.mdgclass_gain_joueur2 = self.player.participant.vars['mdgclass_gain_joueur2']
        # self.player.mdgclass_gain_dgm_eur = self.player.participant.vars['mdgclass_gain_dgm_eur']
        # self.player.mdgclass_gain_joueur2_eur = self.player.participant.vars['mdgclass_gain_joueur2_eur']
        # self.player.dictatorYES = random.randint(0, 1)
        # if self.player.dictatorYES == 1:
        #     self.player.dictatorYES_n = "player_1"
        # elif self.player.dictatorYES == 0:
        #     self.player.dictatorYES_n = "player_2"
        #
        #
        # if self.player.game_select_n == "gp":
        #     self.player.game_select = 1
        #     self.player.gainfinal_eur = self.player.gainrisk_eur
        #     self.player.gainfinal_eur_showupfee = self.player.gainfinal_eur + Constants.showupfee
        # elif  self.player.game_select_n == "bw":
        #     self.player.game_select = 2
        #     self.player.gainfinal_eur = self.player.gain_bw_eur
        #     self.player.gainfinal_eur_showupfee = self.player.gainfinal_eur + Constants.showupfee
        # elif self.player.game_select_n == "MDG_asso":
        #     self.player.game_select = 3
        #     self.player.gainfinal_eur = self.player.mdgasso_gain_dgm_eur
        #     self.player.gainfinal_eur_showupfee = self.player.gainfinal_eur + Constants.showupfee
        #     self.player.gainfinal_asso_eur = self.player.mdgasso_gain_asso_eur
        #     self.player.gainfinal_asso_eur_f = round(self.player.gainfinal_asso_eur, 0)
        # elif  self.player.game_select_n == "MDG_class" and self.player.dictatorYES == 1:
        #     self.player.game_select = 4
        #     self.player.gainfinal_eur = self.player.mdgclass_gain_dgm_eur
        #     self.player.gainfinal_eur_showupfee = self.player.gainfinal_eur + Constants.showupfee
        # elif  self.player.game_select_n == "MDG_class" and self.player.dictatorYES == 0:
        #     self.player.game_select = 4
        #     self.player.gainfinal_eur = -999
        #     self.player.gainfinal_eur_showupfee = -999
        #
        # self.player.gainfinal_eur_f           = round(self.player.gainfinal_eur,0)
        # self.player.gainfinal_eur_showupfee_f = round(self.player.gainfinal_eur_showupfee,0)


        # if   self.player.game_select_n != "MDG_class" :
        #     self.player.payoff = self.player.gainfinal_eur_showupfee
        # elif self.player.game_select_n == "MDG_class"   and self.player.dictatorYES == 1:
        #     self.player.payoff = self.player.gainfinal_eur_showupfee
        # elif self.player.game_select_n == "MDG_class"   and self.player.dictatorYES == 0:
        #     self.player.payoff = -999

class Questionnaire4(Page):
    form_model = 'player'
    form_fields = ['q_40_etude', 'q_41_gender', 'q_42_age',
                   'q_43_nationalite', 'q_44_revenu',
                   'q3_countries', 'q_46_logement_ville', 'q_31',
                   'q_47_residence', 'q_48_satisfaction', 'q_49_propositions'
                   ]

class Questionnaire5(Page):
    pass

class Questionnaire6(Page):
    form_model = 'player'
    form_fields = [
    'g1_easy_to_understand', 'g1_involves_risk', 'g1_annoying_boring',
    'g2_easy_to_understand', 'g2_involves_risk', 'g2_annoying_boring',
    'g3_easy_to_understand', 'g3_involves_altruism', 'g3_annoying_boring',
    'g4_easy_to_understand', 'g4_involves_altruism', 'g4_annoying_boring',
                   ]

class Questionnaire7crt(Page):
    form_model = 'player'
    form_fields = ['crt1', 'crt2', 'crt3',
                   ]
    timeout_seconds = 180  # 3 minutes

    def before_next_page(self):
        # Enregistre automatiquement les réponses si le temps expire
        self.player.gain_crt = 0
        if self.player.crt1 == 5:
            self.player.gain_crt +=1
        if self.player.crt2 == 5:
            self.player.gain_crt +=1
        if self.player.crt3 == 47:
            self.player.gain_crt += 1
        if self.player.gainfinal_eur_showupfee != -999:
            self.player.gainfinal_eur_showupfee_f2025 = self.player.gainfinal_eur_showupfee_f + self.player.gain_crt + 3
        else:
            self.player.gainfinal_eur_showupfee_f2025 = -999


class Result(Page):
    form_model = 'player'
    form_fields = [
        'lastname',
        'firstname',
        'email',
        'room',
    ]

class FinalPage(Page):
    pass

class FinalPage2(Page):

    def vars_for_template(self):

        return dict(
            game_select=self.player.game_select,

        )




page_sequence = [
    Bienvenue,
    #Questionnaire4,
    Questionnaire5,
    Questionnaire6,
    #Questionnaire7crt,
    #Result,
    #FinalPage,  # page test à désactiver
    #FinalPage2,
]
