from otree.api import *



doc = """
One player decides how to divide a certain amount between himself and the other
player.
See: Kahneman, Daniel, Jack L. Knetsch, and Richard H. Thaler. "Fairness
and the assumptions of economics." Journal of business (1986):
S285-S300.
"""


class C(BaseConstants):
    NAME_IN_URL = 'dictator'
    PLAYERS_PER_GROUP = 2
    NUM_ROUNDS = 10
    # Initial amount allocated to the dictator
    ENDOWMENT = cu(100)


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    kept = models.CurrencyField(
        doc="""Amount dictator decided to keep for himself""",
        min=0,
        max=C.ENDOWMENT,
        label="I will keep",
    )


class Player(BasePlayer):
    pass


# FUNCTIONS
def set_payoffs(group: Group):
    p1 = group.get_player_by_id(1)
    p2 = group.get_player_by_id(2)
    p1.payoff = group.kept
    p2.payoff = C.ENDOWMENT - group.kept
    p1.participant.vars['earning_1'] = p1.payoff
    p2.participant.vars['earning_1'] = p2.payoff

def get_players(player: Player):
    participant_name = player.participant
    return dict(participant_name=participant_name)


def vars_for_admin_report(subsession):
    all_offers = []
    for subsession in subsession.in_all_rounds():
        for group in subsession.get_groups():
            # Safely retrieve the value of group.kept
            kept_value = group.field_maybe_none('kept') or 0
            offer = C.ENDOWMENT - kept_value
            all_offers.append(offer)
        payoffs = sorted([p.payoff for p in subsession.get_players()])
    return dict(payoffs=payoffs, all_offers=all_offers)



def js_vars(player: Player):
    group = player.group
    return dict(
        taken=group.kept,
    )


# PAGES
class Introduction(Page):
    pass


class Offer(Page):
    form_model = 'group'
    form_fields = ['kept']

    @staticmethod
    def is_displayed(player: Player):
        return player.id_in_group == 1


class ResultsWaitPage(WaitPage):
    after_all_players_arrive = set_payoffs


class Results(Page):
    @staticmethod
    def vars_for_template(player: Player):
        group = player.group

        return dict(offer=C.ENDOWMENT - group.kept)

    @staticmethod
    def js_vars(player: Player):
        group = player.group
        return dict(
            taken=group.kept,
        )

    @staticmethod
    def vars_for_admin_report(subsession):
        payoffs = sorted([p.payoff for p in subsession.get_players()])
        return dict(payoffs=payoffs)


page_sequence = [
    Introduction,
    Offer,
    ResultsWaitPage,
    Results
]
