from functions import getWeekDay
from functions import mealEvent
import random

def getIntradayActivity(player,person):
    person.lastIntraDayMessage = person.intraDayMessage
    person.intraDayMessage = ""
    def get_eventByTime(person,currentTime):
        plan = person.dailyPlan
        for i in range(0,len(plan)):
            if (plan[i].time == currentTime):
                if (plan[i].location != person.location):
                    person.location = plan[i].location
                # if (plan[i].name == "lunch" or plan[i].name == "dinner"):
                    # mealEvent(player) disabled for now as meals are not distributed properly
                return plan[i]
    event = get_eventByTime(person,player.hourOfDay)
    if (event and getattr(event,'title')):
        person.intraDayMessage = event.title
    return person

def get_dailyPlan(player,person,type='message'):
    from functions import getWeekDay,getFromArray,randArray,find_by_id,locationClass,dailyEvent
    person.dailyPlan = []

    homeID = 'home-'+person.id
    if ('self' in person.relationships or person.familyLevel == 1):
        homeID = 'home-'+player.c.id
    homeObj = find_by_id(player.l, homeID)
    if (not homeObj):
        homeObj = locationClass(homeID,'home')
        player.l.append(homeObj)
    home = homeObj.id
    schoolObj = find_by_id(player.l, 'school-'+player.c.id)
    if (not schoolObj):
        schoolObj = locationClass('school-'+player.c.id,'school')
        player.l.append(schoolObj)
    school = schoolObj.id
    workObj = find_by_id(player.l, 'work-'+player.c.id)
    if (not workObj):
        workObj = locationClass('work-'+player.c.id,'work')
        player.l.append(workObj)
    work = workObj.id

    player.weekend = False
    if (player.dayOfWeek == 'Saturday' or player.dayOfWeek == 'Sunday'):
        player.weekend = True
    for schedule in person.schedules: 
        if (hasattr(schedule,'duration') and schedule.days and schedule.executions < schedule.duration and str(player.dayOfWeek) in schedule.days.daysOfWeek):
            e = dailyEvent(schedule.days.hour,schedule.location)
            e.title = schedule.title
            e.name = schedule.id
            schedule.executions += 1
            person.dailyPlan.append(e)
            if (schedule.executions == schedule.duration):
                player.messageQueue.append('"'+schedule.title+'" has been completed!')
    #holiday events
    currentTime = 1
    if (player.dayEvent): # events on holidays
        currentTime = 7
        if (player.dayEvent == "christmas"):
            location = home
            e = dailyEvent(currentTime,location)
            e.title = "You wake up on Christmas morning"
            e.name = "wakeUp"
            person.dailyPlan.append(e)

            currentTime += 1
            e = dailyEvent(currentTime,location)
            e.title = "You open gifts!"
            e.name = "christmas"
            person.dailyPlan.append(e)
            # get parent affinity and base gift quality on this
            # getFromArray(player.r,'Mother',1)
            # getFromArray(player.r,'Father',1).affinity
        return person
    #retired events
    if (person.ageYears > 50 and person.occupation == 'retired'):
        wakeUpTime = random.randint(7,9)
        currentTime = wakeUpTime
        location = home
        e = dailyEvent(wakeUpTime,location)
        e.title = "You wake up"
        e.name = "wakeUp"
        person.dailyPlan.append(e)

        currentTime += 1
        e = dailyEvent(currentTime,location)
        e.title = "You eat breakfast"
        e.name = "breakfast"
        person.dailyPlan.append(e)

        currentTime = 12
        e = dailyEvent(currentTime,location)
        e.title = "You eat lunch"
        e.name = "lunch"
        person.dailyPlan.append(e)

        currentTime = 18
        e = dailyEvent(currentTime,location)
        e.title = "You eat dinner"
        e.name = "dinner"
        person.dailyPlan.append(e)

    #working adult events
    if (person.ageYears > 10 and person.occupation != 'student' and person.occupation != 'preschool' and person.occupation != 'retired'):
        if (player.weekend == False):
            wakeUpTime = random.randint(5,9)
            currentTime = wakeUpTime
            location = home
            e = dailyEvent(wakeUpTime,location)
            e.title = "You wake up"
            e.name = "wakeUp"
            person.dailyPlan.append(e)

            currentTime += 1
            e = dailyEvent(currentTime,location)
            e.location = work
            e.title = "You arrive at work"
            e.name = "work"
            person.dailyPlan.append(e)

            currentTime = 12
            e = dailyEvent(currentTime,location)
            e.title = "You eat lunch"
            e.name = "lunch"
            person.dailyPlan.append(e)

            currentTime = 17
            e = dailyEvent(currentTime,location)
            e.title = "You finish work"
            e.name = "work"
            person.dailyPlan.append(e)

            currentTime = 18
            e = dailyEvent(currentTime,location)
            e.title = "You arrive home"
            e.location = home
            e.name = "home"
            person.dailyPlan.append(e)

            currentTime = 19
            e = dailyEvent(currentTime,location)
            e.title = "You eat dinner"
            e.name = "dinner"
            person.dailyPlan.append(e)

            currentTime = 20
            location = home
            e = dailyEvent(currentTime,location)
            e.title = "You relax at home"
            e.name = "home"
            person.dailyPlan.append(e)

            currentTime = wakeUpTime - 8
            if (currentTime < 0):
                currentTime = 24 + currentTime
            e = dailyEvent(currentTime,location)
            e.title = "You go to bed"
            e.name = "bed"
            person.dailyPlan.append(e)
        elif (player.weekend):
            wakeUpTime = random.randint(5,9)
            location = home
            e = dailyEvent(wakeUpTime,location)
            e.title = "You wake up"
            e.name = "wakeUp"
            person.dailyPlan.append(e)

            currentTime = wakeUpTime + 1
            e = dailyEvent(currentTime,location)
            e.title = "You eat breakfast"
            e.name = "breakfast"
            person.dailyPlan.append(e)

            currentTime = 12
            e = dailyEvent(currentTime,location)
            e.title = "You eat lunch"
            e.name = "lunch"
            person.dailyPlan.append(e)

            currentTime = 18
            location = home
            e = dailyEvent(currentTime,location)
            e.title = "You eat dinner"
            e.name = "dinner"
            person.dailyPlan.append(e)

            currentTime = 20
            e = dailyEvent(currentTime,location)
            e.title = "You relax at home"
            e.name = "home"
            person.dailyPlan.append(e)

            currentTime = wakeUpTime - 8
            if (currentTime < 0):
                currentTime = 24 + currentTime
            e = dailyEvent(currentTime,location)
            e.title = "You go to bed"
            e.name = "bed"
            person.dailyPlan.append(e)


    #infant events
    if (person.occupation == "preschool"):
        currentTime = random.randint(7,10)
        location = home
        e = dailyEvent(currentTime,location)
        e.title = "You wake up"
        e.location = location
        e.name = "wakeUp"
        person.dailyPlan.append(e) 

        currentTime += 1
        e = dailyEvent(currentTime,location)
        e.title = "You are fed breakfast"
        e.name = "breakfast"
        person.dailyPlan.append(e)

        currentTime = 12
        e = dailyEvent(currentTime,location)
        e.title = "You are fed lunch"
        e.name = "lunch"
        person.dailyPlan.append(e)

        currentTime = 18
        e = dailyEvent(currentTime,location)
        e.title = "You are fed dinner"
        e.name = "dinner"
        person.dailyPlan.append(e)

        currentTime = 20
        e = dailyEvent(currentTime,location)
        e.title = "You go to bed"
        e.name = "bed"
        person.dailyPlan.append(e)

    #student Events
    if ((player.weekend or player.summerVacation) and person.occupation == "student"): #weekend or summer
        # Student Weekend Activities
        currentTime = random.randint(7,10)
        location = home
        e = dailyEvent(currentTime,location)
        e.title = "You wake up"
        e.name = "wakeUp"
        person.dailyPlan.append(e)

        currentTime += 1
        e = dailyEvent(currentTime,location)
        e.title = "You eat breakfast"
        e.name = "breakfast"
        person.dailyPlan.append(e)

        currentTime += 1
        e = dailyEvent(currentTime,location)
        e.title = "You relax at home"
        e.name = "weekend"
        person.dailyPlan.append(e)

        currentTime = 12
        e = dailyEvent(currentTime,location)
        e.title = "You eat lunch"
        e.name = "lunch"
        person.dailyPlan.append(e)

        if (player.dayOfWeek == 'Saturday'):
            extracurricular = getFromArray(person.activities,'extracurricular')
            currentTime = 10
            if (len(extracurricular) > 0):
                activity = randArray(extracurricular)
                if (activity.title == 'Football' or activity.title == 'Baseball' or activity.title == 'Soccer'): 
                    e = dailyEvent(currentTime,location)
                    e.title = "You play a " + activity.title + " game"
                    e.name = "extracurricular"
                    person.dailyPlan.append(e)

                    currentTime += 1
                    e = dailyEvent(currentTime,location)
                    randomEvent = random.randint(0,1)
                    if (randomEvent == 0):
                        e.title = "You Win!"
                    else:
                        e.title = "You Lose!"
                    e.name = "extracurricular"
                    person.dailyPlan.append(e)


        currentTime = 18
        e = dailyEvent(currentTime,location)
        e.title = "You eat dinner"
        e.name = "dinner"
        person.dailyPlan.append(e)

        if (person.occupation == "student" and random.randint(0,1) == 1):
            if (person.occupation == "student" and random.randint(0,1) == 1):
                currentTime = 20
                e = dailyEvent(currentTime,location)
                if (random.randint(0,1) == 1):
                    e.title = "You do your homework"
                else:
                    e.title = "You study"
                e.name = "study"
                person.dailyPlan.append(e)
        elif (person.occupation == "student" and random.randint(0,1) == 1):
            currentTime = 20
            partyObj = find_by_id(player.l,'party-'+player.c.id)
            if (partyObj == None):
                partyObj = locationClass('party-'+player.c.id,'party')
                player.l.append(partyObj)
            location = partyObj.id
            e = dailyEvent(currentTime,location)
            e.title = "You go to a party with friends"
            e.name = "party"
            person.dailyPlan.append(e)
            if (random.randint(0,100) == 1):
                currentTime = 22
                e = dailyEvent(currentTime,location)
                e.title = "You get drunk and pass out"
                e.name = "party"
                person.dailyPlan.append(e)
            else:
                currentTime = 22
                location = home
                e = dailyEvent(currentTime,location)
                e.title = "You return home safely"
                e.name = "party"
            person.dailyPlan.append(e)
        else:
            currentTime = 20
            location = home
            e = dailyEvent(currentTime,location)
            e.title = "You relax at home"
            e.name = "weekend"
            person.dailyPlan.append(e)


        currentTime = random.randint(23,24)
        e = dailyEvent(currentTime,location)
        e.title = "You go to sleep"
        e.name = "sleep"
        person.dailyPlan.append(e)
    if (not player.weekend and person.occupation == "student" and not player.summerVacation):
        # student schoolday activities
        location = home
        currentTime = 7
        e = dailyEvent(currentTime,location)
        e.title = "You wake up for school"
        e.name = "wakeUp"
        person.dailyPlan.append(e)

        currentTime += 1
        e = dailyEvent(currentTime,location)
        e.title = "You're on the way to school"
        e.name = "school"
        person.dailyPlan.append(e)

        currentTime += 1
        location = school
        e = dailyEvent(currentTime,location)
        e.title = "Schoolday starts"
        e.name = "school"
        person.dailyPlan.append(e)

        # Schedule: 9=1st, 10=2nd, 11=3rd, 12=Lunch, 13=4th, 14=5th/Last
        currentTime = 10
        e = dailyEvent(currentTime,location)
        e.title = "Your second class starts"
        e.name = "school"
        person.dailyPlan.append(e)

        currentTime = 11
        e = dailyEvent(currentTime,location)
        e.title = "Your third class starts"
        e.name = "school"
        person.dailyPlan.append(e)

        currentTime = 12
        e = dailyEvent(currentTime,location)
        e.title = "Lunchtime"
        e.name = "lunch"
        person.dailyPlan.append(e)

        currentTime = 13
        e = dailyEvent(currentTime,location)
        e.title = "Your fourth class starts"
        e.name = "school"
        person.dailyPlan.append(e)

        currentTime = 14
        e = dailyEvent(currentTime,location)
        e.title = "Your last class starts"
        e.name = "school"
        person.dailyPlan.append(e)

        extracurricular = getFromArray(person.activities,'extracurricular')
        currentTime = 15
        if (len(extracurricular) > 0):
            activity = randArray(extracurricular)
            e = dailyEvent(currentTime,location)
            e.title = "You go to "+activity.title+" practice."
            e.name = "school"
            person.dailyPlan.append(e)
            currentTime = 16

        e = dailyEvent(currentTime,location)
        e.title = "You're on the way home"
        e.name = "school"
        person.dailyPlan.append(e)

        currentTime+=1
        location = home
        e = dailyEvent(currentTime,location)
        e.title = "You arrive at home"
        e.name = "home"
        person.dailyPlan.append(e)

        currentTime+=1
        e = dailyEvent(currentTime,location)
        e.title = "You eat dinner"
        e.name = "dinner"
        person.dailyPlan.append(e)

        currentTime+=1
        e = dailyEvent(currentTime,location)
        e.title = "You relax at home"
        e.name = "relax"
        person.dailyPlan.append(e)

        currentTime = random.randint(23,24)
        e = dailyEvent(currentTime,location)
        e.title = "You go to sleep"
        e.name = "sleep"
        person.dailyPlan.append(e)

    return person
