"""
Family and Parenthood Events
Having children and family life (ages 18-50)

Events:
- haveChild: Deciding to have a child
- pregnant: Pregnancy announcement
- childBorn: Child birth
"""

import random
from events.base import messageFunction, questionFunction


def haveChild(player, type='message', message=False, response=False):
    """Deciding to have a child"""
    fname = 'haveChild'
    check = fname not in player.askedQuestions and player.c.ageYears > 18 and player.c.ageYears < 50 and 1 >= random.random()*1000
    if (type != 'answer' and check):
        if (player.c.partner):
            from functions import get_person
            partner = get_person(player,player.c.partner)
            if (partner.affinity > 80):
                message = "Would you like to have a child with "+partner.firstname+" "+partner.lastname+"?"
                return questionFunction(fname,message,player,check)
    elif (type == 'answer'):
        if (response['option'] == "Yes"):
            player.c.tryingForChild = True
            player.messageQueue.append("You are now trying to have a child!")
        else:
            player.c.tryingForChild = False
            player.messageQueue.append("You decide not to have a child right now.")


def pregnant(player, type='message', message=False, response=False):
    """Pregnancy announcement"""
    fname = 'pregnant'
    check = fname not in player.askedQuestions and player.c.tryingForChild == True and player.c.ageYears > 18 and player.c.ageYears < 50 and 1 >= random.random()*1000
    if (type != 'answer' and check):
        if (player.c.partner):
            from functions import get_person
            partner = get_person(player,player.c.partner)
            if (partner.affinity > 80):
                message = "You are pregnant!"
                return messageFunction(fname,message,player,check)
    elif (type == 'answer'):
        if (response['option'] == "Yes"):
            player.c.pregnant = True
            player.c.tryingForChild = False
            #create schedule for pregnancy
            from functions import scheduler
            player.c.schedules.append(scheduler(player.c,"Pregnancy",["daily"],location="home"+player.c.id,duration=280))
            player.messageQueue.append("You are now pregnant!")


def childBorn(player, type='message', message=False, response=False):
    """Child birth"""
    fname = 'childBorn'
    from functions import scheduleComplete
    check = getattr(player.c, 'pregnant', False) and scheduleComplete(player.c,'Pregnancy')
    if (type != 'answer' and check):
        #show message for child and create child character
        from functions import add_child
        child = add_child(player)
        player.c.pregnant = False
        player.c.tryingForChild = False
        player.c.children = True
        player.messageQueue.append("You have a child! Their name is "+child.firstname+" "+child.lastname)
        return messageFunction(fname,message,player,check)


__all__ = [
    'haveChild',
    'pregnant',
    'childBorn',
]
