import requests
import uuid
import sys

###
# GLOBALS
###

class ResponseStatus():
    Ok = 0
    Error = 1
    Offline = 2

max_job_name_length = 30

class ToolHead():
    Gripper = 0

###
# CLASSES
###

# TODO: add oriention left or right
# TODO: bei jeder Positionsangabe (einmal bei XY und einmal bei Z) geben wir eine Zahl an als Kraft, die normalerweise nicht überschritten werden soll

class Rex:
    """
    This class represents a rex robot.
    """
    def __init__(self, robot_name, job_name, address='http://localhost:50055'):
        self.robot_name = robot_name
        self.job_id = uuid.uuid4().__str__()
        self.job_name = job_name[:max_job_name_length]
        self.address = address
        self.x = 0
        self.y = 0
        self.z = 0
        self.tool_head = 0

    def _request_data(self):
        return {
            'robotName': self.robot_name, 
            'jobId': self.job_id, 
            'jobName': self.job_name
        }

    def _post_request(self, json):
        obj = {}

        obj = self._request_data()
        obj['task'] = json

        res = requests.post(self.address + '/v1/control/1', json=obj)

        if res.status_code != 200:
            print("status code", res.status_code)
            sys.exit(1)

        json = res.json()

        status = json['Status']

        if status == ResponseStatus.Offline:
            print("robot offline")
            sys.exit(1)

        if status == ResponseStatus.Error:
            print("robot error")
            sys.exit(1)

        if status != ResponseStatus.Ok:
            print("robot some other error")
            sys.exit(1)

    def move_to(self, x = None, y = None, z = None, AprilTagId = None):
        """
        Move the robot to the new position.
        """
        data = {}

        if x != None:
            self.x = x
            data['x'] = self.x

        if y != None:
            self.y = y
            data['y'] = self.y

        if z != None:
            self.z = z
            data['z'] = self.z

        if len(data) == 0:
            print("No data provided for move_to")
            return
        
        if AprilTagId != None:
            data['AprilTagId'] = AprilTagId

        print('Robot: ' + self.robot_name + ' moved to ' + str(data))

        self._post_request(data)

    def change_tool_head(self, tool_head = int):
        """
        Change the tool head.
        """
        print('Robot: ' + self.robot_name + ' changed tool head to ' + str(tool_head))

        data = {'ToolHead': tool_head}

        self._post_request(data)

    def finish(self):
        """
        Finish the robot. 
        This will update the robot status to idle.
        """
        print('Robot finished')

        # TODO: request to the server to finish the robot and update the robot status to idle

        res = requests.post(self.address + '/v1/control/1/finish', json={
            'robotName': self.robot_name, 
            'jobId': self.job_id
        })

        print("status code", res.status_code)