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

from .models import Player as OtreePlayer

import copy

#################################################
#################################################
def save_decisionpage_data(request):
    # Link to the proper player
    pk_passed_from_client = request.GET.get('playerpk', None)
    player = OtreePlayer.objects.get(pk=pk_passed_from_client)
    # Save the data ['information_revealed', 'points', 'medical_treatment', 'confidence_level']
    player.information_revealed = request.GET.get('information_revealed', None)
    player.points = int(request.GET.get('nbpoints', None))
    player.save()
    # Send back response
    data = {
        'saved_information_revealed': player.information_revealed,
        'saved_nbpoints': player.points,
    }
    return JsonResponse(data)


#################################################
#################################################
class ExpertisePage(Page):
    form_model = 'player'
    form_fields = ['speciality', 'prof_activity', 'univ_activity']

    def is_displayed(self):
        return((self.subsession.round_number == 1)
               & (self.session.config['treatment'] == "experts"))


#################################################
#################################################
class AcknowledgementPage(Page):
    def is_displayed(self):
        return self.subsession.round_number == 1


#################################################
#################################################
class InstructionsPage(Page):
    def is_displayed(self):
        return self.subsession.round_number == 1


class SituationPage(Page):
    def is_displayed(self):
        return self.subsession.round_number == 1


class PropositionTherapeutiquePage(Page):
    def is_displayed(self):
        return self.subsession.round_number == 1


class CriteresPage(Page):
    def is_displayed(self):
        return self.subsession.round_number == 1


#################################################
#################################################
class DecisionPage(Page):
    form_model = 'player'
    form_fields = ['information_revealed', 'points', 'medical_treatment', 'confidence_level']

    def vars_for_template(self):
        situation = copy.deepcopy(Constants.c_situations[self.player.situation_nb - 1])
        for i in range(len(situation)):
            if situation[i][0] in self.player.information_revealed:
                situation[i][3] = "shown"
        return {
            'situation': situation,
        }

    def before_next_page(self):
        self.player.medical_treatment_additional_information = self.player.medical_treatment
        self.player.confidence_level_additional_information = self.player.confidence_level
        self.player.determine_confirmation_visibility()


class ConfirmationPage(Page):
    form_model = 'player'
    form_fields = ['medical_treatment_additional_information', 'confidence_level_additional_information']

    def is_displayed(self):
        return self.player.show_confirmation_page

    def vars_for_template(self):
        situation = copy.deepcopy(Constants.c_situations[self.player.situation_nb - 1])
        for i in range(len(situation)):
            if situation[i][0] in self.player.information_revealed:
                situation[i][3] = "shown"
        return {
            'situation': situation
        }

    def before_next_page(self):
        self.player.determine_treatment_changed()


#################################################
#################################################
class MerciPage(Page):
    def is_displayed(self):
        return self.subsession.round_number == Constants.num_rounds


#################################################
#################################################
page_sequence = [
    ExpertisePage,
    AcknowledgementPage,
    InstructionsPage,
    SituationPage,
    PropositionTherapeutiquePage,
    CriteresPage,
    DecisionPage,
    ConfirmationPage,
    MerciPage,
]
