package utils

import (
	"encoding/json"
	"fmt"
	"github.com/sirupsen/logrus"
	"io"
	"net/http"
	"net/url"
)

func CheckSudoku(sudoku string) (error, ResSudoku) {
	resp, err := http.Get(fmt.Sprintf("https://git-ylsa0.cn/api_ylsa/get_sudoku_check/?sudoku=%s", url.QueryEscape(sudoku)))
	if err != nil {
		logrus.Errorln("请求get_sudoku_check接口失败：", err)
		return err, ResSudoku{}
	}
	defer func(Body io.ReadCloser) {
		err = Body.Close()
		if err != nil {
			logrus.Errorln(err.Error())
		}
	}(resp.Body)
	body, _ := io.ReadAll(resp.Body)
	var r ResSudoku
	//解析json结构
	err = json.Unmarshal(body, &r)
	if err != nil {
		logrus.Errorln("请求结果json解析失败：", err)
		return err, ResSudoku{}
	}
	logrus.Infoln("数独检查完成")
	return nil, r
}
