from otree.api import Currency as c, currency_range, models
from ._builtin import Page, WaitPage
from . import models
from .models import Constants
from otreeutils.surveys import SurveyPage, setup_survey_pages
from collections import Counter


class WelcomePage(Page):
	def is_displayed(self):
		return self.round_number == 1
	form_model = 'player'
	form_fields = ['college']

class Instructions1(Page):
	def is_displayed(self):
		return self.round_number == 1
	pass

class Instructions2(Page):
	def is_displayed(self):
		return self.round_number == 1
	pass

class Instructions3(Page):
	def is_displayed(self):
		return self.round_number == 1
	pass

class ShuffleWaitPage(WaitPage):
	wait_for_all_groups = True
	def after_all_players_arrive(self):
		if self.round_number == 1:
			self.subsession.set_player_endowment()
			self.subsession.sync_team_by_college()
			self.subsession.group_players()
		else:
			self.subsession.carry_over_data()
			self.subsession.sync_team_by_previous()
			self.subsession.group_players_new()

		groups = self.subsession.get_groups()

		for idx, g in enumerate(groups):
			g.team_size = len(g.get_players())
			print('There are ', len(g.get_players()), ' player(s) in group ', idx+1)

		matrix = self.subsession.get_group_matrix()
		for row in matrix:
			print(row)

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

class ResultsWaitPage(WaitPage):
	wait_for_all_groups = True
	def after_all_players_arrive(self):
		group = self.subsession.get_groups()
		for g in group:
			players = g.get_players()
			contributions = [c(p.contribution) for p in players]
			g.total_contribution = sum(contributions)
			g.individual_share = c(g.total_contribution) * Constants.multiplier[len(players)-1] / len(players)
			for p in players:
				p.payout = c(p.endowment) - c(p.contribution) + c(g.individual_share)


class Results(Page):
	pass

class SwitchingDecision(Page):
	def is_displayed(self):
		for x in Constants.switching_rounds:
			if x == self.round_number:
				return True
		return False

	form_model = 'player'
	form_fields = ['desired_team']

class SwitchingWaitPage(WaitPage):
	def is_displayed(self):
		for x in Constants.switching_rounds:
			if x == self.round_number:
				return True
		return False
	wait_for_all_groups = True

class SwitchingVoting(Page):
	def is_displayed(self):
		if (len(self.group.get_players()) >= Constants.max_players_in_team):
			return False
		else:
			for x in Constants.switching_rounds:
				if x == self.round_number:
					return True
			return False

	form_model = 'player'
	form_fields = ['voted_for_players']
	def error_message(self, values):
		print('values is', values)

class SwitchingGenerateResults(WaitPage):
	def is_displayed(self):
		for x in Constants.switching_rounds:
			if x == self.round_number:
				return True
		return False
	wait_for_all_groups = True
	def after_all_players_arrive(self):
		self.subsession.count_votes()
		self.subsession.switch_teams()

class SwitchingResults(Page):
	def is_displayed(self):
		for x in Constants.switching_rounds:
			if x == self.round_number:
				return True
		return False

page_sequence = [
    WelcomePage,
	Instructions1,
	Instructions2,
	Instructions3,
	ShuffleWaitPage,
    Contribute,
    ResultsWaitPage,
	Results,
	SwitchingDecision,
	SwitchingWaitPage,
	SwitchingVoting,
	SwitchingGenerateResults,
	SwitchingResults,
]
