package gitea.com/kunmeng/HikNetSDKPkg

import (
	"fmt"
	"github.com/ebitengine/purego"
	"runtime"
	"unsafe"
)

var libc uintptr

var (
	DVR_Init func() bool

	newHIKBallCamera func() unsafe.Pointer
	initBallCamera   func(core unsafe.Pointer, ip string, port string, username string, password string, BallMachineType string) bool
	ptzTo            func(core unsafe.Pointer, Action int, P float32, T float32, Z float32) bool
	ptzGet           func(unsafe.Pointer, unsafe.Pointer, unsafe.Pointer, unsafe.Pointer) bool
	stopBus          func(unsafe.Pointer, int) bool
	startBus         func(unsafe.Pointer, int, int) bool

	newHIKNvr                func() unsafe.Pointer
	initNvr                  func(unsafe.Pointer, string, string, string, string, int) bool
	checkTimeRegionWithMonth func(core unsafe.Pointer, year int, month int) string
	checkTimeRegionWithDay   func(core unsafe.Pointer, year int, month int, day int) string
)

func init() {
	var err error
	libc, err = openLibrary(getSystemLibrary())
	if err != nil {
		panic(err)
	}

	purego.RegisterLibFunc(&DVR_Init, libc, "DVR_Init")

	purego.RegisterLibFunc(&newHIKBallCamera, libc, "NewHIKBallCamera")
	purego.RegisterLibFunc(&initBallCamera, libc, "InitBallCamera")
	purego.RegisterLibFunc(&ptzTo, libc, "PtzGotoPut")
	purego.RegisterLibFunc(&ptzGet, libc, "PtzGet")
	purego.RegisterLibFunc(&stopBus, libc, "StopBus")
	purego.RegisterLibFunc(&startBus, libc, "StartBus")

	purego.RegisterLibFunc(&newHIKNvr, libc, "NewHIKNvr")
	purego.RegisterLibFunc(&initNvr, libc, "InitNvr")
	purego.RegisterLibFunc(&checkTimeRegionWithMonth, libc, "CheckTimeRegionWithMonth")
	purego.RegisterLibFunc(&checkTimeRegionWithDay, libc, "CheckTimeRegionWithDay")

}

func getSystemLibrary() string {
	switch runtime.GOOS {
	case "linux":
		return "libc.so.6"
	case "windows":
		return "Hikvision_Network_SDK_Packaging_Library.dll"
	default:
		panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
	}
}

type HIKBallCamera struct {
	core unsafe.Pointer
}

func NewHIKBallCamera() *HIKBallCamera {
	return &HIKBallCamera{
		core: newHIKBallCamera(),
	}
}

func (h *HIKBallCamera) Login(ip string, port string, username string, password string, BallMachineType string) bool {
	return initBallCamera(h.core, ip, port, username, password, BallMachineType)
}
func (h *HIKBallCamera) PtzTo(Action int, P, T, Z float32) bool {
	return ptzTo(h.core, Action, P, T, Z)
}
func (h *HIKBallCamera) PTZGet(P, T, Z *float32) bool {
	return ptzGet(h.core, unsafe.Pointer(P), unsafe.Pointer(T), unsafe.Pointer(Z))
}
func (h *HIKBallCamera) StopBus(Direction int) bool {
	return stopBus(h.core, Direction)
}
func (h *HIKBallCamera) StartBus(Direction, Speed int) bool {
	return startBus(h.core, Direction, Speed)
}

type HIKNvr struct {
	core unsafe.Pointer
}

func NewHIKNvr() *HIKNvr {
	return &HIKNvr{
		core: newHIKNvr(),
	}
}

func (h *HIKNvr) Login(ip string, port string, username string, password string, nvrType int) bool {
	return initNvr(h.core, ip, port, username, password, nvrType)
}

func (h *HIKNvr) CheckTimeRegionWithMonth(year int, month int) string {
	return checkTimeRegionWithMonth(h.core, year, month)
}
func (h *HIKNvr) CheckTimeRegionWithDay(year int, month int, day int) string {
	return checkTimeRegionWithDay(h.core, year, month, day)
}
