package live_info_service

import (
	"bwallet/models"
	"bwallet/pkg/snowflake"
	"bwallet/pkg/util"
)

type LiveInfo struct {
	Id         int
	Title      string
	Cover      string
	CategoryId uint8
	Duration   uint32
	Mode       uint8
	ApplyId    uint32
	ApplyName  string
	Status     uint8
	LiveStatus uint8
	PlatformId int64
	OrderSn    int64
	Unit       string
	Price      float64

	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) LiveCharge() ([]*models.LiveChargeResp, error) {
	var live []*models.LiveInfo

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

	var records = []*models.LiveChargeResp{}
	for _, value := range live {
		record := &models.LiveChargeResp{}

		record.ApplyId = value.ApplyId
		record.OrderSn = value.OrderSn
		record.Price = value.Price
		record.Unit = value.Unit
		record.Status = value.Status
		record.Duration = value.Duration
		record.Id = value.ID

		records = append(records, record)
	}

	return records, nil
}

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

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

	var records = []*models.LiveInfoBackendResp{}
	for _, value := range live {
		record := &models.LiveInfoBackendResp{}
		record.Id = value.ID
		record.Title = value.Title
		record.Cover = value.Cover
		record.Status = value.Status
		record.LiveStatus = value.LiveStatus
		record.Duration = value.Duration
		record.ApplyName = value.ApplyName
		record.Unit = value.Unit
		record.Price = value.Price
		record.OrderSn = value.OrderSn
		record.Category = value.Category.Name
		record.CreateTime = util.FormatUnix(value.CreateTime)

		records = append(records, record)
	}

	return records, nil
}

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

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

	var records = []*models.LiveInfoClientResp{}
	for _, value := range live {
		record := &models.LiveInfoClientResp{}
		record.Id = value.ID
		record.Title = value.Title
		record.Cover = value.Cover
		record.Status = value.Status
		record.LiveStatus = value.LiveStatus
		record.Duration = value.Duration
		record.Unit = value.Unit
		record.Price = value.Price
		record.OrderSn = value.OrderSn
		record.Category = value.Category.Name

		records = append(records, record)
	}

	return records, nil
}

func (l *LiveInfo) Add() error {
	node, _ := snowflake.NewNode(0)
	id := node.Generate()
	live := map[string]interface{}{
		"title":       l.Title,
		"cover":       l.Cover,
		"category_id": l.CategoryId,
		"duration":    l.Duration,
		"apply_id":    l.ApplyId,
		"apply_name":  l.ApplyName,
		"platform_id": l.PlatformId,
		"order_sn":    id.Int64(),
		"unit":        l.Unit,
		"price":       l.Price,
	}
	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) UpdateStatus() error {
	data := map[string]interface{}{}
	if 0 != l.Status {
		data["status"] = l.Status
	}

	if 0 != l.LiveStatus {
		data["live_status"] = l.LiveStatus
	}
	return models.UpdateStatus(l.Id, data)
}

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

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,
		"mode":        l.Mode,
		"apply_id":    l.ApplyId,
		"status":      l.Status,
		"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
	}

	if l.ApplyId != 0 {
		maps["apply_id"] = l.ApplyId
	}

	maps["status"] = l.Status

	if l.LiveStatus != 0 {
		maps["live_status"] = l.LiveStatus
	}

	if l.OrderSn != 0 {
		maps["order_sn"] = l.OrderSn
	}

	return maps
}
