package com.ruoyi.aicall.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aicall.domain.CcAsrLanguages;
import com.ruoyi.aicall.service.ICcAsrLanguagesService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.aicall.domain.CcTtsAliyun;
import com.ruoyi.aicall.service.ICcTtsAliyunService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 阿里云音色列Controller
 * 
 * @author ruoyi
 * @date 2025-05-29
 */
@Controller
@RequestMapping("/aicall/ttsAliyun")
public class CcTtsAliyunController extends BaseController
{
    private String prefix = "aicall/ttsAliyun";

    @Autowired
    private ICcTtsAliyunService ccTtsAliyunService;
    @Autowired
    private ICcAsrLanguagesService asrLanguagesService;
    @Autowired
    private ISysConfigService sysConfigService;

    @RequiresPermissions("aicall:ttsAliyun:view")
    @GetMapping()
    public String ttsAliyun()
    {
        return prefix + "/ttsAliyun";
    }

    /**
     * 查询阿里云音色列列表
     */
    @RequiresPermissions("aicall:ttsAliyun:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CcTtsAliyun ccTtsAliyun)
    {
        startPage();
        List<CcTtsAliyun> list = ccTtsAliyunService.selectCcTtsAliyunList(ccTtsAliyun);
        return getDataTable(list);
    }

    /**
     * 导出阿里云音色列列表
     */
    @RequiresPermissions("aicall:ttsAliyun:export")
    @Log(title = "阿里云音色列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CcTtsAliyun ccTtsAliyun)
    {
        List<CcTtsAliyun> list = ccTtsAliyunService.selectCcTtsAliyunList(ccTtsAliyun);
        ExcelUtil<CcTtsAliyun> util = new ExcelUtil<CcTtsAliyun>(CcTtsAliyun.class);
        return util.exportExcel(list, "阿里云音色列数据");
    }

    /**
     * 新增阿里云音色列
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存阿里云音色列
     */
    @RequiresPermissions("aicall:ttsAliyun:add")
    @Log(title = "阿里云音色列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CcTtsAliyun ccTtsAliyun)
    {
        return toAjax(ccTtsAliyunService.insertCcTtsAliyun(ccTtsAliyun));
    }

    /**
     * 修改阿里云音色列
     */
    @RequiresPermissions("aicall:ttsAliyun:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CcTtsAliyun ccTtsAliyun = ccTtsAliyunService.selectCcTtsAliyunById(id);
        mmap.put("ccTtsAliyun", ccTtsAliyun);
        return prefix + "/edit";
    }

    /**
     * 修改保存阿里云音色列
     */
    @RequiresPermissions("aicall:ttsAliyun:edit")
    @Log(title = "阿里云音色列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CcTtsAliyun ccTtsAliyun)
    {
        return toAjax(ccTtsAliyunService.updateCcTtsAliyun(ccTtsAliyun));
    }

    /**
     * 删除阿里云音色列
     */
    @RequiresPermissions("aicall:ttsAliyun:remove")
    @Log(title = "阿里云音色列", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ccTtsAliyunService.deleteCcTtsAliyunByIds(ids));
    }

    /**
     * 查询全部音色列表
     */
    @GetMapping("/all")
    @ResponseBody
    public AjaxResult all()
    {
        List<CcTtsAliyun> list = ccTtsAliyunService.selectCcTtsAliyunList(new CcTtsAliyun());
        return AjaxResult.success(list);
    }


    /**
     * 查询全部provider列表
     */
    @GetMapping("/provider/all")
    @ResponseBody
    public AjaxResult allProvider()
    {
        List<CcTtsAliyun> list = ccTtsAliyunService.selectCcTtsAliyunList(new CcTtsAliyun());
        List<String> providerList = new ArrayList<>();
        for (CcTtsAliyun ccTtsAliyun: list) {
            if (!providerList.contains(ccTtsAliyun.getProvider().trim())) {
                providerList.add(ccTtsAliyun.getProvider().trim());
            }
        }
        return AjaxResult.success(providerList);
    }

//    /**
//     * 根据provider查询音色列表
//     */
//    @GetMapping("/getByProvider")
//    @ResponseBody
//    public AjaxResult getByProvider(@RequestParam String provider)
//    {
//        List<CcTtsAliyun> list = ccTtsAliyunService.selectCcTtsAliyunList(new CcTtsAliyun().setProvider(provider));
//        return AjaxResult.success(list);
//    }

    /**
     * 根据provider查询音色列表
     */
    @GetMapping("/getByVoiceSource")
    @ResponseBody
    public AjaxResult getByVoiceSource(@RequestParam String voiceSource)
    {
        List<CcTtsAliyun> list = ccTtsAliyunService.selectCcTtsAliyunList(new CcTtsAliyun().setVoiceSource(voiceSource));
        return AjaxResult.success(list);
    }


    /**
     * 查询全部ttsprovider列表
     */
    @GetMapping("/tts/voiceSource/all")
    @ResponseBody
    public AjaxResult allTtsProvider()
    {
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigKey("config_tts_provider");
        List<SysConfig> list = sysConfigService.selectConfigList(sysConfig);
        Map<String, String> providerMap = new HashMap<>();
        for (SysConfig data: list) {
            providerMap.put(data.getConfigValue(), data.getConfigName());
        }
        return AjaxResult.success(providerMap);
    }

    /**
     * 查询全部asrprovider列表
     */
    @GetMapping("/asr/provider/all")
    @ResponseBody
    public AjaxResult allAsrProvider()
    {
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigKey("config_asr_provider");
        List<SysConfig> list = sysConfigService.selectConfigList(sysConfig);
        Map<String, String> providerMap = new HashMap<>();
        for (SysConfig data: list) {
            providerMap.put(data.getConfigValue(), data.getConfigName());
        }
        return AjaxResult.success(providerMap);
    }



    /**
     * 根据asr厂商获取语言
     */
    @GetMapping("/asr/language")
    @ResponseBody
    public AjaxResult getAsrLanguages(@RequestParam String asrProvider)
    {
        List<JSONObject> languageList = new ArrayList<>();
        Map<String, String> languageCodeMap = new HashMap<>();
        List<CcAsrLanguages> asrLanguagesList = asrLanguagesService.selectCcAsrLanguagesList(new CcAsrLanguages().setAsrProvider(asrProvider));
        for (CcAsrLanguages asrLanguages: asrLanguagesList) {
            if (StringUtils.isNotEmpty(asrLanguages.getLanguageCode())
                    && StringUtils.isEmpty(languageCodeMap.get(asrLanguages.getLanguageCode()))) {
                languageCodeMap.put(asrLanguages.getLanguageCode(), asrLanguages.getLanguageName());
            }
        }
        if (CollectionUtils.isEmpty(languageCodeMap)) {
            languageCodeMap.put("zh-CN", "中文");
        }
        for (String key: languageCodeMap.keySet()) {
            JSONObject obj = new JSONObject();
            obj.put("key", key);
            obj.put("value", languageCodeMap.get(key));
            languageList.add(obj);
        }
        return AjaxResult.success(languageList);
    }
    /**
     * 根据tts厂商获取支持语言
     */
    @GetMapping("/tts/language")
    @ResponseBody
    public AjaxResult getTtsLanguages(@RequestParam String voiceSource)
    {
        List<JSONObject> languageList = new ArrayList<>();
        Map<String, String> languageCodeMap = new HashMap<>();
        List<CcTtsAliyun> ccTtsAliyunList = ccTtsAliyunService.selectCcTtsAliyunList(new CcTtsAliyun().setVoiceSource(voiceSource));
        for (CcTtsAliyun ccTtsAliyun: ccTtsAliyunList) {
            if (StringUtils.isNotEmpty(ccTtsAliyun.getLanguageCode())
                    && StringUtils.isEmpty(languageCodeMap.get(ccTtsAliyun.getLanguageCode()))) {
                languageCodeMap.put(ccTtsAliyun.getLanguageCode(), ccTtsAliyun.getLanguageName());
            }
        }
        if (CollectionUtils.isEmpty(languageCodeMap)) {
            languageCodeMap.put("zh-CN", "中文");
        }
        for (String key: languageCodeMap.keySet()) {
            JSONObject obj = new JSONObject();
            obj.put("key", key);
            obj.put("value", languageCodeMap.get(key));
            languageList.add(obj);
        }
        return AjaxResult.success(languageList);
    }


    /**
     * 根据provider和语言查询音色列表
     */
    @GetMapping("/getByLanguageCode")
    @ResponseBody
    public AjaxResult getByLanguageCode(@RequestParam String voiceSource,
                                        @RequestParam(value = "ttsLanguageCode", required = false) String ttsLanguageCode,
                                        @RequestParam(value = "ttsModels", required = false) String ttsModels)
    {
        CcTtsAliyun ccTtsAliyun = new CcTtsAliyun();
        ccTtsAliyun.setVoiceSource(voiceSource);
        if (StringUtils.isNotEmpty(ttsLanguageCode)) {
            ccTtsAliyun.setLanguageCode(ttsLanguageCode);
        }
        if (StringUtils.isNotEmpty(ttsModels)) {
            ccTtsAliyun.setTtsModels(ttsModels);
        }
        List<CcTtsAliyun> list = ccTtsAliyunService.selectCcTtsAliyunList(ccTtsAliyun);
        return AjaxResult.success(list);
    }


    /**
     * 根据asr厂商和语言获取asr模型列表
     */
    @GetMapping("/asr/models")
    @ResponseBody
    public AjaxResult getAsrModels(@RequestParam String asrProvider, @RequestParam String asrLanguageCode)
    {
        List<JSONObject> modelsList = new ArrayList<>();
        Map<String, String> asrModelsMap = new HashMap<>();
        List<CcAsrLanguages> asrLanguagesList = asrLanguagesService.selectCcAsrLanguagesList(new CcAsrLanguages().setAsrProvider(asrProvider).setLanguageCode(asrLanguageCode));
        for (CcAsrLanguages asrLanguages: asrLanguagesList) {
            if (StringUtils.isNotEmpty(asrLanguages.getModels())
                    && StringUtils.isEmpty(asrModelsMap.get(asrLanguages.getModels()))) {
                asrModelsMap.put(asrLanguages.getModels(), asrLanguages.getModels());
            }
        }
        if (CollectionUtils.isEmpty(asrModelsMap)) {
            asrModelsMap.put("", "");
        }
        for (String key: asrModelsMap.keySet()) {
            JSONObject obj = new JSONObject();
            obj.put("key", key);
            obj.put("value", asrModelsMap.get(key));
            modelsList.add(obj);
        }
        return AjaxResult.success(modelsList);
    }
    /**
     * 根据tts厂商和语言获取asr模型列表
     */
    @GetMapping("/tts/models")
    @ResponseBody
    public AjaxResult getTtsModels(@RequestParam String voiceSource, @RequestParam String ttsLanguageCode)
    {
        List<JSONObject> modelsList = new ArrayList<>();
        Map<String, String> ttsModelMap = new HashMap<>();
        List<CcTtsAliyun> ccTtsAliyunList = ccTtsAliyunService.selectCcTtsAliyunList(new CcTtsAliyun().setVoiceSource(voiceSource).setLanguageCode(ttsLanguageCode));
        for (CcTtsAliyun ccTtsAliyun: ccTtsAliyunList) {
            if (StringUtils.isNotEmpty(ccTtsAliyun.getTtsModels())
                    && StringUtils.isEmpty(ttsModelMap.get(ccTtsAliyun.getTtsModels()))) {
                ttsModelMap.put(ccTtsAliyun.getTtsModels(), ccTtsAliyun.getTtsModels());
            }
        }
        if (CollectionUtils.isEmpty(ttsModelMap)) {
            ttsModelMap.put("", "");
        }
        for (String key: ttsModelMap.keySet()) {
            JSONObject obj = new JSONObject();
            obj.put("key", key);
            obj.put("value", ttsModelMap.get(key));
            modelsList.add(obj);
        }
        return AjaxResult.success(modelsList);
    }
}
