package common

import (
	"f5"
	"fmt"
	"main/constant"
	"q5"
)

type NftHomeMeta struct {
	Name         string `json:"name"`
	Symbol       string `json:"symbol"`
	Description  string `json:"description"`
	Image        string `json:"image"`
	ExternalLink string `json:"external_link"`
}

type StreamPagination struct {
	NextCursor     string `json:"next_cursor"`
	PreviousCursor string `json:"previous_cursor"`
	Remaining      int32  `json:"remaining"`
	Count          int32  `json:"count"`
	TotalCount     int32  `json:"total_count"`
}

type ShopCartGoods struct {
	NetId                int32  `json:"net_id"`
	ContractAddress      string `json:"address"`
	TokenId              string `json:"token_id"`
	SortIdx              uint32 `json:"idx"`
	AddTime              int32  `json:"addtime"`
	Flag                 int32
	PriceType            string
	PriceContractAddress string
	OrderId              string
}

type ShopCart struct {
	CurrSortIdx uint32           `json:"curr_sort_idx"`
	Goods       []*ShopCartGoods `json:"goods"`
}

type NftCache interface {
	GetRawData() string
	GetJsonData() interface{}
}

type NftDto struct {
	NetId           int32
	ContractAddress string
	TokenId         string
	NftCache        NftCache
	Price           string
	Payload         interface{}
	Param1          int64
	LockTime        int32
}

type App interface {
	Run(func(), func())
}

type RouterMgr interface {
}

type TaskMgr interface {
}

type CacheMgr interface {
	GetNfts([]*NftDto)
	GetIngameHero(string) (error, []interface{})
}

type ServiceMgr interface {
}

type ModelMgr interface {
}

func (this *StreamPagination) FillPage(page *f5.StreamPagination) {
	if page.NextCursor != 0 {
		this.NextCursor = q5.ToString(page.NextCursor)
	}
	if page.PreviousCursor != 0 {
		this.PreviousCursor = q5.ToString(page.PreviousCursor)
	}
	this.Count = page.Count
	this.TotalCount = page.TotalCount
	this.Remaining = page.Remaining
}

func (this *NftDto) GetKey() string {
	key := fmt.Sprintf("%d_%s_%s", this.NetId, this.ContractAddress, this.TokenId)
	return key
}

func (this *ShopCart) GetGoods(netId int32, contractAddress string, tokenId string) *ShopCartGoods {
	for _, val := range this.Goods {
		if val.NetId == netId && val.ContractAddress == contractAddress && val.TokenId == tokenId {
			return val
		}
	}
	return nil
}

func (this *ShopCart) DeleteExcessData() {
	q5.Sort[ShopCartGoods](this.Goods,
		func(a *ShopCartGoods, b *ShopCartGoods) bool {
			return a.SortIdx > b.SortIdx
		})
	newGoods := []*ShopCartGoods{}
	for _, val := range this.Goods {
		if len(newGoods) < constant.SHOPCART_MAX_GOODS_NUM {
			q5.AppendSlice(&newGoods, val)
		} else {
			break
		}
	}
	this.Goods = newGoods
	q5.Sort[ShopCartGoods](this.Goods,
		func(a *ShopCartGoods, b *ShopCartGoods) bool {
			return a.SortIdx < b.SortIdx
		})
}

func NewShopCart() *ShopCart {
	p := new(ShopCart)
	q5.NewSlice(&p.Goods, 0, 10)
	return p
}

func NewShopCartGoods() *ShopCartGoods {
	p := new(ShopCartGoods)
	return p
}
