package utils

import (
	jsoniter "github.com/json-iterator/go"
	"wireguard-ui/global/client"
	"wireguard-ui/http/vo"
)

type dailyPoetry struct{}

func DailyPoetry() dailyPoetry {
	return dailyPoetry{}
}

// HitokotoPoetry
// @description: 获取每日一句【一言】
// @receiver dailyPoetry
// @return data
// @return err
func (dailyPoetry) HitokotoPoetry() (data *vo.Poetry, err error) {
	req := client.HttpClient.R()
	req.SetQueryParam("c", "h")
	req.SetQueryParam("c", "i")
	req.SetQueryParam("c", "k")
	req.SetQueryParam("encode", "json")
	req.SetQueryParam("charset", "utf-8")

	response, err := req.Get("https://hitokoto.mrxqq.top")
	if err != nil {
		return nil, err
	}

	author := jsoniter.Get(response.Body(), "from_who").ToString()
	if author == "" {
		author = "佚名"
	}

	data = &vo.Poetry{
		Content: jsoniter.Get(response.Body(), "hitokoto").ToString(),
		Author:  author,
	}

	return
}
