package fee_service

import (
	"bwallet/models"
)

type Fee struct {
	Id              int
	Cid             int
	PlatformId      int
	FeesType        uint8
	Fees            float32
	TransactionFees float32
	CoinName        string

	PageNum  int
	PageSize int
}

func (c *Fee) Get() (*models.Fee, error) {

	fee, err := models.GetFee(c.Id)
	if err != nil {
		return nil, err
	}

	return fee, nil
}

func (c *Fee) GetAll() ([]*models.Fee, error) {
	var fee []*models.Fee

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

	return fee, nil
}

func (c *Fee) Add() error {
	fee := map[string]interface{}{
		"cid":         c.Cid,
		"platform_id": c.PlatformId,
		"coin_name":   c.CoinName,
	}

	if err := models.AddFee(fee); err != nil {
		return err
	}

	return nil
}

func (c *Fee) Edit() error {
	return models.EditFee(c.Id, map[string]interface{}{
		"fees_type":        c.FeesType,
		"fees":             c.Fees,
		"transaction_fees": c.TransactionFees,
	})
}

func (f *Fee) ExistById() (bool, error) {
	return models.ExistFeeById(f.Id)
}

func (f *Fee) Count() (int, error) {
	return models.GetFeeTotal(f.getMaps())
}

func (f *Fee) Search() (*models.Fee, error) {
	var fee *models.Fee

	fee, err := models.Search(f.getMaps())
	if err != nil {
		return nil, err
	}

	return fee, nil
}

func (c *Fee) DeleteByCondition() error {
	return models.DeleteFeeByCondition(c.getMaps())
}

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

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

	if c.Cid != 0 {
		maps["cid"] = c.Cid
	}

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

	return maps
}
