# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Modem grpc interface."""

from floss.pandora.server import bluetooth as bluetooth_module
import grpc
from pandora_experimental import modem_grpc_aio
from pandora_experimental import modem_pb2


class Modem(modem_grpc_aio.ModemServicer):
    """Service to trigger modem procedures.

    This class implements the Pandora bluetooth test interfaces,
    where the meta class definition is automatically generated by the protobuf.
    The interface definition can be found in:
    https://cs.android.com/android/platform/superproject/main/+/main:packages/modules/Bluetooth/pandora/interfaces/pandora_experimental/modem.proto
    """

    def __init__(self, bluetooth: bluetooth_module.Bluetooth):
        self.bluetooth = bluetooth

    async def Call(self, request: modem_pb2.CallRequest, context: grpc.ServicerContext) -> modem_pb2.CallResponse:
        phone_number = request.phone_number
        if phone_number is None or len(phone_number) == 0:
            await context.abort(grpc.StatusCode.INVALID_ARGUMENT, 'Cannot call empty number.')

        call_result = self.bluetooth.incoming_call(phone_number)
        if not call_result:
            await context.abort(grpc.StatusCode.INTERNAL, 'Failed to receive a call.')

        return modem_pb2.CallResponse()

    async def AnswerCall(self, request: modem_pb2.AnswerCallRequest,
                         context: grpc.ServicerContext) -> modem_pb2.AnswerCallResponse:
        context.set_code(grpc.StatusCode.UNIMPLEMENTED)  # type: ignore
        context.set_details('Method not implemented!')  # type: ignore
        raise NotImplementedError('Method not implemented!')

    async def Close(self, request: modem_pb2.CloseRequest, context: grpc.ServicerContext) -> modem_pb2.CloseResponse:
        context.set_code(grpc.StatusCode.UNIMPLEMENTED)  # type: ignore
        context.set_details('Method not implemented!')  # type: ignore
        raise NotImplementedError('Method not implemented!')
