package util

import (
	"bytes"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

var BaseHost = "https://arkids.net/items"

const ProxyHost = "http://127.0.0.1:7890"

var ArkItemIDs = make([]ArkItemID, 0)

func GetArkIds(u string, page int) error {
	fmt.Println("page:", u)
	req, err := http.NewRequest("GET", u, nil)
	if err != nil {
		log.Fatalf("创建请求失败: %v", err)
		return err
	}
	proxy, err := url.Parse(ProxyHost)
	if err != nil {
		log.Fatalf("Failed to parse proxy URL: %v", err)
		return err
	}
	transport := &http.Transport{
		Proxy: http.ProxyURL(proxy),
	}

	client := &http.Client{Transport: transport}
	resp, err := client.Do(req)
	if err != nil {
		log.Fatalf("发送请求失败: %v", err)
		return err
	}
	defer resp.Body.Close()

	responseBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("读取响应正文失败: %v", err)
		return err
	}

	// 使用 goquery 解析 HTML 内容
	doc, err := goquery.NewDocumentFromReader(bytes.NewReader(responseBody))
	if err != nil {
		log.Fatalf("解析 HTML 失败: %v", err)
		return err
	}
	var sign = 0
	// 查找所有 <h1> 标签并打印内容
	doc.Find("tr").Each(func(index int, item *goquery.Selection) {
		sign += 1
		aii := ArkItemID{}
		//ret, _ := item.Html()
		item.Find("img").Each(func(index int, img *goquery.Selection) {
			if img.AttrOr("data-src", "") != "" {
				aii.ImageURL = img.AttrOr("data-src", "")
			}
		})
		item.Find(".w-25 a").Each(func(index int, name *goquery.Selection) {
			aii.Name = name.Text()
		})
		item.Find(".ts.cbl").Each(func(index int, name *goquery.Selection) {
			aii.ID = name.Text()
		})
		item.Find(".cbl").Each(func(index int, name *goquery.Selection) {
			aii.Command = name.Text()
		})
		aii.Page = page
		ArkItemIDs = append(ArkItemIDs, aii)
		//fmt.Println(aii.String())
	})
	return nil
}

type ArkItemID struct {
	ID       string `json:"id"`        // id
	Name     string `json:"name"`      // 名称
	ImageURL string `json:"image_url"` // 图片地址
	Command  string `json:"command"`   // 命令
	Page     int    `json:"page"`      // 页码
}

func (a ArkItemID) String() string {
	return fmt.Sprint("ID:", a.ID, " Name:", a.Name, " Img:", a.ImageURL, " Command:", a.Command, " Page:", a.Page)
}
