from clients._path_setup import ensure_paths

ensure_paths()

from google.protobuf import timestamp_pb2

from clients.base_client import RobotClientBase
from cmvr.api import hlc_command_pb2 as pb


class TouchClient(RobotClientBase):
    """Client to send Touch commands"""

    def send(self, u: int, v: int, max_force: int, device_id="hc01"):
        """
        u, v: target coordinates
        max_force: maximum touch force
        device_id: device ID
        """
        # Construct request
        req = pb.Touch.Request()
        req.header.device_id = device_id
        ts = timestamp_pb2.Timestamp()
        ts.GetCurrentTime()
        req.header.timestamp.CopyFrom(ts)

        req.u = u
        req.v = v
        req.max_force = max_force

        # Call RPC
        try:
            resp = self.stub.touch(req, timeout=self.timeout)
            success = getattr(resp.header, "success", None)
            error_msg = getattr(resp.header, "error_message", "")
            timestamp_sec = getattr(resp.header.timestamp, "seconds", 0)

            print("Touch RPC call succeeded")
            print(f"Success: {success}")
            print(f"Error message: {error_msg}")
            print(f"Timestamp: {timestamp_sec}")
        except Exception as e:
            print("Touch RPC call failed:", e)


if __name__ == "__main__":
    # Initialize client
    client = TouchClient()

    # Send Touch command
    client.send(u=12, v=13, max_force=1300, device_id="hc01")

    # Close client
    client.close()
