from agentspace import Agent, space
#from receiver_client import ReceiverAgent
from receiver_server import ReceiverAgent
from lips import LipsAgent
from pyicubsim import iCubApplicationName, iCubEmotion, iCubHead, iCubLeftArm, iCubRightArm, iCubBall, iCubTorso, hit
from speak import speak
from prompt import askGPT
import time
import re
import os
import signal

def quit():
    os._exit(0)
    
# exit on ctrl-c
def signal_handler(signal, frame):
    quit()
    
signal.signal(signal.SIGINT, signal_handler)

iCubApplicationName('/listener')

class ListenerAgent(Agent):

    def match(self,pattern,text):
        search = re.search(pattern,text)
        if search is None:
            self.groups = []
            return False
        else:
            self.groups = search.groups()
            return True
    
    def matched(self):
        return self.groups

    def __init__(self,name):
        self.name = name
        super().__init__()
        
    def init(self):
        self.head = iCubHead()
        self.left_arm = iCubLeftArm()
        self.right_arm = iCubRightArm()
        self.emotion = iCubEmotion()
        self.ball = iCubBall()
        self.torso = iCubTorso()
        space.attach_trigger(self.name,self)
 
    def senseSelectAct(self):
        text = space(default='')[self.name]
        text = text.lower()
        if len(text) > 0:
            print("->",text)
            if self.match(r'say (.*)',text):
                print(self.matched()[0],'should be said')
                speak(self.matched()[0])
            elif text == "connect":
                pass
            else:
                if self.ball.get()[2] > -50:
                    situation = "There is a ball on the table."
                else:
                    situation = "The table is empty."
                actions = askGPT(command=text,situation=situation)
                for action in actions:
                    if action['action'] == 'Say':
                        speak(action['parameters'][0])
                        time.sleep(0.5)
                        self.emotion.set('neu')
                    elif action['action'] == 'Hit':
                        goal = self.ball.get() 
                        possible = hit(self.torso,self.right_arm,goal,False)
                        if possible:
                            time.sleep(2)
                            self.right_arm.reset()
                            self.torso.reset()
                            time.sleep(2)
                        else:
                            speak('It is not possible')
                    elif action['action'] == 'Smile':
                        self.emotion.set('hap') #TBD: params
                    elif action['action'] == 'StandardPosition':
                        self.right_arm.set((0,80,0,50,0,0,0,59,20,20,20,10,10,10,10,10))
                    elif action['action'] == 'WaveWithHand': 
                        self.right_arm.set((0,80,0,50,0,0,0,59,20,20,20,10,10,10,10,10))
                        time.sleep(0.75)
                        self.right_arm.set(joint1=85,joint3=40,joint2=40)
                        time.sleep(0.5)
                        self.right_arm.set((0,80,0,50,0,0,0,59,20,20,20,10,10,10,10,10))
                        time.sleep(0.5)
            
#ReceiverAgent('158.195.28.235',7001,'text') #192.168.0.20 behind router
ReceiverAgent(7171,'text')
LipsAgent('speaking')
ListenerAgent('text')
