package wallet_service

import (
	"bwallet/models"
	"fmt"
)

type Wallet struct {
	Id          int
	Name        string
	DownloadUrl string
	Introduce   string
	ChainId     int
	IssueCharge float32
	ChargeUnit  string

	PageNum  int
	PageSize int
}

func (w *Wallet) Get() (*models.Wallet, error) {

	wallet, err := models.GetWallet(w.Id)
	if err != nil {
		return nil, err
	}

	return wallet, nil
}

func (w *Wallet) GetAll() ([]*models.Wallet, error) {
	var wallets []*models.Wallet

	wallets, err := models.GetWallets(w.PageNum, w.PageSize, w.getMaps())
	if err != nil {
		return nil, err
	}

	return wallets, nil
}

func (w *Wallet) Add() error {
	wallet := map[string]interface{}{
		"name":         w.Name,
		"download_url": w.DownloadUrl,
		"introduce":    w.Introduce,
		"chain_id":     w.ChainId,
		"charge_unit":  w.ChargeUnit,
		"issue_charge": w.IssueCharge,
	}

	if err := models.AddWallet(wallet); err != nil {
		return err
	}

	return nil
}

func (w *Wallet) Edit() error {
	return models.EditWallet(w.Id, map[string]interface{}{
		"name":         w.Name,
		"download_url": w.DownloadUrl,
		"introduce":    w.Introduce,
		"chain_id":     w.ChainId,
		"issue_charge": w.IssueCharge,
		"charge_unit":  w.ChargeUnit,
	})
}

func (w *Wallet) ExistById() (bool, error) {
	fmt.Println(w.Id)
	return models.ExistWalletById(w.Id)
}

func (w *Wallet) Count() (int, error) {
	return models.GetWalletTotal(w.getMaps())
}

func (w *Wallet) Delete() error {
	return models.DeleteWallet(w.Id)
}

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

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

	if c.Name != "" {
		maps["name"] = c.Name
	}

	return maps
}
