"""
Education Events - Quick Wins
Additional school and college events from EVENT_IDEAS_QUICK_WINS.md section 3

Events:
- popQuiz: Unexpected quiz when unprepared (questionEvent)
- raisedHandNotCalled: Knowing the answer but not getting called on (messageEvent)
- lostHomework: Can't find completed homework (questionEvent)
- presentationNerves: Giving a presentation in front of the class (questionEvent)
- teacherFavorite: Becoming the teacher's favorite (messageEvent)
- studyGroupInvite: Invited to join a study group (questionEvent)
- extracurricularBurnout: Overwhelmed by too many activities (questionEvent)
- cafeteriaFoodPoisoning: Getting sick from school lunch (messageEvent)
- substituteTeacher: Having a substitute teacher (messageEvent)
- collegeAllNighter: Pulling an all-nighter to study (questionEvent)
"""

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


def popQuiz(player, type='message', message=False, response=False):
    """Unexpected quiz when unprepared"""
    fname = 'popQuiz'
    check = (fname not in player.askedQuestions and
             'school' in player.c.location and
             player.c.ageYears >= 10 and
             player.c.ageYears <= 18 and
             1 >= random.random() * 200)
    message = "The teacher announces a surprise pop quiz! You didn't study. What do you do?"
    answerOptions = [
        answerOption('Try your best', energyCost=10),
        answerOption("Glance at neighbor's paper"),
        answerOption('Fake being sick'),
        answerOption('Stay calm and think', diamondCost=5)
    ]
    if (type != 'answer'):
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        if (response['option'] == answerOptions[0].option):
            player.messageQueue.append("You focus hard and try your best. You might not ace it, but at least you gave it an honest effort!")
            player.c.happiness += 5
        elif (response['option'] == answerOptions[1].option):
            player.messageQueue.append("You sneak a peek at your neighbor's paper. The teacher catches your eye and you quickly look away. You feel guilty and anxious for the rest of class.")
            player.c.social -= 5
            player.c.happiness -= 10
        elif (response['option'] == answerOptions[2].option):
            player.messageQueue.append("You clutch your stomach and ask to go to the nurse. The teacher looks skeptical but lets you go. Now you have to make up the quiz later...")
            player.c.happiness -= 10
        elif (response['option'] == answerOptions[3].option):
            player.messageQueue.append("You take a deep breath and trust your instincts. You're surprised by how much you actually remember! Sometimes staying calm is the best strategy.")
            player.c.happiness += 5


def raisedHandNotCalled(player, type='message', message=False, response=False):
    """Knowing the answer but not getting called on"""
    fname = 'raisedHandNotCalled'
    check = (fname not in player.events and
             'school' in player.c.location and
             player.c.ageYears >= 8 and
             player.c.ageYears <= 18 and
             1 >= random.random() * 300)
    message = "You know the answer! Your hand shoots up, but the teacher calls on someone else who gets it wrong. So frustrating."
    return messageFunction(fname, message, player, check)


def lostHomework(player, type='message', message=False, response=False):
    """Can't find completed homework"""
    fname = 'lostHomework'
    check = (fname not in player.askedQuestions and
             ('school' in player.c.location or 'home' in player.c.location) and
             player.c.ageYears >= 8 and
             player.c.ageYears <= 16 and
             1 >= random.random() * 250)
    message = "You finished your homework but can't find it anywhere! What do you do?"
    answerOptions = [
        answerOption('Redo it quickly before class', energyCost=20),
        answerOption('Explain to the teacher'),
        answerOption('Say your dog ate it'),
        answerOption('Take the late grade')
    ]
    if (type != 'answer'):
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        if (response['option'] == answerOptions[0].option):
            player.messageQueue.append("You frantically rush to redo your homework. It's exhausting and stressful, but you manage to turn in something before class starts.")
            player.c.happiness -= 5
        elif (response['option'] == answerOptions[1].option):
            player.messageQueue.append("You approach the teacher and explain what happened. They seem understanding and give you until tomorrow to find it or redo it. What a relief!")
            player.c.social += 5
            player.c.happiness += 5
        elif (response['option'] == answerOptions[2].option):
            player.messageQueue.append("You tell the teacher your dog ate your homework. They raise an eyebrow and clearly don't believe you. 'That's the oldest excuse in the book,' they say with a sigh.")
            player.c.social -= 10
            player.c.happiness += 5
        elif (response['option'] == answerOptions[3].option):
            player.messageQueue.append("You decide it's not worth the stress and just take the late grade. It stings a bit, but at least you don't have to panic about it anymore.")
            player.c.happiness -= 10


def presentationNerves(player, type='message', message=False, response=False):
    """Giving a presentation in front of the class"""
    fname = 'presentationNerves'
    check = (fname not in player.askedQuestions and
             'school' in player.c.location and
             player.c.ageYears >= 10 and
             player.c.ageYears <= 18 and
             1 >= random.random() * 200)
    message = "It's your turn to present to the class. How do you handle your nerves?"
    answerOptions = [
        answerOption('Take deep breaths and go', energyCost=10),
        answerOption('Rush through it quickly', energyCost=5),
        answerOption('Picture everyone in underwear'),
        answerOption('Ask to go last', energyCost=15, diamondCost=3)
    ]
    if (type != 'answer'):
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        if (response['option'] == answerOptions[0].option):
            player.messageQueue.append("You take some deep breaths, walk to the front, and deliver your presentation. Your voice shakes a little, but you make it through! You feel proud of yourself.")
            player.c.social += 5
            player.c.happiness += 10
        elif (response['option'] == answerOptions[1].option):
            player.messageQueue.append("You speed through your presentation as fast as possible. The teacher asks you to slow down, but it's too late - you're already done. At least it's over!")
            player.c.social -= 5
        elif (response['option'] == answerOptions[2].option):
            player.messageQueue.append("You try the old 'picture them in underwear' trick. It makes you giggle nervously, which weirdly helps you relax. Your presentation goes okay!")
            player.c.happiness += 5
        elif (response['option'] == answerOptions[3].option):
            player.messageQueue.append("You ask the teacher if you can go last. They agree, which gives you more time to prepare. By the time it's your turn, you're actually feeling pretty confident!")
            player.c.happiness += 5


def teacherFavorite(player, type='message', message=False, response=False):
    """Becoming the teacher's favorite"""
    fname = 'teacherFavorite'
    check = (fname not in player.events and
             'school' in player.c.location and
             player.c.ageYears >= 8 and
             player.c.ageYears <= 17 and
             1 >= random.random() * 300)
    message = "Your teacher keeps calling on you and praising your work. Some classmates whisper that you're the 'teacher's pet.'"
    return messageFunction(fname, message, player, check)


def studyGroupInvite(player, type='message', message=False, response=False):
    """Invited to join a study group"""
    fname = 'studyGroupInvite'
    check = (fname not in player.askedQuestions and
             'school' in player.c.location and
             player.c.ageYears >= 14 and
             player.c.ageYears <= 22 and
             1 >= random.random() * 200)
    message = "Some classmates invite you to their study group. Do you join?"
    answerOptions = [
        answerOption('Yes, study together', energyCost=10),
        answerOption('No, prefer solo studying'),
        answerOption('Yes, but mostly socialize', energyCost=5)
    ]
    if (type != 'answer'):
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        if (response['option'] == answerOptions[0].option):
            player.messageQueue.append("You join the study group and actually get a lot of work done! It's nice to have people to bounce ideas off of. Plus, you made some new study buddies.")
            player.c.social += 10
            player.c.happiness += 5
        elif (response['option'] == answerOptions[1].option):
            player.messageQueue.append("You politely decline and explain that you study better alone. They seem a little disappointed, but they understand. You stick to your solo study routine.")
            player.c.social -= 5
        elif (response['option'] == answerOptions[2].option):
            player.messageQueue.append("You show up to the study group, but honestly, you spend most of the time chatting and laughing with everyone. Not much studying gets done, but it's a fun time!")
            player.c.social += 15
            player.c.happiness += 10


def extracurricularBurnout(player, type='message', message=False, response=False):
    """Overwhelmed by too many activities"""
    fname = 'extracurricularBurnout'
    check = (fname not in player.askedQuestions and
             player.c.ageYears >= 13 and
             player.c.ageYears <= 18 and
             len(player.c.activities) > 2 and
             1 >= random.random() * 150)
    message = "You're juggling school, activities, and friends. You're exhausted. What gives?"
    answerOptions = [
        answerOption('Quit an activity'),
        answerOption('Power through it', energyCost=20),
        answerOption('Talk to parents about it', diamondCost=5),
        answerOption('Drop the lowest priority')
    ]
    if (type != 'answer'):
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        if (response['option'] == answerOptions[0].option):
            player.messageQueue.append("You decide to quit one of your activities. It's a tough decision, but you immediately feel lighter and less stressed. Sometimes less is more.")
            player.c.happiness += 10
            player.c.social -= 5
            # Remove one activity
            if len(player.c.activities) > 0:
                removed_activity = player.c.activities.pop()
                player.messageQueue.append(f"You quit {removed_activity}.")
        elif (response['option'] == answerOptions[1].option):
            player.messageQueue.append("You tell yourself you can handle it and push through. The stress continues to pile up, and you're running on fumes. This isn't sustainable...")
            player.c.happiness -= 10
        elif (response['option'] == answerOptions[2].option):
            player.messageQueue.append("You sit down with your parents and explain how overwhelmed you feel. They help you reorganize your schedule and set better boundaries. You feel much better!")
            player.c.energy += 10
            player.c.happiness += 15
        elif (response['option'] == answerOptions[3].option):
            player.messageQueue.append("You identify your lowest priority activity and scale it back. It's a good compromise - you're still involved but not drowning in commitments.")
            player.c.happiness += 5


def cafeteriaFoodPoisoning(player, type='message', message=False, response=False):
    """Getting sick from school lunch"""
    fname = 'cafeteriaFoodPoisoning'
    check = (fname not in player.events and
             'school' in player.c.location and
             player.c.ageYears >= 8 and
             player.c.ageYears <= 18 and
             1 >= random.random() * 400)
    message = "That mystery meat from the cafeteria is not sitting well. You spend most of the afternoon in the bathroom."
    return messageFunction(fname, message, player, check, energyCost=15)


def substituteTeacher(player, type='message', message=False, response=False):
    """Having a substitute teacher"""
    fname = 'substituteTeacher'
    check = (fname not in player.events and
             'school' in player.c.location and
             player.c.ageYears >= 8 and
             player.c.ageYears <= 18 and
             1 >= random.random() * 300)
    message = "You have a substitute teacher today. The class is chaotic and you barely get any work done."
    return messageFunction(fname, message, player, check)


def collegeAllNighter(player, type='message', message=False, response=False):
    """Pulling an all-nighter to study"""
    fname = 'collegeAllNighter'
    check = (fname not in player.askedQuestions and
             player.c.ageYears >= 18 and
             player.c.ageYears <= 22 and
             player.c.education == 'college' and
             1 >= random.random() * 150)
    message = "You have a huge exam tomorrow and you're behind. Pull an all-nighter?"
    answerOptions = [
        answerOption('Yes, study all night', energyCost=30),
        answerOption('No, get sleep instead'),
        answerOption('Study until midnight', energyCost=15),
        answerOption('Get help from study buddy', diamondCost=10)
    ]
    if (type != 'answer'):
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        if (response['option'] == answerOptions[0].option):
            player.messageQueue.append("You chug coffee and energy drinks all night, cramming as much information as possible. By morning you're exhausted and jittery, but you know the material!")
            player.c.happiness -= 10
        elif (response['option'] == answerOptions[1].option):
            player.messageQueue.append("You decide sleep is more important than cramming. You get a full night's rest and wake up refreshed. You might not know everything, but at least your brain is functioning!")
            player.c.happiness += 5
            player.c.energy += 10
        elif (response['option'] == answerOptions[2].option):
            player.messageQueue.append("You study hard until midnight, then force yourself to get some sleep. It's a good balance - you reviewed the key concepts but still got some rest.")
            player.c.happiness += 5
        elif (response['option'] == answerOptions[3].option):
            player.messageQueue.append("You call up a classmate who's also studying. You quiz each other and help fill in gaps in your knowledge. Studying with someone else makes it way more effective!")
            player.c.social += 10
            player.c.happiness += 10


__all__ = [
    'popQuiz',
    'raisedHandNotCalled',
    'lostHomework',
    'presentationNerves',
    'teacherFavorite',
    'studyGroupInvite',
    'extracurricularBurnout',
    'cafeteriaFoodPoisoning',
    'substituteTeacher',
    'collegeAllNighter',
]
