package coin_ticker_service

import (
	"bwallet/models"
)

type CoinTicker struct {
	Id         int
	CoinName   string
	Ticker     string
	PlatformId int
}

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

	return coins_ticker, nil
}

func (t *CoinTicker) Add() error {
	coinTicker := map[string]interface{}{
		"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.CoinName, t.PlatformId)
}

func (t *CoinTicker) ExistById() (bool, error) {
	return models.ExistById(t.Id)
}

func (t *CoinTicker) GetCoinTicker() (*models.CoinTicker, error) {
	ticker, err := models.GetCoinTicker(t.Id)
	if err != nil {
		return nil, err
	}

	return ticker, nil
}

func (t *CoinTicker) Delete() error {
	return models.DeleteCoinTicker(t.Id)
}

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

	if c.CoinName != "" {
		maps["coin_name"] = c.CoinName
	}

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