package streamer_service

import (
	"bwallet/models"
)

type Streamer struct {
	Uid           int32
	UserName      string
	NickName      string
	Password      string
	RegTime       int64
	LastLoginTime int64
	RegIp         uint32
	LastLoginIp   uint32
	Status        uint8
	PlatformId    int64

	PageNum  int
	PageSize int
}

func (s *Streamer) GetOneStreamer() (*models.Streamer, error) {
	streamer, err := models.GetStreamer(s.Uid)
	if err != nil {
		return nil, err
	}

	return streamer, nil
}

func (s *Streamer) GetAll() ([]*models.Streamer, error) {
	var streamer []*models.Streamer

	streamer, err := models.GetStreamers(s.PageNum, s.PageSize, s.getMaps())
	if err != nil {
		return nil, err
	}

	return streamer, nil
}

func (s *Streamer) Add() error {
	streamer := map[string]interface{}{
		"username":      s.UserName,
		"nickname":      s.NickName,
		"password":      s.Password,
		"reg_ip":        s.RegIp,
		"last_login_ip": s.LastLoginIp,
		"status":        s.Status,
		"platform_id":   s.PlatformId,
	}

	if err := models.AddStreamer(streamer); err != nil {
		return err
	}

	return nil
}

func (s *Streamer) Edit() error {
	return models.EditStreamer(s.Uid, map[string]interface{}{
		"username":      s.UserName,
		"nickname":      s.NickName,
		"password":      s.Password,
		"reg_ip":        s.RegIp,
		"last_login_ip": s.LastLoginIp,
		"status":        s.Status,
		"platform_id":   s.PlatformId,
	})
}

func (s *Streamer) ExistById() (bool, error) {
	return models.ExistStreamerById(s.Uid)
}

func (s *Streamer) Count() (int, error) {
	return models.GetStreamerTotal(s.getMaps())
}

func (s *Streamer) Delete() error {
	return models.DeleteStreamer(s.Uid)
}

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

	if s.Uid != 0 {
		maps["id"] = s.Uid
	}

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

	maps["status"] = s.Status

	return maps
}
