"""
Health & Wellness Issue Events (Negative)
Health-related negative events and wellness setbacks

Events:
- injuryFromAccident: Injury from accident requiring recovery (ages 5-100, messageEvent)
- chronicPain: Developing chronic pain (ages 25-100, messageEvent)
- dentalEmergency: Severe tooth pain requiring emergency dental work (ages 10-100, messageEvent)
- seriousIllness: Serious health condition diagnosis (ages 20-100, messageEvent)
- weightGain: Significant weight gain (ages 16-100, messageEvent)
- sleepDeprivation: Extended period of poor sleep (ages 14-100, messageEvent)
- addictionProblem: Developing unhealthy dependency (ages 16-100, questionEvent)
"""

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


def injuryFromAccident(player, type='message'):
    """Injury from accident requiring time to recover"""
    fname = 'injuryFromAccident'
    check = fname not in player.events and player.c.ageYears >= 5 and player.c.ageYears <= 100 and 1 >= random.random()*1000
    message = "You got injured in an accident. You'll need time to recover."

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

    return messageFunction(fname, message, player, check, moneyCost=300)


def chronicPain(player, type='message'):
    """Developing chronic pain that makes daily activities harder"""
    fname = 'chronicPain'
    check = fname not in player.events and player.c.ageYears >= 25 and player.c.ageYears <= 100 and 1 >= random.random()*2000
    message = "You've developed chronic pain that makes daily activities harder."

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

    return messageFunction(fname, message, player, check)


def dentalEmergency(player, type='message'):
    """Severe tooth pain requiring emergency dental work"""
    fname = 'dentalEmergency'
    check = fname not in player.events and player.c.ageYears >= 10 and player.c.ageYears <= 100 and 1 >= random.random()*1500
    message = "Severe tooth pain! You need emergency dental work."

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

    return messageFunction(fname, message, player, check, moneyCost=500)


def seriousIllness(player, type='message'):
    """Diagnosis of a serious health condition requiring long treatment"""
    fname = 'seriousIllness'
    check = fname not in player.events and player.c.ageYears >= 20 and player.c.ageYears <= 100 and 1 >= random.random()*3000
    message = "You've been diagnosed with a serious health condition. Treatment will be long and difficult."

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

    return messageFunction(fname, message, player, check, moneyCost=2000)


def weightGain(player, type='message'):
    """Significant weight gain affecting comfort and self-image"""
    fname = 'weightGain'
    check = fname not in player.events and player.c.ageYears >= 16 and player.c.ageYears <= 100 and 1 >= random.random()*1000
    message = "You've gained significant weight. Your clothes don't fit and you feel uncomfortable."

    if check:
        player.events.add(fname)
        player.c.happiness -= 20
        # Use confidence if available, otherwise fall back to social
        if hasattr(player.c, 'confidence'):
            player.c.confidence -= 25
        else:
            player.c.social -= 15
        player.c.health -= 10
        # Increase weight if the system tracks it
        if hasattr(player.c, 'weight'):
            player.c.weight += 10

    return messageFunction(fname, message, player, check)


def sleepDeprivation(player, type='message'):
    """Extended period of poor sleep affecting everything"""
    fname = 'sleepDeprivation'
    check = fname not in player.events and player.c.ageYears >= 14 and player.c.ageYears <= 100 and 1 >= random.random()*800
    message = "You haven't been sleeping well for weeks. Everything is harder now."

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

    return messageFunction(fname, message, player, check)


def addictionProblem(player, type='message', message=False, response=False):
    """Developing an unhealthy dependency affecting life"""
    fname = 'addictionProblem'
    check = fname not in player.askedQuestions and player.c.ageYears >= 16 and player.c.ageYears <= 100 and 1 >= random.random()*3000
    message = "You've developed an unhealthy dependency. It's affecting your life."

    answerOptions = [
        answerOption('Seek professional help', '0', moneyCost=1000, energyCost=30),
        answerOption('Try to quit alone', '1', energyCost=50),
        answerOption('Ignore the problem', '2')
    ]

    if (type != 'answer'):
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        if (response['option'] == answerOptions[0].option):
            # Seek professional help
            player.messageQueue.append("You seek professional help to address your dependency. It's difficult and expensive, but you're taking the right steps toward recovery.")
            player.c.happiness += 10
            # Long-term benefit noted in message
        elif (response['option'] == answerOptions[1].option):
            # Try to quit alone
            player.messageQueue.append("You try to quit on your own. It's incredibly difficult and emotionally draining, but you're determined to break free.")
            player.c.happiness -= 20
        elif (response['option'] == answerOptions[2].option):
            # Ignore the problem
            player.messageQueue.append("You ignore the problem and continue as before. Your health and happiness continue to deteriorate.")
            player.c.health -= 30
            player.c.happiness -= 30


__all__ = [
    'injuryFromAccident',
    'chronicPain',
    'dentalEmergency',
    'seriousIllness',
    'weightGain',
    'sleepDeprivation',
    'addictionProblem',
]
