package young

import (
	"kkscrap-go/controllers/scraper/common"
	"kkscrap-go/model"
	"strings"

	util "kkscrap-go/controllers/scraper/util"
	"regexp"

	"github.com/PuerkitoBio/goquery"
)

func Parse(body string, item *model.ItemInfo) {
	item.Language = common.GetLanguage(body)

	doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
	util.CheckError(err)

	if len(doc.Find("#sit_title").Nodes) > 0 {
		item.ItemName = doc.Find("#sit_title").Nodes[0].FirstChild.Data
	}
	item.ShortDesc = doc.Find("#sit_desc").Text()
	item.SalesPrice = util.GetFloat32(doc.Find("#it_price").AttrOr("value", ""))
	item.Currency = getCurrency(body)
	item.Images = append(item.Images, doc.Find("#sit_pvi img").AttrOr("src", ""))

	return
}

func getLanguage(body string) string {
	reg, _ := regexp.Compile("var gdLocale = '(.*)';")
	ss := reg.FindStringSubmatch(body)
	return ss[1]
}

func getCurrency(body string) string {
	return "KRW"
}

func getPrice(body string) string {
	//`#it_price`
	reg, _ := regexp.Compile("'setGoodsPrice'[ ]*:[ ]*'(.*)'")
	ss := reg.FindStringSubmatch(body)
	return ss[1]
}
