package main

import (
	_ "ARKItems/configs"
	configs "ARKItems/configs"
	"ARKItems/router"
	"ARKItems/util"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"time"
)

func main() {
	//加载配置文件
	router.ARKConfig = util.LoadArkShopConfig("resources/config.json")
	engine := gin.Default()
	router.LoadRouter(engine)
	engine.StaticFS("/static", gin.Dir("file", true))
	engine.Run(configs.Server.Host + ":" + configs.Server.Port)
}

func Test() {
	//读取内容
	readFile, err := ioutil.ReadFile("arkids.json")
	if err != nil {
		panic(err)
	}
	arr := make([]util.ArkItemID, 0)
	json.Unmarshal(readFile, &arr)
	m := make(map[int]int)

	for i := range arr {
		_, ok := m[arr[i].Page]
		if ok {
			m[arr[i].Page] += 1
		} else {
			m[arr[i].Page] = 1
		}
	}
	for i := range m {
		fmt.Println("page:", i, " size:", m[i])
	}
}

func GetArkIds() {
	for i := 0; i < 42; i++ {
		var err error
		var sign = true
		for sign || err != nil {
			sign = false
			if i == 0 {
				err = util.GetArkIds(util.BaseHost, i+1)
			} else {
				err = util.GetArkIds(util.BaseHost+"/page/"+strconv.FormatInt(int64(i+1), 10), i+1)
			}
		}
	}
	file, err := os.OpenFile("arkids.json", os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	marshal, err := json.Marshal(util.ArkItemIDs)
	if err != nil {
		panic(err)
	}
	file.Write(marshal)
}

var sign = make(chan int)

// BatchDownloadImage 批量下载网络图片
func BatchDownloadImage() {
	//读取内容
	readFile, err := ioutil.ReadFile("arkids.json")
	if err != nil {
		panic(err)
	}
	arr := make([]util.ArkItemID, 0)
	json.Unmarshal(readFile, &arr)
	for i := range arr {
		var item = arr[i]
		index := strings.LastIndex(item.ImageURL, "/")
		name := item.ImageURL[index+1:]
		url := "https://arkids.net" + item.ImageURL
		go DownloadImage(url, name)
	}
	for {
		time.Sleep(time.Millisecond * 10)
		num := <-sign
		if num == len(arr) {
			fmt.Println("所有图片下载指令执行完成")
			time.Sleep(time.Second * 5)
			break
		}
	}

}
func DownloadImage(url, name string) {
	file, err2 := os.OpenFile("file/"+name, os.O_RDONLY, 0666)
	if err2 == nil {
		file.Close()
		//判断文件大小
		bytes := make([]byte, 0)
		file.Read(bytes)
		if len(bytes) == 170 {
			fmt.Println("失败的图片:", name, "下载失败")
		} else {
			num := <-sign
			sign <- num + 1
			return
		}
	}

	fmt.Println("调用指令", "curl", " -x", " http://127.0.0.1:7890/", " -o", " file/"+name, " ", url)
	//调用指令
	cmd := exec.Command("curl", "-x", "http://127.0.0.1:7890/", "-o", "file/"+name, url)
	// 执行命令
	err := cmd.Run()
	if err != nil {
		fmt.Printf("Error executing curl command: %v\n", err)
		return
	}
	fmt.Println("文件", name, "下载完成")
	num := <-sign
	sign <- num + 1
}
