from direct.directnotify import DirectNotifyGlobal
from direct.interval.IntervalGlobal import *
from direct.showbase import PythonUtil
from pandac.PandaModules import *
import random
import types

import Toon, ToonDNA
from otp.avatar import Emote
from otp.otpbase import OTPLocalizer
from toontown.chat.ChatGlobals import *
from toontown.nametag.NametagGlobals import *
from toontown.toonbase import TTLocalizer


EmoteSleepIndex = 4
EmoteClear = -1

def doVictory(toon, volume = 1):
    duration = toon.getDuration('victory', 'legs')
    sfx = base.loadSfx('phase_3.5/audio/sfx/ENC_Win.ogg')
    sfxDuration = duration - 1.0
    sfxTrack = SoundInterval(sfx, loop=1, duration=sfxDuration, node=toon, volume=volume)
    track = Sequence(Func(toon.play, 'victory'), sfxTrack, duration=0)
    return (track, duration, None)


def doJump(toon, volume = 1):
    track = Sequence(Func(toon.play, 'jump'))
    return (track, 0, None)


def doDead(toon, volume = 1):
    toon.animFSM.request('Sad')
    return (None, 0, None)


def doAnnoyed(toon, volume = 1):
    duration = toon.getDuration('angry', 'torso')
    sfx = None
    if toon.style.getAnimal() == 'bear':
        sfx = base.loadSfx('phase_3.5/audio/dial/AV_bear_exclaim.ogg')
    else:
        sfx = base.loadSfx('phase_3.5/audio/sfx/avatar_emotion_angry.ogg')

    def playSfx():
        base.playSfx(sfx, volume=volume, node=toon)

    track = Sequence(Func(toon.angryEyes), Func(toon.blinkEyes), Func(toon.play, 'angry'), Func(playSfx))
    exitTrack = Sequence(Func(toon.normalEyes), Func(toon.blinkEyes))
    return (track, duration, exitTrack)


def doAngryEyes(toon, volume = 1):
    track = Sequence(Func(toon.angryEyes), Func(toon.blinkEyes), Wait(10.0), Func(toon.normalEyes))
    return (track, 0.1, None)


def doHappy(toon, volume = 1):
    track = Sequence(Func(toon.play, 'jump'), Func(toon.normalEyes), Func(toon.blinkEyes))
    duration = toon.getDuration('jump', 'legs')
    return (track, duration, None)


def doSad(toon, volume = 1):
    track = Sequence(Func(toon.sadEyes), Func(toon.blinkEyes))
    exitTrack = Sequence(Func(toon.normalEyes), Func(toon.blinkEyes))
    return (track, 3, exitTrack)


def doSleep(toon, volume = 1):
    duration = 4
    track = Sequence(Func(toon.stopLookAround), Func(toon.stopBlink), Func(toon.closeEyes), Func(toon.lerpLookAt, Point3(0, 1, -4)), Func(toon.loop, 'neutral'), Func(toon.setPlayRate, 0.4, 'neutral'), Func(toon.setChatAbsolute, TTLocalizer.ToonSleepString, CFThought))

    def wakeUpFromSleepEmote():
        toon.startLookAround()
        toon.openEyes()
        toon.startBlink()
        toon.setPlayRate(1, 'neutral')
        if toon.nametag.getChatText() == TTLocalizer.ToonSleepString:
            toon.clearChat()
        toon.lerpLookAt(Point3(0, 1, 0), time=0.25)

    exitTrack = Sequence(Func(wakeUpFromSleepEmote))
    return (track, duration, exitTrack)


def doYes(toon, volume = 1):
    tracks = Parallel(autoFinish=1)
    for lod in toon.getLODNames():
        h = toon.getPart('head', lod)
        tracks.append(Sequence(LerpHprInterval(h, 0.1, Vec3(0, -30, 0)), LerpHprInterval(h, 0.15, Vec3(0, 20, 0)), LerpHprInterval(h, 0.15, Vec3(0, -20, 0)), LerpHprInterval(h, 0.15, Vec3(0, 20, 0)), LerpHprInterval(h, 0.15, Vec3(0, -20, 0)), LerpHprInterval(h, 0.15, Vec3(0, 20, 0)), LerpHprInterval(h, 0.1, Vec3(0, 0, 0))))

    tracks.start()
    return (None, 0, None)


def doNo(toon, volume = 1):
    tracks = Parallel(autoFinish=1)
    for lod in toon.getLODNames():
        h = toon.getPart('head', lod)
        tracks.append(Sequence(LerpHprInterval(h, 0.1, Vec3(40, 0, 0)), LerpHprInterval(h, 0.15, Vec3(-40, 0, 0)), LerpHprInterval(h, 0.15, Vec3(40, 0, 0)), LerpHprInterval(h, 0.15, Vec3(-40, 0, 0)), LerpHprInterval(h, 0.15, Vec3(20, 0, 0)), LerpHprInterval(h, 0.15, Vec3(-20, 0, 0)), LerpHprInterval(h, 0.1, Vec3(0, 0, 0))))

    tracks.start()
    return (None, 0, None)


def doOk(toon, volume = 1):
    return (None, 0, None)


def doShrug(toon, volume = 1):
    sfx = base.loadSfx('phase_3.5/audio/sfx/avatar_emotion_shrug.ogg')

    def playSfx():
        base.playSfx(sfx, volume=volume, node=toon)

    track = Sequence(Func(toon.play, 'shrug'), Func(playSfx))
    duration = toon.getDuration('shrug', 'torso')
    return (track, duration, None)


def doWave(toon, volume = 1):
    track = Sequence(Func(toon.play, 'wave'))
    duration = toon.getDuration('wave', 'torso')
    return (track, duration, None)


def doApplause(toon, volume = 1):
    sfx = base.loadSfx('phase_4/audio/sfx/avatar_emotion_applause.ogg')

    def playSfx():
        base.playSfx(sfx, volume=1, node=toon)

    track = Sequence(Func(toon.play, 'applause'), Func(playSfx))
    duration = toon.getDuration('applause', 'torso')
    return (track, duration, None)


def doConfused(toon, volume = 1):
    sfx = base.loadSfx('phase_4/audio/sfx/avatar_emotion_confused.ogg')

    def playSfx():
        base.playSfx(sfx, node=toon, volume=volume)

    track = Sequence(Func(toon.play, 'confused'), Func(playSfx))
    duration = toon.getDuration('confused', 'torso')
    return (track, duration, None)


def doSlipForward(toon, volume = 1):
    sfx = base.loadSfx('phase_4/audio/sfx/MG_cannon_hit_dirt.ogg')

    def playSfx():
        base.playSfx(sfx, volume=volume, node=toon)

    sfxDelay = 0.7
    track = Sequence(Func(toon.play, 'slip-forward'), Wait(sfxDelay), Func(playSfx))
    duration = toon.getDuration('slip-forward', 'torso') - sfxDelay
    return (track, duration, None)


def doBored(toon, volume = 1):
    sfx = base.loadSfx('phase_4/audio/sfx/avatar_emotion_bored.ogg')

    def playSfx():
        base.playSfx(sfx, volume=volume, node=toon)

    sfxDelay = 2.2
    track = Sequence(Func(toon.play, 'bored'), Wait(sfxDelay), Func(playSfx))
    duration = toon.getDuration('bored', 'torso') - sfxDelay
    return (track, duration, None)


def doBow(toon, volume = 1):
    if toon.style.torso[1] == 'd':
        track = Sequence(Func(toon.play, 'curtsy'))
        duration = toon.getDuration('curtsy', 'torso')
    else:
        track = Sequence(Func(toon.play, 'bow'))
        duration = toon.getDuration('bow', 'torso')
    return (track, duration, None)


def doSlipBackward(toon, volume = 1):
    sfx = base.loadSfx('phase_4/audio/sfx/MG_cannon_hit_dirt.ogg')

    def playSfx():
        base.playSfx(sfx, volume=volume, node=toon)

    sfxDelay = 0.7
    track = Sequence(Func(toon.play, 'slip-backward'), Wait(sfxDelay), Func(playSfx))
    duration = toon.getDuration('slip-backward', 'torso') - sfxDelay
    return (track, duration, None)


def doThink(toon, volume = 1):
    duration = 47.0 / 24.0 * 2
    animTrack = Sequence(ActorInterval(toon, 'think', startFrame=0, endFrame=46), ActorInterval(toon, 'think', startFrame=46, endFrame=0))
    track = Sequence(animTrack, duration=0)
    return (track, duration, None)


def doCringe(toon, volume = 1):
    track = Sequence(Func(toon.play, 'cringe'))
    duration = toon.getDuration('cringe', 'torso')
    return (track, duration, None)


def doResistanceSalute(toon, volume=1):
    track = Sequence(
        Func(toon.setChatAbsolute, OTPLocalizer.CustomSCStrings[4020], CFSpeech|CFTimeout),
        Func(toon.setPlayRate, 0.75, 'victory'),
        Func(toon.pingpong, 'victory', fromFrame=0, toFrame=9),
        Func(toon.setPlayRate, 1, 'victory')
    )
    duration = 20 / toon.getFrameRate('victory')
    return (track, duration, None)


def doNothing(toon, volume = 1):
    return (None, 0, None)


def doSurprise(toon, volume = 1):
    sfx = None
    sfx = base.loadSfx('phase_4/audio/sfx/avatar_emotion_surprise.ogg')

    def playSfx(volume = 1):
        base.playSfx(sfx, volume=volume, node=toon)

    def playAnim(anim):
        anim.start()

    def stopAnim(anim):
        anim.finish()
        toon.stop()
        sfx.stop()

    anim = Sequence(ActorInterval(toon, 'conked', startFrame=9, endFrame=50), ActorInterval(toon, 'conked', startFrame=70, endFrame=101))
    track = Sequence(Func(toon.stopBlink), Func(toon.surpriseEyes), Func(toon.showSurpriseMuzzle), Parallel(Func(playAnim, anim), Func(playSfx, volume)))
    exitTrack = Sequence(Func(toon.hideSurpriseMuzzle), Func(toon.openEyes), Func(toon.startBlink), Func(stopAnim, anim))
    return (track, 3.0, exitTrack)


def doUpset(toon, volume = 1):
    sfxList = ('phase_4/audio/sfx/avatar_emotion_very_sad_1.ogg', 'phase_4/audio/sfx/avatar_emotion_very_sad.ogg')
    sfx = base.loadSfx(random.choice(sfxList))

    def playSfx(volume = 1):
        base.playSfx(sfx, volume=volume, node=toon)

    def playAnim(anim):
        anim.start()

    def stopAnim(anim):
        anim.finish()
        toon.stop()
        sfx.stop()