import json
from typing import Optional, Tuple

from ...rpc.client import Client
from .audio_api import *


"""
" class AudioClient
"""
class AudioClient(Client):
    def __init__(self):
        super().__init__(AUDIO_SERVICE_NAME, False)
        self.tts_index = 0

    def Init(self):
        # set api version
        self._SetApiVerson(AUDIO_API_VERSION)

        # regist api
        self._RegistApi(ROBOT_API_ID_AUDIO_TTS, 0)
        self._RegistApi(ROBOT_API_ID_AUDIO_ASR, 0)
        self._RegistApi(ROBOT_API_ID_AUDIO_START_PLAY, 0)
        self._RegistApi(ROBOT_API_ID_AUDIO_STOP_PLAY, 0)
        self._RegistApi(ROBOT_API_ID_AUDIO_GET_VOLUME, 0)
        self._RegistApi(ROBOT_API_ID_AUDIO_SET_VOLUME, 0)
        self._RegistApi(ROBOT_API_ID_AUDIO_SET_RGB_LED, 0)

    ## API Call ##
    def TtsMaker(self, text: str, speaker_id: int) -> int:
        p = {}
        p["index"] = self.tts_index
        p["text"] = text
        p["speaker_id"] = speaker_id
        self.tts_index += 1
        parameter = json.dumps(p)
        code, data = self._Call(ROBOT_API_ID_AUDIO_TTS, parameter)
        return code

    def GetVolume(self):
        p = {}
        parameter = json.dumps(p)
        code, data = self._Call(ROBOT_API_ID_AUDIO_GET_VOLUME, parameter)
        if code == 0:
            return code, json.loads(data)
        else:
            return code, None

    def SetVolume(self, volume: int) -> int:
        p = {}
        p["volume"] = volume
        parameter = json.dumps(p)
        code, data = self._Call(ROBOT_API_ID_AUDIO_SET_VOLUME, parameter)
        return code

    def LedControl(self, R: int, G: int, B: int) -> int:
        p = {}
        p["R"] = R
        p["G"] = G
        p["B"] = B
        parameter = json.dumps(p)
        code, data = self._Call(ROBOT_API_ID_AUDIO_SET_RGB_LED, parameter)
        return code

    def PlayStream(self, app_name: str, stream_id: str, pcm_data: bytes):
        param = json.dumps({"app_name": app_name, "stream_id": stream_id})
        pcm_list = list(pcm_data)
        return self._CallRequestWithParamAndBin(ROBOT_API_ID_AUDIO_START_PLAY, param, pcm_list)

    def PlayStop(self, app_name: str) -> int:
        parameter = json.dumps({"app_name": app_name})
        code, data = self._Call(ROBOT_API_ID_AUDIO_STOP_PLAY, parameter)
        return 0

