package nhentai

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"watn3y/bloaterbotv3/config"
)

func doAPIRequest(hentaiID string) (exists bool, Details nhentaiResponse) {

	client := &http.Client{}

	req, err := http.NewRequest("GET", config.BotConfig.Nhentai.APIEndpoint+"/gallery/"+hentaiID, nil)
	if err != nil {
		log.Fatalln(err)
	}

	req.Header.Set("User-Agent", "Golang_Spider_Bot/3.0")

	resp, err := client.Do(req)
	if err != nil {
		log.Fatalln(err)
	}

	if resp.StatusCode == 404 {
		return false, nhentaiResponse{}
	} else if resp.StatusCode != 200 {
		return false, nhentaiResponse{}
	}

	defer resp.Body.Close()
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatalln(err)
	}

	var hentai nhentaiResponse
	if err := json.Unmarshal(body, &hentai); err != nil { // Parse []byte to the go struct pointer
		fmt.Println("Can not unmarshal JSON")
	}

	return true, hentai

}

func parseAPIResponse(rawHentai nhentaiResponse) (formattedHentai hentai) {
	var hentai = hentai{
		ID:         rawHentai.ID,
		Title:      rawHentai.Title.Pretty,
		UploadDate: rawHentai.UploadDate,
	}

	for _, tag := range rawHentai.Tags {
		if tag.Type == "tag" {
			hentai.Tags = append(hentai.Tags, struct {
				Name string
				URL  string
			}{Name: tag.Name, URL: tag.URL})

		} /* else if tag.Type == "parody" {

		} else if tag.Type == "character" {

		} else if tag.Type == "language" {

		} else if tag.Type == "group" {

		} else if tag.Type == "artist" {

		} else if tag.Type == "category" {

		}*/

	}

	return hentai

}