package godo

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

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

	"github.com/PuerkitoBio/goquery"
)

func Parse(body string, item *model.ItemInfo) {
	item.Language = 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: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.OriginDesc, _ = doc.Find("div.detail_cont").Html()
	item.ItemCategory = doc.Find("div.location_tit").Text()
	item.DeliveryPrice = util.GetPrice(doc.Find("dl.item_delivery>dd>strong").Text())

	doc.Find("select.chosen-select").Each(func(i int, selection *goquery.Selection) {
		opt := model.Option{}
		selection.Find("option").Each(func(i int, selection *goquery.Selection) {
			opt.Choices = append(opt.Choices, model.Choice{Name: selection.Text()})
		})
		item.Options = append(item.Options, opt)
	})

	doc.Find("li>a>img.middle").Each(func(i int, selection *goquery.Selection) {
		img, ok := selection.Attr("src")
		if ok {
			item.Images = append(item.Images, img)
		}
	})

	item.SalesPrice = getPrice(body)
	item.Currency = getCurrency(body)
	item.Emails = common.GetEmails(body)

	return
}

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

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

func getPrice(body string) float32 {
	reg, _ := regexp.Compile("'setGoodsPrice'[ ]*:[ ]*'(.*)'")
	ss := reg.FindStringSubmatch(body)
	return util.GetFloat32(ss[1])
}
