package magento

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

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

	"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)
	doc.Find("meta").Each(func(i int, s *goquery.Selection) {
		v, _ := s.Attr("property")
		if v == "og:description" {
			item.ShortDesc = s.AttrOr("content", "")
		} else if v == "og:title" {
			item.ItemName = s.AttrOr("content", "")
		} else if v == "og:image" {
			item.Images = append(item.Images, s.AttrOr("content", ""))
		} else if v == "product:price:amount" {
			item.SalesPrice = util.GetFloat32(s.AttrOr("content", ""))
		} else if v == "product:price:currency" {
			item.Currency = s.AttrOr("content", "")
		}
	})
	item.Emails = common.GetEmails(body)
	return
}
