package pack

import (
	"encoding/json"
	"github.com/caixw/lib.go/conv"
	"github.com/tidwall/gjson"
	"strconv"
	"youngee_b_api/consts"
	"youngee_b_api/model/gorm_model"
	"youngee_b_api/model/http_model"
)

func GetSelectionSquareCondition(req http_model.GetAllSelectionRequest) http_model.SelectionSquareCondition {
	return http_model.SelectionSquareCondition{
		ProductType: req.ProductType,
		Platform:    req.Platform,
		SampleMode:  req.SampleMode,
		TaskMode:    req.TaskMode,
	}
}

func GormSelectionListToSelectionBriefInfoList(gormSelectionInfos []*gorm_model.YounggeeSelectionInfo) []*http_model.SelectionBriefInfo {
	var httpSelectionPreviews []*http_model.SelectionBriefInfo
	for _, gormSelectionInfo := range gormSelectionInfos {
		selectionBriefInfo := GormSelectionToSelectionBriefInfo(gormSelectionInfo)
		httpSelectionPreviews = append(httpSelectionPreviews, selectionBriefInfo)
	}
	return httpSelectionPreviews
}

func GormSelectionToSelectionBriefInfo(selectionInfo *gorm_model.YounggeeSelectionInfo) *http_model.SelectionBriefInfo {
	icon := consts.GetPaltformIcon(selectionInfo.Platform)
	productPrice := conv.MustString(gjson.Get(selectionInfo.ProductSnap, "ProductPrice"), "")
	price, _ := strconv.ParseFloat(productPrice, 64)
	estimatedIncome := price * float64(selectionInfo.CommissionRate) / 100
	var photoUrl string
	var p []gorm_model.YounggeeProductPhoto
	_ = json.Unmarshal([]byte(selectionInfo.ProductPhotoSnap), &p)
	for _, v := range p {
		if v.Symbol == 1 {
			photoUrl = v.PhotoUrl
		}
	}
	return &http_model.SelectionBriefInfo{
		SelectionId:   selectionInfo.SelectionID,
		SelectionName: selectionInfo.SelectionName,
		Platform:      selectionInfo.Platform,
		//ProductSnap:      selectionInfo.ProductSnap,
		PlatformIcon: icon,
		ProductPrice: price,
		//ProductPhotoSnap: selectionInfo.ProductPhotoSnap,
		EstimatedIncome: estimatedIncome,
		CommissionRate:  selectionInfo.CommissionRate,
		SampleMode:      selectionInfo.SampleMode,
		TaskMode:        selectionInfo.TaskMode,
		PhotoUrl:        photoUrl,
		TaskReward:      conv.MustFloat64(selectionInfo.TaskReward, 64),
	}
}
