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
	PlatformId int64

	PageNum  int
	PageSize int
}

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

	return live, nil
}

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

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

	return live, nil
}

func (l *LiveInfo) Add() error {
	live := 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,
		"platform_id": l.PlatformId,
	}

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

	return nil
}

func (l *LiveInfo) Verify() error {
	return models.EditLiveInfo(l.Id, map[string]interface{}{
		"status": l.Status,
	})
}

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,
		"platform_id": l.PlatformId,
	})
}

func (l *LiveInfo) TitleUpdate() error {
	return models.TitleUpdate(l.Id, map[string]interface{}{
		"title":       l.Title,
	})
}

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
	}


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

	maps["status"] = l.Status

	return maps
}
