from google.protobuf.timestamp_pb2 import Timestamp

from clients._path_setup import ensure_paths

ensure_paths()

from clients.base_client import RobotClientBase
from cmvr.api import common_pb2
from cmvr.api import hlc_command_pb2 as pb
from cmvr.api import hlc_service_pb2_grpc as rpc


class HlcClient(RobotClientBase):
    """Client for calling cmvr.api.HlcService."""

    def _init_stubs(self, channel):
        self.stub = rpc.HlcServiceStub(channel)

    @staticmethod
    def _make_header(device_id: str):
        header = common_pb2.CommandHeader.Request()
        header.device_id = device_id
        timestamp = Timestamp()
        timestamp.GetCurrentTime()
        header.timestamp.CopyFrom(timestamp)
        return header

    def touch(
        self,
        u: int,
        v: int,
        max_force: float,
        device_id="hc01",
        timeout=None,
    ):
        request = pb.Touch.Request()
        request.header.CopyFrom(self._make_header(device_id))
        request.u = u
        request.v = v
        request.max_force = max_force

        rpc_timeout = self.timeout if timeout is None else timeout
        response = self.stub.touch(request, timeout=rpc_timeout)
        return response


def main():
    client = HlcClient()
    try:
        response = client.touch(
            u=12,
            v=34,
            max_force=10.0,
            device_id="hc01",
        )
        print("touch RPC call completed")
        print(f"success: {response.header.success}")
        print(f"error_message: {response.header.error_message}")
        print(f"timestamp: {response.header.timestamp.seconds}")
        return response
    finally:
        client.close()


if __name__ == "__main__":
    main()
