import os
import uuid

from sziszapangma.integration.service_core.asr.asr_base_processor import AsrBaseProcessor
from sziszapangma.integration.service_core.asr.asr_result import AsrResult


class SpeechbrainAsrProcessor(AsrBaseProcessor):

    def process_asr(self, audio_file_path: str) -> AsrResult:
        file_tag = str(uuid.uuid4())
        file_extension = audio_file_path.split('.')[-1]
        file_name = f'{file_tag}.{file_extension}'
        result_file_path = f'processing_flask/{file_tag}.txt'
        file_path = f'processing_flask/{file_name}'
        # create file in /data/uuid.ext
        os.system(f"cp {audio_file_path} /data/{file_path}")

        command = f'/tools/Recognize/run.sh {file_path} {result_file_path}'
        print(f'run {command}')
        os.system(command)
        with open(f'/data/{result_file_path}', 'r') as f:
            transcription = f.read()

        transcription = transcription.replace('\n', ' ')

        # remove temp file
        os.remove(f'/data/{file_path}')
        os.remove(f'/data/{result_file_path}')

        return AsrResult(
            words=[it for it in transcription.split(' ')],
            full_text=transcription,
            words_time_alignment=None
        )


if __name__ == '__main__':
    SpeechbrainAsrProcessor().start_processor()