package schedule

import (
	"netease_control_center/interfaces/log"
	"strconv"
	"sync/atomic"
	"time"
	"unsafe"
)

func SendSCmd(scmd SCMD, arg ...interface{}) {
	select {
	case coreChan <- SCmdStruct{SCmd: scmd, Arg: arg, SendTime: time.Now()}:
		if scmd != SCMD_SChedule {
			log.Printf(log.DebugLog, "Send cmd: %d success!\n", scmd)
		}
	default:
		log.Println(log.DebugLog, "Send fail, cmd:", scmd, arg)
	}
}

func SendSCmdStruct(scmd SCmdStruct) {
	scmd.SendTime = time.Now()

	select {
	case coreChan <- scmd:
		if scmd.SCmd != SCMD_SChedule {
			log.Printf(log.DebugLog, "Send cmd: %+v success!\n", scmd)
		}
	default:
		log.Println(log.DebugLog, "Send fail, cmd:", scmd)
	}
}

type SCmd_Base_Args struct {
	OemArg interface{}
}

type SCmdPlayer_Args struct {
	StartPos int
}

type SCmdTask_Args struct {
}

type SCmd_Args struct {
	SCmd_Base_Args
	SCmdPlayer_Args
	SCmdTask_Args
}

type ScheduleDomain uint8

const (
	ScheduleDomain_Music ScheduleDomain = iota
	ScheduleDomain_NoneMusic
	ScheduleDomain_Voice
	ScheduleDomain_Alarm
	ScheduleDomain_Remind
	ScheduleDomain_Notice
	ScheduleDomain_IotWifiParing
)

func (this *ScheduleDomain) String() string {
	switch *this {
	case ScheduleDomain_Music:
		return "ScheduleDomain_Music"
	case ScheduleDomain_NoneMusic:
		return "ScheduleDomain_NoneMusic"
	case ScheduleDomain_Voice:
		return "ScheduleDomain_Voice"
	default:
		return strconv.Itoa((int(*this)))
	}
}

func (this ScheduleDomain) IsMusicDomain() bool {
	switch this {
	case ScheduleDomain_Music:
		return true
	default:
		return false
	}
}

type ScheduleDomainGetFunc (func() ScheduleDomain)

var (
	scheduleDomain_default_player ScheduleDomainGetFunc = func() ScheduleDomain {
		return ScheduleDomain_Music
	}
	scheduleDomain_default_nonemusic ScheduleDomainGetFunc = func() ScheduleDomain {
		return ScheduleDomain_NoneMusic
	}

	scheduleDomainStorage unsafe.Pointer = unsafe.Pointer(&scheduleDomain_default_nonemusic)
)

func GetCurScheduleDomain() ScheduleDomain {
	p := atomic.LoadPointer(&scheduleDomainStorage)
	return (*(*ScheduleDomainGetFunc)(p))()
}

func setscheduleDomain(f ScheduleDomainGetFunc) {
	if f == nil || f() == ScheduleDomain_Voice || atomic.LoadPointer(&scheduleDomainStorage) == unsafe.Pointer(&f) {
		return
	}
	//log.Println(log.ErrorLog, "!!!Will set domain:", f())
	//debug.PrintStack()
	atomic.StorePointer(&scheduleDomainStorage, unsafe.Pointer(&f))
}

func setscheduleDomainWithPointer(p unsafe.Pointer) {
	atomic.StorePointer(&scheduleDomainStorage, p)
}
