#encoding = utf8
import logging

from asr import AsrObserver
from utils import AsyncTaskQueue

logger = logging.getLogger(__name__)


class NLPBase(AsrObserver):
    def __init__(self, split, callback=None):
        self._ask_queue = AsyncTaskQueue()
        self._split_handle = split
        self._callback = callback

    @property
    def callback(self):
        return self._callback

    @callback.setter
    def callback(self, value):
        self._callback = value

    def _on_callback(self, txt: str):
        if self._callback is not None:
            self._callback.on_message(txt)

    async def _request(self, question):
        pass

    async def _on_close(self):
        pass

    def process(self, message: str):
        pass

    def completed(self, message: str):
        logger.info(f'complete:{message}')
        self.ask(message)

    def ask(self, question):
        logger.info(f'ask:{question}')
        self._ask_queue.add_task(self._request, question)

    def stop(self):
        self._ask_queue.add_task(self._on_close)
        self._ask_queue.stop()

    def pause_talk(self):
        self._ask_queue.clear()
    