"""
Adolescent Social Events
Romance, friendships, and social development (ages 10-18)

Events:
- firstCrush: First romantic crush
- firstKiss: First kiss
- dating_choice: Choosing someone to ask out
- romanticDate: Going on a romantic date
- newFriend: Making a new friend at school
"""

import random
from events.base import messageFunction, questionFunction, answerOption


def firstCrush(player, type='message', message=False, response=False):
    """First romantic crush"""
    fname = 'firstCrush'
    check = fname not in player.askedQuestions and player.c.ageYears >= 10 and player.c.ageYears < 13 and 1 >= random.random()*100
    message = "You have a crush on someone at school, who is it?"
    if (type != 'answer' and check):
        from functions import get_random_classmate, get_random_friend
        answerOptions = []
        # choose / create up to 5 options
        option = get_random_friend(player)
        if (option):
            answerOptions.append(answerOption('Your '+option.relationships[0]+', '+option.firstname+', a '+str(option.ageYears)+' year old '+option.sex,option.id))
        added_ids = set()
        for i in range(4):
            while True: # this ensures that we don't get the same person twice
                option = get_random_classmate(player)
                if option.id not in added_ids:
                    break
            added_ids.add(option.id)
            answerOptions.append(answerOption('Your '+option.relationships[0]+', '+option.firstname+', a '+str(option.ageYears)+' year old '+option.sex,option.id))
        return questionFunction(fname,message,player,check,answerOptions)
    if (type == 'answer'):
        player.c.firstCrush = response['data']
        from functions import get_person
        crush = get_person(player,response['data'])
        if (crush.sex == player.c.sex):
            player.c.sexualOrientation = 'homosexual'
        else:
            player.c.sexualOrientation = 'heterosexual'
        from app import update_relationship
        update_relationship(player,'Crush','Crush','Crush')


def firstKiss(player, type='message', message=False, response=False):
    """First kiss"""
    fname = 'firstKiss'
    check = fname not in player.askedQuestions and player.c.ageYears >= 13 and 1 >= random.random()*1000
    message = "Someone leans in for a kiss, how do you want to respond?"
    answerOptions = [answerOption('Lean in'),answerOption('Slowly back away while their eyes are closed'),answerOption('Let them down easy',diamondCost=5)]
    if (type != 'answer'):
        return questionFunction(fname,message,player,check, answerOptions)
    elif (type == 'answer'):
        if (response['option'] == answerOptions[0]):
            player.messageQueue.append("Your heart starts to beat fast and you can feel them inching closer. You squeeze your eyes shut and your lips touch. That wasn't so bad!")
        elif (response ['option'] == answerOptions[1]):
            player.messageQueue.append("Their eyes flutter open and you quickly lean to the left and dodge their puckering lips coming closer to you. Phew! Seems like you dodged a bullet. You leave them standing there feeling dejected.")
        elif (response ['option'] == answerOptions[2]):
            player.messageQueue.append('You tell them you appreciate their gesture but you do not feel the same way and wish them best of luck in their endeavors. They respond positively and the two of you carry on as if nothing happened.')


def dating_choice(player, type='message', message=False, response=False):
    """Choosing someone to ask out"""
    fname = 'dating_choice'
    from functions import find_where_test
    check = fname not in player.askedQuestions and not player.c.relationship and len(list(find_where_test(player.r,{'affinity__gt':50}))) > 0 and player.c.ageYears > 13 and 1 >= random.random()*100
    message = "Who would you like to ask out?"
    if (type != 'answer' and check):
        options = list(find_where_test(player.r,{'affinity__gt':50}))
        answerOptions = []
        for option in options:
            if abs(player.c.ageYears - option.ageYears) <= 5 and option.relationships[0] and option.familyLevel == 0 and option.sex != player.c.sex:
                answerOptions.append(answerOption("Your "+option.relationships[0]+', '+ option.firstname + ' ' + option.lastname,option.id))
        if len(answerOptions) > 0:
            return questionFunction(fname,message,player,check,answerOptions)
        else:
            player.askedQuestions.add(fname)
            player.messageQueue.append("No one likes you enough to date you!")
    elif (type == 'answer'):
        from functions import get_person
        #success chance based on affinity
        p = get_person(player,response['data'])
        affinityRatio = p.affinity/100
        if (random.random() < affinityRatio):
            player.c.partner = response['data']
            from app import update_relationship
            update_relationship(player,'Partner','Partner','Partner')
            player.messageQueue.append(p.firstname+" said yes!")
        else:
            player.messageQueue.append("They said no!")


def romanticDate(player, type='message', message=False, response=False):
    """Going on a romantic date"""
    fname = 'romanticDate'
    check = fname not in player.askedQuestions and player.c.ageYears >= 16 and player.c.ageYears <= 18 and 1 >=random.random() * 100 #Craig - need a hand for checking if the user has a significant other
    message = "Love is in the air! Its your turn to pick date night. What would you like to do?"
    answerOptions = [
        answerOption('Have a candlelit dinner at a fancy restaurant', moneyCost=50),
        answerOption('Go for a scenic walk in the park', energyCost=10),
        answerOption('Watch a movie under the stars at a drive-in theater', moneyCost=30, energyCost=5)
    ]
    if (type != 'answer'):
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        if (response['option'] == answerOptions[0]):
            player.messageQueue.append("You had a magical candlelit dinner at a fancy restaurant. It was a night to remember!")
            #ideally outcomes of this event can effect romantic relationship. Not sure if framework is in place for this yet.
        elif (response['option'] == answerOptions[1]):
            player.messageQueue.append("You enjoyed a long walk in the park, hand in hand, as the sun set. It was beautiful (but you're both a little tired!)")
        elif (response['option'] == answerOptions[2]):
            player.messageQueue.append("You cuddled up in the car, watching a movie under the starry sky at the drive-in theater. It was a perfect date night!")


def newFriend(player, type='message', message=False, response=False):
    """Making a new friend at school"""
    fname = 'newFriend'
    check = player.c.location == 'school' and player.c.occupation == 'student' and player.c.ageYears >= 4 and player.c.ageYears < 22 and 1 >= random.random()*1000
    from functions import get_random_classmate
    classmate = get_random_classmate(player)
    message = "You have met "+classmate.firstname+" "+classmate.lastname+" at school, would you like to be friends?"
    answerOptions = ['Yes','No']
    if (type != 'answer'):
        return questionFunction(fname,message,player,check,answerOptions)
    elif(type == 'answer'):
        if (response['option'] == answerOptions[0]):
            from app import add_friend
            player = add_friend(player,player.c.sex,'similar')
            player.messageQueue.append("You have made a new friend!")
        else:
            player.social -= 10
            player.messageQueue.append("You have not made a new friend.")


__all__ = [
    'firstCrush',
    'firstKiss',
    'dating_choice',
    'romanticDate',
    'newFriend',
]
