package com.icomsys.main_vm.biz.rcp.learningTest.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import selvytts.Pttsnet;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class LearningTestInTtsService {

    @Value("${feign.url.selvas}")
    private String ttsServer;

    @Value("${feign.url.ttsport}")
    private String ttsPort;

    @Value("${file.path}")
    private String filePath;

    /**
     * @Name speechSynthesis
     * @Description
     * @Author jun
     * @CreateDate 2023. 3. 10.
     *
     * @return
     *
     * @ChangeDescription
     */
    public String ttsTestApi(Map<String, String> tMap) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar c1 = Calendar.getInstance();

        int result = -1;    // 0 : 성공, 0 미만 : 실패
        String localFileFolderName = ttsFilepath(sdf.format(c1.getTime()));
        String localFilePathName = ttsFileName();

        String hostIP = ttsServer;
        String hostPort = ttsPort;
        int connection_timeout = 60;
        int read_timeout = 60;
        String talkText = (String) tMap.get("talkText");
        String resultFilePath = localFileFolderName + "/" + localFilePathName;
        String pHighlight = null;
        String pLipsync = null;
        int nLanguage = 0;
        int nSpeakerid = 8;
        int sformat = Pttsnet.PTTSNET_FMT8K_16BIT_WAVE;    // 16bit Linear PCM
        int pitch = Integer.parseInt((String) tMap.get("pitch"));
        int speed = Integer.parseInt((String) tMap.get("speed"));
        int volume = Integer.parseInt((String) tMap.get("volume"));
        int nFlag = 0x00;
        int content_type = Pttsnet.PTTSNET_CONTENT_PLAIN;
        int charset = Pttsnet.PTTSNET_CHARSET_UTF8;
        int vc_type = -1;
        String audioFile = null;
        int audio_offset = Pttsnet.PTTSNET_AUDIO_MIX;


        File Folder = new File(localFileFolderName);
        if (!Folder.exists()) {
            try {
                Folder.mkdirs();
            } catch (Exception e) {
                log.debug("error_mkdir");
                e.getStackTrace();
            }
        }

        try {
            result = new Pttsnet().PTTSNET_FILE_A_EX(hostIP, hostPort, connection_timeout, read_timeout, talkText
                    , resultFilePath, pHighlight, pLipsync, nLanguage, nSpeakerid, sformat, pitch
                    , speed, volume, nFlag, content_type, charset, vc_type, audioFile, audio_offset);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return result == 0 ? serverFilepath(localFilePathName, sdf.format(c1.getTime())) : "";
    }

    private String ttsFilepath(String date) {
        return filePath + "ttstest/" + date;
    }

    private String ttsFileName() {
        return UUID.randomUUID().toString().replaceAll("-", "") + ".wav";
    }

    private String serverFilepath(String fileName, String date) {
        InetAddress local;
        String ip = "";
        try {
            local = InetAddress.getLocalHost();
            ip = local.getHostAddress();
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }

        return "http://" + ip + ":8050/files/ttstest/" + date +"/" + fileName;
    }
}
