package live_info_service

import (
	"bwallet/models"
)

type LiveInfo struct {
	Id         int
	Title      string
	Cover      string
	CategoryId uint8
	Duration   uint32
	Attendance uint32
	Mode       uint8
	ApplyId    uint32
	Status     uint8
	Sort       uint8

	PageNum  int
	PageSize int
}

func (l *LiveInfo) GetOneLiveInfo() (*models.LiveInfo, error) {
	category, err := models.GetOneLiveInfo(l.Id)
	if err != nil {
		return nil, err
	}

	return category, nil
}

func (l *LiveInfo) GetAll() ([]*models.LiveInfo, error) {
	var article []*models.LiveInfo

	article, err := models.GetLiveInfo(l.PageNum, l.PageSize, l.getMaps())
	if err != nil {
		return nil, err
	}

	return article, nil
}

func (l *LiveInfo) Add() error {
	category := map[string]interface{}{
		"title":       l.Title,
		"cover":       l.Cover,
		"category_id": l.CategoryId,
		"duration":    l.Duration,
		"attendance":  l.Attendance,
		"mode":        l.Mode,
		"apply_id":    l.ApplyId,
		"sort":        l.Sort,
	}

	if err := models.AddLiveInfo(category); err != nil {
		return err
	}

	return nil
}

func (l *LiveInfo) Edit() error {
	return models.EditLiveInfo(l.Id, map[string]interface{}{
		"title":       l.Title,
		"cover":       l.Cover,
		"category_id": l.CategoryId,
		"duration":    l.Duration,
		"attendance":  l.Attendance,
		"mode":        l.Mode,
		"apply_id":    l.ApplyId,
		"status":      l.Status,
		"sort":        l.Sort,
	})
}

func (l *LiveInfo) ExistById() (bool, error) {
	return models.ExistLiveInfoById(l.Id)
}

func (l *LiveInfo) Count() (int, error) {
	return models.GetLiveInfoTotal(l.getMaps())
}

func (l *LiveInfo) Delete() error {
	return models.DeleteLiveInfo(l.Id)
}

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

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

	if l.Title != "" {
		maps["title"] = l.Title
	}

	if l.CategoryId != 0 {
		maps["category_id"] = l.CategoryId
	}

	return maps
}
