package models

import (
	"bwallet/pkg/setting"
	"github.com/jinzhu/gorm"
	"time"
)

type LiveInfo struct {
	Model

	Title      string  `json:"title"`
	Cover      string  `json:"cover"`
	CategoryId uint8   `json:"category_id"`
	Duration   uint32  `json:"duration"`
	Mode       uint8   `json:"mode"`
	ApplyId    uint32  `json:"apply_id"`
	ApplyName  string  `json:"apply_name"`
	Status     uint8   `json:"status"`
	LiveStatus uint8   `json:"live_status"`
	PlatformId int64   `json:"platform_id"`
	OrderSn    int64   `json:"order_sn"`
	Unit       string  `json:"unit"`
	Price      float64 `json:"price"`
	CreateTime int64   `json:"create_time,omitempty"`

	Category *LiveCategory `gorm:"foreignkey:CategoryId" json:"category"`
}

type LiveInfoClientResp struct {
	Id         int     `json:"id"`
	Title      string  `json:"title"`
	Cover      string  `json:"cover"`
	Duration   uint32  `json:"duration"`
	Status     uint8   `json:"status"`
	LiveStatus uint8   `json:"live_status"`
	Unit       string  `json:"unit"`
	Price      float64 `json:"price"`
	OrderSn    int64   `json:"order_sn"`
	Category   string  `json:"category"`
}

type LiveInfoBackendResp struct {
	Id         int     `json:"id"`
	Title      string  `json:"title"`
	Cover      string  `json:"cover"`
	Duration   uint32  `json:"duration"`
	ApplyName  string  `json:"apply_name"`
	Status     uint8   `json:"status"`
	LiveStatus uint8   `json:"live_status"`
	OrderSn    int64   `json:"order_sn"`
	Unit       string  `json:"unit"`
	Price      float64 `json:"price"`
	Category   string  `json:"category"`
	CreateTime string  `json:"create_time"`
}

type LiveChargeResp struct {
	Id       int     `json:"id,omitempty"`
	Status   uint8   `json:"status"`
	ApplyId  uint32  `json:"apply_id"`
	OrderSn  int64   `json:"order_sn"`
	Price    float64 `json:"price"`
	Unit     string  `json:"unit"`
	Duration uint32  `json:"duration"`
}

const (
	PROCESSING      = 0 //待审核
	PEDDING_PAYMENT = 1 //审核通过（待支付）
	VOIDED          = 2 //无效
	PEDDING_LIVE    = 2 //等待开播
	LIVEING         = 3 //直播中
	LIVEED          = 4 //直播结束

)

func (w LiveInfo) TableName() string {
	return setting.DatabaseSetting.Name_Sources + ".wallet_live_info"
}

func GetOneLiveInfo(id int) (*LiveInfo, error) {
	var live LiveInfo
	err := db.Where("id = ?", id).First(&live).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	err = db.Model(&live).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return &live, nil
}

func ExistLiveInfoById(id int) (bool, error) {
	var live LiveInfo
	err := db.Select("id").Where("id = ?", id).First(&live).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err
	}

	if live.ID > 0 {
		return true, nil
	}

	return false, nil
}

func GetLiveInfoTotal(maps interface{}) (int, error) {
	var count int

	if err := db.Model(&LiveInfo{}).Where(maps).Count(&count).Error; err != nil {
		return 0, err
	}

	return count, nil
}

func GetLiveInfo(pageNum, pageSize int, maps interface{}) ([]*LiveInfo, error) {
	var live []*LiveInfo

	err := db.Where(maps).Preload("Category").Order("create_time desc").Offset(pageNum).Limit(pageSize).Find(&live).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return live, nil
}

func AddLiveInfo(data map[string]interface{}) (error) {
	live := LiveInfo{
		Title:      data["title"].(string),
		Cover:      data["cover"].(string),
		CategoryId: data["category_id"].(uint8),
		Duration:   data["duration"].(uint32),
		Mode:       1,
		ApplyId:    data["apply_id"].(uint32),
		ApplyName:  data["apply_name"].(string),
		PlatformId: data["platform_id"].(int64),
		OrderSn:    data["order_sn"].(int64),
		Unit:       data["unit"].(string),
		Price:      data["price"].(float64),
		Status:     PROCESSING,
		LiveStatus: PROCESSING,
		CreateTime: time.Now().UTC().Unix(),
	}

	if err := db.Create(&live).Error; err != nil {
		return err
	}

	return nil
}

func EditLiveInfo(id int, data interface{}) error {
	if err := db.Model(&LiveInfo{}).Where("id = ?", id).Updates(data).Error; err != nil {
		return err
	}

	return nil
}

func UpdateStatus(id int, data interface{}) error {
	if err := db.Model(&LiveInfo{}).Where("id = ?", id).Updates(data).Error; err != nil {
		return err
	}

	return nil
}

func TitleUpdate(id int, data interface{}) error {
	if err := db.Model(&LiveInfo{}).Where("id = ?", id).Updates(data).Error; err != nil {
		return err
	}

	return nil
}

func DeleteLiveInfo(id int) error {
	if err := db.Where("id = ?", id).Delete(LiveInfo{}).Error; err != nil {
		return err
	}

	return nil
}
