# 1. launch single navigation.
# 2. run this test.


from sub_engine_client import IF as Engine
import time
from pyrois.RoIS_Common import Component_Status
from utilities import setup_single_robot
from collections import namedtuple
from pyrois.Service_Application_IF import Service_Application_IF
import xmlrpc


class CommandStatus(object):
    def __init__(self, is_finished):
        self.is_finished = is_finished

    def __repr__(self):
        return "CommandStatus<is_finished: {}>\n".format(self.is_finished)


class Service(Service_Application_IF):
    def __init__(self, uri):
        super().__init__(uri)
        self.commmand_status_table = {}
        self.is_navigation_running = False

    def completed(self, command_id, status):
        print(self.commmand_status_table[command_id])
        self.commmand_status_table[command_id].is_finished = True
        print("----------------------------------------------")
        print("commmand_status:\n", self.commmand_status_table)
        print("----------------------------------------------")
        self.is_navigation_running = False

    def go_robot_to(self, dest):
        self.is_navigation_running = True
        self._proxy = xmlrpc.client.ServerProxy(self._uri)
        time.sleep(3)
        status, command_id = \
            self._proxy.set_parameter('Navigation', dest, "", "")
        self.commmand_status_table[command_id] = CommandStatus(False)
        print("----------------------------------------------")
        print("commmand_status:\n", self.commmand_status_table)
        print("----------------------------------------------")

    def recog_speech(self):
        print("Starting recog_speech...")
        time.sleep(3)
        self._proxy = xmlrpc.client.ServerProxy(self._uri)
        time.sleep(3)
        status, command_id = \
            self._proxy.set_parameter('Speech_Recognition', "lang", "grammer", "rule")
        self.commmand_status_table[command_id] = CommandStatus(False)
        print("----------------------------------------------")
        print("commmand_status:\n", self.commmand_status_table)
        print("----------------------------------------------")


    def run(self):
        destinations = ["point_e", "point_d", "point_c", "point_b"]
        for dest in destinations:
            self.recog_speech()
            time.sleep(3)


process = setup_single_robot()
time.sleep(10)
service = Service('http://127.0.0.1:8010')
time.sleep(10)

service.run()

print("finish")
