"""
Negative Random Events
Unfortunate events and mishaps

Events:
- carCrash: Car accident with a deer
- oneTimeEventTest: Test event
- lowAffinity: Low affinity warning
- spilledDrink: Spilling drink on yourself
- phoneDropped: Dropping and potentially breaking phone
- birdPooped: Bird poops on you
- forgotWallet: Realizing you left wallet at home
- missedBus: Just missed the bus/train
- badHairDay: Having a terrible hair day
- caughtInRain: Getting caught in rain without umbrella
- lostKeys: Can't find keys anywhere
- ruinedClothes: Ruined favorite outfit
- badRestaurantExperience: Terrible restaurant service and food
- sleepThroughAlarm: Sleeping through alarm and being late
- packageStolen: Package stolen from doorstep
- phoneBreakdown: Phone completely died, need new one
- dataLoss: Computer crashed and lost important files
- socialMediaCanceled: Being 'cancelled' on social media
- cyberbullying: Being cyberbullied online
- accountHacked: Accounts hacked with embarrassing posts
"""

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


def carCrash(player, type='message', message=False, response=False):
    """Car accident with a deer"""
    fname = 'carCrash'
    check = fname not in player.askedQuestions and player.c.canDrive and 1 >= random.random()*100000
    message = "You're driving at night and a deer jumps in front of your car."
    answerOptions = [answerOption('Swerve left',diamondCost=5), answerOption('Hit the brakes',moneyCost=50), answerOption('Speed up',energyCost=5)]
    if (type != 'answer'):
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        if (response ['option'] == answerOptions[0]):
            player.messageQueue.append("Its a quiet night and no other cars are on the road. You swerve around the deer without a scratch!")
        elif (response ['option'] == answerOptions[1]):
            player.c.happiness -= 5
            player.messageQueue.append("You slam the breaks but still hit the deer. You (and the deer) are safe, but the windshield will need some costly repairs!")
        elif (response ['option'] == answerOptions[2]):
            player.c.happiness -= 10
            player.messageQueue.append("...Why would you speed up? You hit the deer at full speed and spent the night in the hospital.")


def oneTimeEventTest(player, type='message', message=False, response=False):
    """Test event"""
    fname = 'oneTimeEventTest'
    check = fname not in player.events and player.c.ageDays > 0
    if (type != 'answer' and check):
        from functions import oneTimeEvent
        player.events.add(fname)
        player.c.oneTimeEvents.append(oneTimeEvent(title="Oct 10th",message="Are you today's date? Because you're 10/10!!",date="10-10"))


def lowAffinity(player, type='message'):
    """Low affinity warning"""
    for person in player.r:
        check = False
        fname = 'lowAffinity'
        fname = fname+person.firstname+person.lastname
        if person.affinity < -50 and fname not in player.events:
            check = True
            message = 'Your '+person.title+' '+person.firstname+' '+person.lastname+' deeply dislikes you after years of neglect.'
            return messageFunction(fname,message,player,check)


def spilledDrink(player, type='message'):
    """Spilling drink on yourself"""
    fname = 'spilledDrink'
    check = fname not in player.events and player.c.ageYears >= 8 and player.c.ageYears <= 100 and 1 >= random.random()*5000
    message = "You spilled coffee/juice all over yourself. Great start to the day..."

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

    return messageFunction(fname, message, player, check)


def phoneDropped(player, type='message', message=False, response=False):
    """Dropping and potentially breaking phone"""
    fname = 'phoneDropped'
    check = fname not in player.askedQuestions and player.c.ageYears >= 12 and player.c.ageYears <= 100 and 1 >= random.random()*10000
    message = "You dropped your phone! Did it survive?"

    if (type != 'answer'):
        # Random outcomes with different probabilities
        answerOptions = ["It's fine!", 'Screen cracked', 'Completely shattered']
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        # Randomly determine the outcome
        outcome = random.choice([0, 0, 0, 1, 1, 2])  # Weighted: 3/6 fine, 2/6 cracked, 1/6 shattered

        if outcome == 0:  # It's fine
            player.messageQueue.append("Phew! Your phone is perfectly fine. That was a close one!")
            player.c.happiness += 5
        elif outcome == 1:  # Screen cracked
            player.messageQueue.append("Oh no! The screen is cracked. You'll need to get it repaired.")
            player.c.happiness -= 15
            player.c.money -= 200
        else:  # Completely shattered
            player.messageQueue.append("The phone is completely shattered. You'll need to buy a new one.")
            player.c.happiness -= 30
            player.c.money -= 500


def birdPooped(player, type='message'):
    """Bird poops on you"""
    fname = 'birdPooped'
    check = fname not in player.events and player.c.ageYears >= 5 and player.c.ageYears <= 100 and 1 >= random.random()*8000
    message = "A bird pooped on you. People say it's good luck, but it just feels gross."

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

    return messageFunction(fname, message, player, check)


def forgotWallet(player, type='message'):
    """Realizing you left wallet at home"""
    fname = 'forgotWallet'
    check = fname not in player.events and player.c.ageYears >= 16 and player.c.ageYears <= 100 and 1 >= random.random()*6000
    message = "You're at the checkout and realize you forgot your wallet. So embarrassing!"

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

    return messageFunction(fname, message, player, check)


def missedBus(player, type='message'):
    """Just missed the bus/train"""
    fname = 'missedBus'
    check = fname not in player.events and player.c.ageYears >= 10 and player.c.ageYears <= 100 and 1 >= random.random()*7000
    message = "You ran for the bus but it pulled away right as you got there. Now you have to wait 20 minutes."

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

    return messageFunction(fname, message, player, check)


def badHairDay(player, type='message'):
    """Having a terrible hair day"""
    fname = 'badHairDay'
    check = fname not in player.events and player.c.ageYears >= 12 and player.c.ageYears <= 40 and 1 >= random.random()*400
    message = "Your hair looks terrible today and there's nothing you can do about it."

    if check:
        player.events.add(fname)
        player.c.happiness -= 5
        # Note: confidence stat not yet implemented in personClass

    return messageFunction(fname, message, player, check)


def caughtInRain(player, type='message'):
    """Getting caught in rain without umbrella"""
    fname = 'caughtInRain'
    check = fname not in player.events and player.c.ageYears >= 5 and player.c.ageYears <= 100 and 1 >= random.random()*500
    message = "You got caught in a downpour without an umbrella. You're soaked."

    if check:
        player.events.add(fname)
        player.c.happiness -= 10
        player.c.health -= 5

    return messageFunction(fname, message, player, check)


def lostKeys(player, type='message'):
    """Can't find keys anywhere"""
    fname = 'lostKeys'
    check = fname not in player.events and player.c.ageYears >= 12 and player.c.ageYears <= 100 and 1 >= random.random()*450
    message = "You can't find your keys anywhere. You're going to be late."

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

    return messageFunction(fname, message, player, check)


def ruinedClothes(player, type='message'):
    """Ruined favorite outfit"""
    fname = 'ruinedClothes'
    check = fname not in player.events and player.c.ageYears >= 10 and player.c.ageYears <= 100 and 1 >= random.random()*500
    message = "You ruined your favorite outfit. It's beyond repair."

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

    return messageFunction(fname, message, player, check)


def badRestaurantExperience(player, type='message'):
    """Terrible restaurant service and food"""
    fname = 'badRestaurantExperience'
    check = fname not in player.events and player.c.ageYears >= 16 and player.c.ageYears <= 100 and 1 >= random.random()*450
    message = "Terrible service and bad food at a restaurant. What a waste of money."

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

    return messageFunction(fname, message, player, check)


def sleepThroughAlarm(player, type='message'):
    """Sleeping through alarm and being late"""
    fname = 'sleepThroughAlarm'
    check = fname not in player.events and player.c.ageYears >= 12 and player.c.ageYears <= 100 and 1 >= random.random()*400
    message = "You slept through your alarm! You're going to be late for something important."

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

    return messageFunction(fname, message, player, check)


def packageStolen(player, type='message'):
    """Package stolen from doorstep"""
    fname = 'packageStolen'
    check = fname not in player.events and player.c.ageYears >= 18 and player.c.ageYears <= 100 and 1 >= random.random()*500
    message = "Someone stole the package from your doorstep. Great."

    if check:
        player.events.add(fname)
        player.c.happiness -= 20
        player.c.money -= 100

    return messageFunction(fname, message, player, check)


# ============================================================
# Technology & Modern Problems
# ============================================================

def phoneBreakdown(player, type='message'):
    """Phone completely died, need new one"""
    fname = 'phoneBreakdown'
    check = fname not in player.events and player.c.ageYears >= 12 and player.c.ageYears <= 100 and 1 >= random.random()*1200
    message = "Your phone completely died. You'll need a new one."

    if check:
        player.events.add(fname)
        player.c.money -= 600
        player.c.happiness -= 20
        player.c.stress += 15

    return messageFunction(fname, message, player, check)


def dataLoss(player, type='message'):
    """Computer crashed and lost important files"""
    fname = 'dataLoss'
    check = fname not in player.events and player.c.ageYears >= 16 and player.c.ageYears <= 100 and 1 >= random.random()*1500
    message = "Your computer crashed and you lost important files. All that work... gone."

    if check:
        player.events.add(fname)
        player.c.happiness -= 30
        player.c.stress += 35
        player.c.energy -= 40

    return messageFunction(fname, message, player, check)


def socialMediaCanceled(player, type='message'):
    """Being 'cancelled' on social media"""
    fname = 'socialMediaCanceled'
    has_social_media = hasattr(player.c, 'hasSocialMedia') and player.c.hasSocialMedia
    check = fname not in player.events and player.c.ageYears >= 16 and player.c.ageYears <= 40 and has_social_media and 1 >= random.random()*2000
    message = "You're being 'cancelled' on social media. People you don't know are attacking you."

    if check:
        player.events.add(fname)
        player.c.happiness -= 35
        player.c.social -= 30
        player.c.stress += 40

    return messageFunction(fname, message, player, check)


def cyberbullying(player, type='message'):
    """Being cyberbullied online"""
    fname = 'cyberbullying'
    has_social_media = hasattr(player.c, 'hasSocialMedia') and player.c.hasSocialMedia
    check = fname not in player.events and player.c.ageYears >= 10 and player.c.ageYears <= 25 and has_social_media and 1 >= random.random()*1500
    message = "You're being cyberbullied. The constant harassment is taking a toll."

    if check:
        player.events.add(fname)
        player.c.happiness -= 35
        player.c.stress += 40
        # Note: confidence stat not yet implemented in personClass

    return messageFunction(fname, message, player, check)


def accountHacked(player, type='message'):
    """Accounts hacked with embarrassing posts"""
    fname = 'accountHacked'
    check = fname not in player.events and player.c.ageYears >= 14 and player.c.ageYears <= 100 and 1 >= random.random()*1800
    message = "Your accounts were hacked and embarrassing posts were made. Everyone saw."

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

    return messageFunction(fname, message, player, check)


__all__ = [
    'carCrash',
    'oneTimeEventTest',
    'lowAffinity',
    'spilledDrink',
    'phoneDropped',
    'birdPooped',
    'forgotWallet',
    'missedBus',
    'badHairDay',
    'caughtInRain',
    'lostKeys',
    'ruinedClothes',
    'badRestaurantExperience',
    'sleepThroughAlarm',
    'packageStolen',
    'phoneBreakdown',
    'dataLoss',
    'socialMediaCanceled',
    'cyberbullying',
    'accountHacked',
]
