package supported_currency_service

import "bwallet/models"

type SupportedCurrency struct {
	ID         int
	PlatformId int
	CurrencyId int
	Sort       int

	PageNum  int
	PageSize int
}

func (c *SupportedCurrency) Get() (*models.SupportedCurrency, error) {
	currency, err := models.GetSupportedCurrency(c.ID)
	if err != nil {
		return nil, err
	}

	return currency, nil
}

func (c *SupportedCurrency) GetAll() ([]*models.SupportedCurrency, error) {
	var currencies []*models.SupportedCurrency

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

	return currencies, nil
}

func (c *SupportedCurrency) Add() error {
	currency := map[string]interface{}{
		"platform_id": c.PlatformId,
		"currency_id": c.CurrencyId,
		"sort":        c.Sort,
	}

	if err := models.AddSupportedCurrency(currency); err != nil {
		return err
	}

	return nil
}

func (c *SupportedCurrency) Edit() error {
	return models.EditSupportedCurrency(c.ID, map[string]interface{}{
		"sort": c.Sort,
	})
}

func (c *SupportedCurrency) ExistById() (bool, error) {
	return models.ExistSupportedCurrencyById(c.ID)
}

func (c *SupportedCurrency) Count() (int, error) {
	return models.GetSupportedCurrencyTotal(c.getMaps())
}

func (c *SupportedCurrency) Delete() error {
	return models.DeleteSupportedCurrency(c.ID)
}

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

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

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

	if c.CurrencyId != 0 {
		maps["currency_id"] = c.CurrencyId
	}

	return maps
}

func GetCurrencies() ([]*models.Currency, error) {
	var currencies []*models.Currency

	currencies, err := models.GetCurrencies()
	if err != nil {
		return nil, err
	}

	return currencies, nil
}
