from pydantic import BaseModel, Field, validator
from typing import List, Optional, Callable, Any
from .audio import AudioBinary_data_list, _AudioBinary_data


class VAD_Functor_result(BaseModel):
    """
    VADFunctor结果
    """

    audiobinary_data_list: AudioBinary_data_list = Field(description="音频数据列表")
    audiobinary_index: int = Field(description="音频数据索引")
    audiobinary_data: _AudioBinary_data = Field(
        description="音频数据, 指向AudioBinary_data"
    )
    start_time: int = Field(description="开始时间", is_required=True)
    end_time: int = Field(description="结束时间", is_required=True)

    @validator("audiobinary_data_list")
    def validate_audiobinary_data_list(cls, v):
        if not isinstance(v, AudioBinary_data_list):
            raise ValueError("audiobinary_data_list必须是AudioBinary_data_list类型")
        return v

    @validator("audiobinary_index")
    def validate_audiobinary_index(cls, v):
        if not isinstance(v, int):
            raise ValueError("audiobinary_index必须是int类型")
        if v < 0:
            raise ValueError("audiobinary_index必须大于0")
        return v

    @validator("audiobinary_data")
    def validate_audiobinary_data(cls, v):
        if not isinstance(v, _AudioBinary_data):
            raise ValueError("audiobinary_data必须是AudioBinary_data类型")
        return v

    @validator("start_time")
    def validate_start_time(cls, v):
        if not isinstance(v, int):
            raise ValueError("start_time必须是int类型")
        if v < 0:
            raise ValueError("start_time必须大于0")
        return v

    @validator("end_time")
    def validate_end_time(cls, v, values):
        if not isinstance(v, int):
            raise ValueError("end_time必须是int类型")
        if "start_time" in values and v <= values["start_time"]:
            raise ValueError("end_time必须大于start_time")
        return v

    @classmethod
    def create_from_push_data(
        cls,
        audiobinary_data_list: AudioBinary_data_list,
        data: Any,
        start_time: int,
        end_time: int,
    ):
        """
        创建VAD片段
        """
        index = audiobinary_data_list.push_data(data)

        return cls(
            audiobinary_data_list=audiobinary_data_list,
            audiobinary_index=index,
            audiobinary_data=audiobinary_data_list[index],
            start_time=start_time,
            end_time=end_time,
        )

    def __len__(self):
        """
        获取音频数据长度
        """
        return len(self.audiobinary_data.binary_data)

    def __str__(self):
        """
        字符串展示内容
        """
        tostr = f"audiobinary_data_index: {self.audiobinary_index}\n"
        tostr += f"start_time: {self.start_time}\n"
        tostr += f"end_time: {self.end_time}\n"
        tostr += f"data_length: {len(self.audiobinary_data.binary_data)}\n"
        tostr += f"data_type: {type(self.audiobinary_data.binary_data)}\n"
        return tostr
