package main

import (
	_ "ARKItems/configs"
	configs "ARKItems/configs"
	"ARKItems/entity"
	"ARKItems/router"
	"ARKItems/util"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
)

func main() {
	RunGin()
	//ReSet()
}
func RunGin() {
	//ReSet()
	engine := gin.Default()
	engine.StaticFS("/static", http.Dir("file"))
	router.LoadRouter(engine)
	engine.Run(configs.Server.Host + ":" + configs.Server.Port)
}
func ReSet() {
	var items []entity.ArkItemInfo
	_, err := configs.Engine.Table("ark_item_info").FindAndCount(&items)
	if err != nil {
		panic(err)
		return
	}
	url := "D:\\project\\study\\ark-shop-tools\\file\\download.bat"
	m := make(map[string]string)
	readFile, err := ioutil.ReadFile(url)
	split := strings.Split(string(readFile), "\n")
	for i := range split {
		index := strings.Index(split[i], "\"")
		lastIndex := strings.LastIndex(split[i], "\"")
		name := split[i][index+1 : lastIndex]
		m[name] = split[i]
	}

	for i := range items {
		item := items[i]
		if strings.Index(item.ImgUrl, "/api") >= 0 {
			name := item.ImgUrl[23:]
			_, err := os.OpenFile("file/item-image/"+name, os.O_RDWR, 0666)
			if err != nil {
				u := m[name]
				ind := strings.Index(u, ".png/")
				last := strings.LastIndex(u, "-o")

				fmt.Println(u[:ind+4] + " " + u[last:])
			}
		}
	}
}
func Test() {
	var names []string
	err := configs.Engine.Table("ark_item_info").Cols("name").Find(&names)
	if err != nil {
		panic(err)
	}
	var nameList []string
	var labelList []string

	str := ""
	for i := range names {
		str = str + "," + names[i]
	}
	str = str[1:]
	request, err := util.SendOpenAIRequest(str)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("send:", str, "\n", "read:", request)
	nameList = append(nameList, str[0:])
	labelList = append(labelList, request)

	n := ""
	for i := 0; i < len(nameList); i++ {
		if i == 0 {
			n = nameList[0]
			continue
		}
		n = n + "," + nameList[i]
	}

	l := ""
	for i := 0; i < len(labelList); i++ {
		if i == 0 {
			l = labelList[0]
			continue
		}
		l = l + "," + labelList[i]
	}

	ioutil.WriteFile("resources/file1.txt", []byte(n), 0666)
	ioutil.WriteFile("resources/file2.txt", []byte(l), 0666)
	fmt.Println(len(str), len(names))
}

func Test01() {
	file, err := ioutil.ReadFile("resources/file1.txt")
	if err != nil {
		panic(err)
	}
	str := string(file)
	split := strings.Split(str, ",")
	m := make(map[string]string)
	for i := range split {
		key := strings.TrimSpace(split[i])
		m[key] = ""
	}
	file2, err := ioutil.ReadFile("resources/file2.txt")
	if err != nil {
		panic(err)
	}
	split2 := strings.Split(string(file2), "，")
	other := ""
	for i := range split2 {
		kv := strings.Split(split2[i], "：")
		if len(kv) != 2 {
			fmt.Println("+!?", split2[i])
			continue
		}
		key := strings.TrimSpace(kv[0])
		value := strings.TrimSpace(kv[1])
		if _, ok := m[key]; ok {
			//lm := map[string]interface{}{
			//	"label": value,
			//}
			//_, err := configs.Engine.Table("ark_item_info").Where("name = ? and label", key).
			//	Update(lm)
			//if err != nil {
			//	fmt.Println(err)
			//}
			m[key] = value
		} else {
			other += kv[0] + ":" + kv[1] + "\n"
		}
	}
	fmt.Println(other)

}
