package main

import (
	"encoding/base64"
	"fmt"
	"github.com/manifoldco/promptui"
	"os"
	"path/filepath"
	"strings"
)

func img(video *Video) {
	tip("图片信息，可以是图片URL或图片在此电脑上的本地图片。")
	tip("图片URL：请确保图片URL可被访问。eg:https://ark-project.tos-cn-beijing.volces.com/doc_image/i2v_foxrgirl.png")
	tip("本地图片：请确保图片存在的jpg/jpge/png图片，路径名称请用英文，避免不必要的错误。eg:C:\\Users\\Administrator\\Desktop\\sucai\\fox.png")
	for {
		input("请设置图片信息: ")
		m, _ := reader.ReadString('\n')
		video.Url = strings.TrimSpace(m)
		if isURL(video.Url) { //判断是否为URL
			isImg, err := checkURL(video.Url)
			if !isImg {
				warning("URL内容不是图片，请检查URL是否正确！")
				continue
			}
			if err != nil {
				warning("URL不可访问，请检查网络或URL是否正确！")
				continue
			}
		}

		if isLocalPath(video.Url) {
			fileExt := strings.ToLower(filepath.Ext(video.Url))
			var mimeType string
			switch fileExt {
			case ".jpg", ".jpeg":
				mimeType = "jpeg"
			case ".png":
				mimeType = "png"
			default:
				warning("不支持的图片格式: %s", fileExt)
			}
			content, err := os.ReadFile(video.Url)
			if err != nil {
				warning("无法读取文件: %v", err)
				continue
			}
			// 将文件内容编码为Base64字符串
			dataURL := fmt.Sprintf("data:image/%s;base64,%s", mimeType, base64.StdEncoding.EncodeToString(content))
			video.Url = dataURL
		}
		break
	}
}

func desc(video *Video) {
	var d strings.Builder
	tip("描述信息，请输入一段描述文字，描述文字越多，生成视频越精彩。")
	input("请设置描述信息: ")
	text, _ := reader.ReadString('\n')
	d.WriteString(fmt.Sprintf("%s ", strings.TrimSpace(text)))
	d.WriteString("--watermark ")
	d.WriteString("false ")
	for {
		prompt := promptui.Select{
			Label: "分辨率: ",
			Items: []string{"480p", "720p"},
		}
		_, r, err := prompt.Run()
		if err != nil {
			warning("Prompt failed %v\n", err)
			continue
		}
		d.WriteString("--resolution ")
		d.WriteString(r)
		d.WriteString(" ")
		break
	}

	for {
		prompt := promptui.Select{
			Label: "视频时长(秒): ",
			Items: []string{"5", "10"},
		}
		_, r, err := prompt.Run()
		if err != nil {
			warning("Prompt failed %v\n", err)
			continue
		}
		d.WriteString("--dur ")
		d.WriteString(r)
		d.WriteString(" ")
		break
	}
	tip("adaptive：根据所上传图片的比例，自动选择最合适的宽高比。")
	for {
		prompt := promptui.Select{
			Label: "视频的宽高比例: ",
			Items: []string{"adaptive", "16:9", "9:16", "4:3", "3:4", "21:9", "9:21", "1:1"},
		}
		_, r, err := prompt.Run()
		if err != nil {
			warning("Prompt failed %v\n", err)
			continue
		}
		d.WriteString("--ratio ")
		d.WriteString(r)
		d.WriteString(" ")
		break
	}

	for {
		prompt := promptui.Select{
			Label: "帧率: ",
			Items: []string{"16", "24"},
		}
		_, r, err := prompt.Run()
		if err != nil {
			warning("Prompt failed %v\n", err)
			continue
		}
		d.WriteString("--framepersecond ")
		d.WriteString(r)
		d.WriteString(" ")
		break
	}

	for {
		prompt := promptui.Select{
			Label: "是否固定摄像头: ",
			Items: []string{"false", "true"},
		}
		_, r, err := prompt.Run()
		if err != nil {
			warning("Prompt failed %v\n", err)
			continue
		}
		d.WriteString("--camerafixed ")
		d.WriteString(r)
		d.WriteString(" ")
		break
	}
	video.Text = d.String()
}
