package com.ruoyi.aicall.controller;

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

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.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 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);
    }
}
