# coding=utf-8

from typing import Type

from django.utils.translation import gettext_lazy as _
from rest_framework import serializers

from application.flow.common import WorkflowMode
from application.flow.i_step_node import INode, NodeResult


class SpeechToTextNodeSerializer(serializers.Serializer):
    stt_model_id = serializers.CharField(required=False, allow_blank=True, allow_null=True, label=_("Model id"))
    stt_model_id_type = serializers.CharField(required=False, default='custom', label=_("Model id type"))
    stt_model_id_reference = serializers.ListField(required=False, child=serializers.CharField(), allow_empty=True,
                                                   label=_("Reference Field"))
    is_result = serializers.BooleanField(required=False,
                                         label=_('Whether to return content'))

    audio_list = serializers.ListField(required=True,
                                       label=_("The audio file cannot be empty"))
    model_params_setting = serializers.DictField(required=False,
                                                 label=_("Model parameter settings"))


class ISpeechToTextNode(INode):
    type = 'speech-to-text-node'
    support = [WorkflowMode.APPLICATION, WorkflowMode.APPLICATION_LOOP, WorkflowMode.KNOWLEDGE,
               WorkflowMode.KNOWLEDGE_LOOP,WorkflowMode.TOOL,WorkflowMode.TOOL_LOOP]

    def get_node_params_serializer_class(self) -> Type[serializers.Serializer]:
        return SpeechToTextNodeSerializer

    def _run(self):
        res = self.workflow_manage.get_reference_field(self.node_params_serializer.data.get('audio_list')[0],
                                                       self.node_params_serializer.data.get('audio_list')[1:])
        for audio in res:
            if 'file_id' not in audio:
                raise ValueError(
                    _("Parameter value error: The uploaded audio lacks file_id, and the audio upload fails"))

        return self.execute(audio=res, **self.node_params_serializer.data, **self.flow_params_serializer.data)

    def execute(self, stt_model_id,
                audio, model_params_setting=None, stt_model_id_type=None, stt_model_id_reference=None,
                **kwargs) -> NodeResult:
        pass
