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


#################################################
# Common
#################################################
class ConsignesPage(Page):
    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


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

    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


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

    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


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

    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


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

    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


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

    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


#################################################
# Treatment announcement
#################################################
class PersonalityResultsPage(Page):

    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


#################################################
# Questions page
#################################################
class Questions_Page(Page):
    form_model = 'player'
    form_fields = ['appreciate_picturaux', 'identify_to_picturaux',
                   'appreciate_experientiels', 'identify_to_experientiels']

    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


#################################################
# Matrices pages: fixed order, differentiated inside the template according to the treatment
#################################################
class Matrix1Page(Page):
    form_model = 'player'
    form_fields = ['matrix1_response']

    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


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

    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


# V1:  #################################################
# V1:  # Representations questions randomized order
# V1:  #################################################
# V1:  class RepresentationsGeneralPresentationPage(Page):
# V1:      timeout_seconds = Constants.c_timeout_inseconds
# V1:
# V1:      def before_next_page(self):
# V1:          # For automatic inactive pushing
# V1:          if self.timeout_happened is False:
# V1:              self.player.participant.vars['active_flag'] = time.time()
# V1:          # End-For automatic inactive pushing
# V1:
# V1:
# V1:  class RepresentationsPage1(Page):
# V1:      timeout_seconds = Constants.c_timeout_inseconds
# V1:      form_model = 'player'
# V1:      form_fields = ['idealgroup_nocheaf_iagree',
# V1:                     'idealgroup_nocheaf_mygroupagree',
# V1:                     'idealgroup_nocheaf_othergroupagree']
# V1:
# V1:      def before_next_page(self):
# V1:          # For automatic inactive pushing
# V1:          if self.timeout_happened is False:
# V1:              self.player.participant.vars['active_flag'] = time.time()
# V1:          # End-For automatic inactive pushing
# V1:
# V1:
# V1:  class RepresentationsPage2(Page):
# V1:      timeout_seconds = Constants.c_timeout_inseconds
# V1:      form_model = 'player'
# V1:      form_fields = ['idealgroup_sameenvironment_iagree',
# V1:                     'idealgroup_sameenvironment_mygroupagree',
# V1:                     'idealgroup_sameenvironment_othergroupagree']
# V1:
# V1:      def before_next_page(self):
# V1:          # For automatic inactive pushing
# V1:          if self.timeout_happened is False:
# V1:              self.player.participant.vars['active_flag'] = time.time()
# V1:          # End-For automatic inactive pushing
# V1:
# V1:
# V1:  class RepresentationsPage3(Page):
# V1:      timeout_seconds = Constants.c_timeout_inseconds
# V1:      form_model = 'player'
# V1:      form_fields = ['wastesorting_useful_iagree',
# V1:                     'wastesorting_useful_mygroupagree',
# V1:                     'wastesorting_useful_othergroupagree']
# V1:
# V1:      def before_next_page(self):
# V1:          # For automatic inactive pushing
# V1:          if self.timeout_happened is False:
# V1:              self.player.participant.vars['active_flag'] = time.time()
# V1:          # End-For automatic inactive pushing
# V1:
# V1:
# V1:  class RepresentationsPage4(Page):
# V1:      timeout_seconds = Constants.c_timeout_inseconds
# V1:      form_model = 'player'
# V1:      form_fields = ['wastesorting_clean_iagree',
# V1:                     'wastesorting_clean_mygroupagree',
# V1:                     'wastesorting_clean_othergroupagree']
# V1:
# V1:      def before_next_page(self):
# V1:          # For automatic inactive pushing
# V1:          if self.timeout_happened is False:
# V1:              self.player.participant.vars['active_flag'] = time.time()
# V1:          # End-For automatic inactive pushing
# V1:
# V1:
# V1:  class RepresentationsPage5(Page):
# V1:      timeout_seconds = Constants.c_timeout_inseconds
# V1:      form_model = 'player'
# V1:      form_fields = ['studies_knowledge_iagree',
# V1:                     'studies_knowledge_mygroupagree',
# V1:                     'studies_knowledge_othergroupagree']
# V1:
# V1:      def before_next_page(self):
# V1:          # For automatic inactive pushing
# V1:          if self.timeout_happened is False:
# V1:              self.player.participant.vars['active_flag'] = time.time()
# V1:          # End-For automatic inactive pushing
# V1:
# V1:
# V1:  class RepresentationsPage6(Page):
# V1:      timeout_seconds = Constants.c_timeout_inseconds
# V1:      form_model = 'player'
# V1:      form_fields = ['studies_culture_iagree',
# V1:                     'studies_culture_mygroupagree',
# V1:                     'studies_culture_othergroupagree']
# V1:
# V1:      def before_next_page(self):
# V1:          # For automatic inactive pushing
# V1:          if self.timeout_happened is False:
# V1:              self.player.participant.vars['active_flag'] = time.time()
# V1:          # End-For automatic inactive pushing
# V1:
# V1:
#################################################
# Representations questions randomized order
#################################################
class RepresentationsV2Page1(Page):
    form_model = 'player'

    def get_form_fields(self):
        questions_list = ["studies_require_work", "studies_require_investment", "studies_give_knowledge",
                          "studies_prepare_future", "studies_imply_exams", "studies_imply_diplomas",
                          "studies_access_culture", "studies_long_term", "studies_university"]
        random.shuffle(questions_list)
        return questions_list

    def error_message(self, values):
        answers = list(values.values())
        if (answers.count(Constants.c_representations_v2_choices[0]) != 3)\
                | (answers.count(Constants.c_representations_v2_choices[1]) != 3)\
                | (answers.count(Constants.c_representations_v2_choices[2]) != 3):
            return "Il faut sélectionner 3 " + "'" + Constants.c_representations_v2_choices[0] + "'" \
                   + ", 3 " + "'" + Constants.c_representations_v2_choices[1] + "'" \
                   + ", et 3 " + "'" + Constants.c_representations_v2_choices[2] + "'"

    def is_displayed(self):
        return self.session.config['representation_or_importation'] == "representation"


class RepresentationsV2Page2(Page):
    form_model = 'player'

    def get_form_fields(self):
        questions_list = ["ideal_group_no_cheaf", "ideal_group_positive_relations", "ideal_group_frequent_meetings",
                          "ideal_group_same_interests", "ideal_group_same_activities", "ideal_group_proximity",
                          "ideal_group_same_opinion", "ideal_group_same_background"]
        random.shuffle(questions_list)
        return questions_list

    def error_message(self, values):
        answers = list(values.values())
        if (answers.count(Constants.c_representations_v2_choices[0]) != 3) \
                | (answers.count(Constants.c_representations_v2_choices[1]) != 3) \
                | (answers.count(Constants.c_representations_v2_choices[2]) != 2):
            return "Il faut sélectionner 3 " + "'" + Constants.c_representations_v2_choices[0] + "'" \
                   + ", 3 " + "'" + Constants.c_representations_v2_choices[1] + "'" \
                   + ", et 2 " + "'" + Constants.c_representations_v2_choices[2] + "'"

    def is_displayed(self):
        return self.session.config['representation_or_importation'] == "representation"


class ImportationPage1(Page):
    form_model = 'player'
    form_fields = ['experientiels_studies_1', 'experientiels_studies_2',
                   'experientiels_studies_3', 'experientiels_studies_4',
                   'myself_studies_1', 'myself_studies_2',
                   'myself_studies_3', 'myself_studies_4'
                   ]

    def is_displayed(self):
        return self.session.config['representation_or_importation'] == "importation"

    def vars_for_template(self):
        return {
            'forward_or_reverse': random.choice(['forward', 'reverse'])
        }


class ImportationPage2(Page):
    form_model = 'player'
    form_fields = ['experientiels_ideal_friend_group_1', 'experientiels_ideal_friend_group_2',
                   'experientiels_ideal_friend_group_3', 'experientiels_ideal_friend_group_4',
                   'myself_ideal_friend_group_1', 'myself_ideal_friend_group_2',
                   'myself_ideal_friend_group_3', 'myself_ideal_friend_group_4'
                   ]

    def is_displayed(self):
        return self.session.config['representation_or_importation'] == "importation"

    def vars_for_template(self):
        return {
            'forward_or_reverse': random.choice(['forward', 'reverse'])
        }


#################################################
#################################################
class IdentificationPage(Page):
    form_model = 'player'
    form_fields = ['gender', 'age']

    def before_next_page(self):
        # For automatic inactive pushing
        if self.timeout_happened is False:
            self.player.participant.vars['active_flag'] = time.time()
        # End-For automatic inactive pushing


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


#################################################
# Build of page sequence
#################################################
#  start_pages = [
#      ConsignesPage,
#      Test1_Page,
#      Test2_Page,
#      Test3_Page,
#      Test4_Page,
#      Test5_Page,
#      PersonalityResultsPage,
#      Questions_Page,
#      Matrix1Page,
#      Matrix2Page,
#      RepresentationsGeneralPresentationPage
#  ]
#
#  end_pages = [
#      IdentificationPage,
#      MerciPage
#  ]
#
#  initial_page_sequence = [
#      RepresentationsPage1,
#      RepresentationsPage2,
#      RepresentationsPage3,
#      RepresentationsPage4,
#      RepresentationsPage5,
#      RepresentationsPage6,
#  ]

page_sequence = [
    ConsignesPage,
    Test1_Page,
    Test2_Page,
    Test3_Page,
    Test4_Page,
    Test5_Page,
    PersonalityResultsPage,
    Questions_Page,
    Matrix1Page,
    Matrix2Page,
    RepresentationsV2Page1,
    RepresentationsV2Page2,
    ImportationPage1,
    ImportationPage2,
    IdentificationPage,
    MerciPage
]


#  class MyPage(Page):
#      def inner_dispatch(self):
#          page_seq = int(self.__class__.__name__.split('_')[1])
#          page_to_show = json.loads(self.player.page_sequence)[page_seq]
#          self._is_frozen = False
#          self.__class__ = globals()[page_to_show]
#          return super(globals()[page_to_show], self).inner_dispatch()
#
#
#  for i, _ in enumerate(initial_page_sequence):
#      NewClassName = "Page_{}".format(i)
#      A = type(NewClassName, (MyPage,), {})
#      locals()[NewClassName] = A
#      page_sequence.append(locals()[NewClassName])
#
#  page_sequence = start_pages + page_sequence + end_pages
