package coin_ticker_service

import (
	"bwallet/models"
)

type CoinTicker struct {
	CoinId     int
	CoinName   string
	Ticker     float32
	PlatformId uint8
}

func (ct *CoinTicker) GetAll() ([]*models.CoinTicker, error) {
	coins_ticker, err := models.GetCoinsTicker(ct.getMaps())
	if err != nil {
		return nil, err
	}

	//var CoinsTickerResp = []*models.CoinTickerResp{}
	//for _, val := range coins_ticker {
	//	tmp := &models.CoinTickerResp{}
	//	tmp.CoinId = val.CoinInfo.ID
	//	tmp.Name = val.CoinInfo.Name
	//	tmp.Ticker = val.Ticker
	//
	//	CoinsTickerResp = append(CoinsTickerResp, tmp)
	//}

	return coins_ticker, nil
}

func (t *CoinTicker) Add() error {
	coinTicker := map[string]interface{}{
		"coin_id":     t.CoinId,
		"coin_name":   t.CoinName,
		"ticker":      t.Ticker,
		"platform_id": t.PlatformId,
	}

	if err := models.AddCoinTicker(coinTicker); err != nil {
		return err
	}

	return nil
}

func (t *CoinTicker) Edit() error {
	return models.EditCoinTicker(t.getMaps(), map[string]interface{}{
		"ticker": t.Ticker,
	})
}

func (t *CoinTicker) Exist() (bool, error) {
	return models.ExistCoin(t.CoinId, t.PlatformId)
}

func (t *CoinTicker) Delete() error {
	return models.DeleteCoinTicker(t.CoinId, t.PlatformId)
}

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

	if c.CoinId != 0 {
		maps["coin_id"] = c.CoinId
	}

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