package ebaiapi

import (
	"time"

	"git.rosy.net.cn/baseapi/utils"
)

const (
	ReplyStatusAll        = -1
	ReplyStatusNotReplied = 0
	ReplyStatusReplied    = 1
)

type GoodsComment struct {
	CommentID     int64  `json:"comment_id"`
	Name          string `json:"name"`
	Rating        int    `json:"rating"`
	RatingContent string `json:"rating_content"`
}

type OrderComment struct {
	CommentID     int64  `json:"comment_id"`
	PackageRating int    `json:"package_rating"`
	QualityRating int    `json:"quality_rating"`
	RatingAt      int64  `json:"rating_at"`
	RatingContent string `json:"rating_content"`
	ReplyAt       int64  `json:"reply_at"`
	ReplyContent  string `json:"reply_content"`
	ServiceRating int    `json:"service_rating"`
}

type ShopOrderComment struct {
	AnonymousRating string          `json:"anonymous_rating"`
	CanEditReply    string          `json:"can_edit_reply"`
	CanReply        string          `json:"can_reply"`
	DishRateList    []*GoodsComment `json:"dish_rate_list"`
	OrderID         int64           `json:"order_id"`
	OrderRateList   []*OrderComment `json:"order_rate_list"`
}

// 不要使用此接口，用OrderCommetGet
func (a *API) OrderRatesGet(shopID string, baiduShopID int64, startTime, endTime time.Time, replyStatus int) (commentList []map[string]interface{}, err error) {
	params := a.genShopIDParams(shopID, baiduShopID, 0)
	if !utils.IsTimeZero(startTime) {
		params["startTime"] = startTime.Unix()
	}
	if !utils.IsTimeZero(endTime) {
		params["endTime"] = endTime.Unix()
	}
	if replyStatus != ReplyStatusAll {
		params["replyStatus"] = replyStatus
	}
	pageNo := 1
	for {
		params["page"] = pageNo
		result, err := a.AccessAPI("ugc.orderrates.get", params)
		if err != nil {
			return nil, err
		}
		data := result.Data.(map[string]interface{})
		commentList = append(commentList, utils.Slice2MapSlice(data["comment_list"].([]interface{}))...)
		total := int(utils.MustInterface2Int64(data["total"]))
		if len(commentList) >= total {
			break
		}
	}
	return commentList, err
}

func (a *API) OrderRatesReply(shopID string, baiduShopID int64, commentID, content string) (err error) {
	params := a.genShopIDParams(shopID, baiduShopID, 0)
	params["comment_id"] = commentID
	params["content"] = content
	_, err = a.AccessAPI("ugc.reply", params)
	return err
}

// 此函数在用户匿名评价时，order_id为0，还是只有扒网页
func (a *API) OrderCommetGet(shopID string, baiduShopID int64, startTime, endTime time.Time, replyStatus int) (commentList []*ShopOrderComment, err error) {
	params := a.genShopIDParams(shopID, baiduShopID, 0)
	if !utils.IsTimeZero(startTime) {
		params["startTime"] = startTime.Unix()
	}
	if !utils.IsTimeZero(endTime) {
		params["endTime"] = endTime.Unix()
	}
	if replyStatus != ReplyStatusAll {
		params["replyStatus"] = replyStatus
	}
	pageNo := 1
	for {
		params["page"] = pageNo
		result, err := a.AccessAPI("ugc.ordercomment.get", params)
		if err != nil {
			return nil, err
		}
		data := result.Data.(map[string]interface{})
		var pageCommentList []*ShopOrderComment
		if err = utils.Map2StructByJson(data["comment_list"], &pageCommentList, false); err == nil {
			commentList = append(commentList, pageCommentList...)
		} else {
			break
		}
		total := int(utils.MustInterface2Int64(data["total"]))
		if len(commentList) >= total {
			break
		}
	}
	return commentList, err
}
