package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

// 定义 VoiceModelSet 结构体来存储每个模型的路径
type VoiceModelSet struct {
	GPTModelPath    string `json:"gpt_model_path"`
	SoVITSModelPath string `json:"sovits_model_path"`
}

// 定义 VoiceModels 结构体储存所有的 VoiceModelSet
type VoiceModels struct {
	VoiceModelSets map[string]VoiceModelSet `json:"voice_model_sets"`
}

// 方法，根据人名获取模型路径
func (vm *VoiceModels) GetModelPathsByName(name string) (string, string, bool) {
	if modelSet, exists := vm.VoiceModelSets[name]; exists {
		return modelSet.GPTModelPath, modelSet.SoVITSModelPath, true
	}
	return "", "", false
}

func VoiceInference(text, textLanguage string) (string, error) {
	// 构建请求的URL
	url := "http://58.34.54.45:17666/"

	// 构建请求的参数
	requestBody, err := json.Marshal(
		map[string]string{
			"refer_wav_path":  "/psycheEpic/GPT-SoVITS/voices/huainvren_1.wav",
			"prompt_text":     "什么意思呀，他不播了是吧？",
			"prompt_language": "zh",
			"text":            text,
			"text_language":   textLanguage,
		})
	if err != nil {
		return "", err
	}

	// 构建HTTP请求
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
	if err != nil {
		return "", err
	}
	req.Header.Set("Content-Type", "application/json")

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	// 读取响应体
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	return string(body), nil
}

func main() {
	// 假定 vm 是从某处获取的模型路径数据
	vm := VoiceModels{
		VoiceModelSets: map[string]VoiceModelSet{
			"huaiNvRen": {
				GPTModelPath:    "/psycheEpic/GPT-SoVITS/GPT_weights/huainvren-e20.ckpt",
				SoVITSModelPath: "/psycheEpic/GPT-SoVITS/SoVITS_weights/huainvren_e51_s816.pth",
			},
			"leiDianJiangJun": {
				GPTModelPath:    "/psycheEpic/GPT-SoVITS/GPT_weights/leidianjiangjun-e15.ckpt",
				SoVITSModelPath: "/psycheEpic/GPT-SoVITS/SoVITS_weights/leidianjiangjun_e8_s200.pth",
			},
			"ZhongLi": {
				GPTModelPath:    "/psycheEpic/GPT-SoVITS/GPT_weights/zhongli-e30.ckpt",
				SoVITSModelPath: "/psycheEpic/GPT-SoVITS/SoVITS_weights/zhongli_e16_s432.pth",
			},
		},
	}

	// 示例：获取 "huaiNvRen" 的模型路径
	gptPath, sovitsPath, found := vm.GetModelPathsByName("huaiNvRen")
	if found {
		fmt.Printf("GPT Model Path: %s\nSoVITS Model Path: %s\n", gptPath, sovitsPath)
	} else {
		fmt.Println("Model not found.")
	}
}
