"""
Childhood Setback Events
Negative events and minor disappointments for young children (ages 2-18)

Events:
- lostFavoriteToy: Lost favorite toy causing sadness
- notInvitedToParty: Feeling excluded from a birthday party
- scolded: Getting in trouble with parents
- lostGame: Losing a game and playing poorly
- friendMovedAway: Best friend moving to another city
"""

import random
from events.base import messageFunction


def lostFavoriteToy(player, type='message'):
    """Lost favorite toy causing sadness"""
    fname = 'lostFavoriteToy'
    check = fname not in player.events and player.c.ageYears >= 2 and player.c.ageYears <= 8 and 1 >= random.random()*3000
    message = "You lost your favorite toy and can't find it anywhere. You're heartbroken."

    if check:
        player.events.add(fname)
        player.c.happiness -= 20
        player.c.energy -= 10

    return messageFunction(fname, message, player, check)


def notInvitedToParty(player, type='message'):
    """Feeling excluded from a birthday party"""
    fname = 'notInvitedToParty'
    check = fname not in player.events and player.c.ageYears >= 5 and player.c.ageYears <= 12 and 1 >= random.random()*4000
    message = "Everyone in your class is talking about a birthday party you weren't invited to. You feel left out."

    if check:
        player.events.add(fname)
        player.c.happiness -= 15
        player.c.social -= 10

    return messageFunction(fname, message, player, check)


def scolded(player, type='message'):
    """Getting in trouble with parents"""
    fname = 'scolded'
    check = fname not in player.events and player.c.ageYears >= 3 and player.c.ageYears <= 12 and 1 >= random.random()*2500
    message = "You got in trouble and your parents scolded you. You feel bad."

    if check:
        player.events.add(fname)
        player.c.happiness -= 15

        # Decrease affinity with parents if they exist
        for person in player.r:
            if person.title in ['Mother', 'Father', 'Parent']:
                person.affinity -= 10

    return messageFunction(fname, message, player, check)


def lostGame(player, type='message'):
    """Losing a game and playing poorly"""
    fname = 'lostGame'
    check = fname not in player.events and player.c.ageYears >= 5 and player.c.ageYears <= 15 and 1 >= random.random()*3500
    message = "Your team lost the game and you played poorly. You're disappointed in yourself."

    if check:
        player.events.add(fname)
        player.c.happiness -= 15
        player.c.energy -= 10

    return messageFunction(fname, message, player, check)


def friendMovedAway(player, type='message'):
    """Best friend moving to another city"""
    fname = 'friendMovedAway'
    check = fname not in player.events and player.c.ageYears >= 5 and player.c.ageYears <= 18 and 1 >= random.random()*8000
    message = "Your best friend is moving to another city. You're sad you won't see them anymore."

    if check:
        player.events.add(fname)
        player.c.happiness -= 25
        player.c.social -= 15

        # Decrease affinity with a random friend if they exist
        friends = [person for person in player.r if person.title == 'Friend']
        if friends:
            friend = random.choice(friends)
            friend.affinity -= 30

    return messageFunction(fname, message, player, check)


__all__ = [
    'lostFavoriteToy',
    'notInvitedToParty',
    'scolded',
    'lostGame',
    'friendMovedAway',
]
