package coin_service

import (
	"bwallet/models"
	"encoding/json"
)

type Coin struct {
	Id             int
	Name           string
	Nickname       json.RawMessage
	Icon           string
	Introduce      json.RawMessage
	Official       string
	Paper          string
	Platform       string
	Chain          string
	Release        string
	AreaSearch     string
	PublishCount   float64
	CirculateCount float64
	Decimals       int
	Address        string
	PlatformId     int
	Treaty         int

	PageNum  int
	PageSize int
}

/**
 * 通过id获取指定币种信息
 * @param id
 * @return
 */
func (c *Coin) Get() (*models.Coin, error) {
	coin, err := models.GetCoin(c.Id)
	if err != nil {
		return nil, err
	}

	//var m map[string]interface{}
	//if err := json.Unmarshal([]byte(coin.Name), &m); err == nil {
	//	coin.Name = m["zh-CN"].(string)
	//}
	return coin, nil
}

/**
 * 获取符合条件的信息
 * @param id
 * @return
 */
func (c *Coin) GetAll() ([]*models.Coin, error) {
	var coins []*models.Coin

	coins, err := models.GetCoins(c.PageNum, c.PageSize, c.getMaps())
	if err != nil {
		return nil, err
	}

	//var m map[string]interface{}
	//for _, value := range coins {
	//	if err := json.Unmarshal([]byte(value.Name), &m); err == nil {
	//		value.Name = m["zh-CN"].(string)
	//	}
	//}

	return coins, nil
}

func (c *Coin) Find() (*models.Coin, error) {
	var coin *models.Coin

	coin, err := models.Find(c.Name, c.Chain)
	if err != nil {
		return nil, err
	}
	return coin, nil
}

/**
 * 添加币种信息
 * @param
 * @return
 */
func (c *Coin) Add() error {
	coin := map[string]interface{}{
		"name":            c.Name,
		"nickname":        c.Nickname,
		"icon":            c.Icon,
		"introduce":       c.Introduce,
		"official":        c.Official,
		"paper":           c.Paper,
		"platform":        c.Platform,
		"chain":           c.Chain,
		"release":         c.Release,
		"area_search":     c.AreaSearch,
		"publish_count":   c.PublishCount,
		"circulate_count": c.CirculateCount,
		"decimals":        c.Decimals,
		"address":         c.Address,
		"platform_id":     c.PlatformId,
		"treaty":          c.Treaty,
	}

	if err := models.AddCoin(coin); err != nil {
		return err
	}

	return nil
}

/**
 * 更新币种信息
 * @param
 * @return
 */
func (c *Coin) Edit() error {
	return models.EditCoin(c.Id, map[string]interface{}{
		"name":            c.Name,
		"nickname":        c.Nickname,
		"icon":            c.Icon,
		"introduce":       c.Introduce,
		"official":        c.Official,
		"paper":           c.Paper,
		"platform":        c.Platform,
		"chain":           c.Chain,
		"release":         c.Release,
		"area_search":     c.AreaSearch,
		"publish_count":   c.PublishCount,
		"circulate_count": c.CirculateCount,
		"decimals":        c.Decimals,
		"address":         c.Address,
		"platform_id":     c.PlatformId,
		"treaty":          c.Treaty,
	})
}

/**
 * 通过id检查币种是否存在
 * @param id
 * @return
 */
func (c *Coin) ExistById() (bool, error) {
	return models.ExistCoinById(c.Id)
}

/**
 * 获取符合条件的信息总数
 * @param id
 * @return
 */
func (c *Coin) Count() (int, error) {
	return models.GetCoinTotal(c.getMaps())
}

/**
 * 删除币种信息
 * @param
 * @return
 */
func (c *Coin) Delete() error {
	return models.DeleteCoin(c.Id)
}

func (c *Coin) GetCoinsPlatform() ([]*models.Coin, error) {
	var coins []*models.Coin

	coins, err := models.GetCoinsPlatform()
	if err != nil {
		return nil, err
	}

	return coins, nil
}

func (c *Coin) getMaps() (map[string]interface{}) {
	maps := make(map[string]interface{})

	if c.Id != 0 {
		maps["id"] = c.Id
	}

	if c.Name != "" {
		maps["name"] = c.Name
	}
	if c.Chain != "" {
		maps["chain"] = c.Chain
	}
	if c.Platform != "" {
		maps["platform"] = c.Platform
	}

	if c.PlatformId != 0 {
		maps["platform_id"] = c.PlatformId
	}

	return maps
}
