package goonvif

import (
	"fmt"
	"testing"
	"time"

	"git.pyer.club/kingecg/goonvif/onvif"
)

func TestListInterfaces(t *testing.T) {
	ifaces, err := listLocalNetworkInterfaces()
	if err != nil {
		t.Fatal(err)
	}
	t.Log(ifaces)
}

func TestDiscovery(t *testing.T) {
	devices, err := Discovery()
	if err != nil {
		t.Fatal(err)
	}
	for _, device := range devices {
		t.Log(device)
	}
}

func TestGetCapabilities(t *testing.T) {
	// 创建一个模拟的 Device 对象
	d := &Device{
		DeviceParams: onvif.DeviceParams{
			Xaddr:    "192.168.12.52",
			Username: "dctdev",
			Password: "dacenT2017",
		},
	}

	// 测试正常情况
	response, err := d.GetCapabilities()
	if err != nil {
		t.Errorf("Expected no error, got %v", err)
	}

	t.Log(response)
	// 模拟 check 函数返回错误
}

func TestGetEndpoints(t *testing.T) {
	// 创建一个模拟的 Device 对象
	d := &Device{
		DeviceParams: onvif.DeviceParams{
			Xaddr:    "192.168.12.52",
			Username: "dctdev",
			Password: "dacenT2017",
		},
	}
	response, err := d.GetEndpoints()
	if err != nil {
		t.Errorf("Expected no error, got %v", err)
	}
	t.Log(response)
}
func TestGetRecordingSummary(t *testing.T) {
	// 创建一个模拟的 Device 对象
	d := &Device{
		DeviceParams: onvif.DeviceParams{
			Xaddr:    "192.168.12.52",
			Username: "dctdev",
			Password: "dacenT2017",
		},
	}
	response, err := d.GetRecordingSummary()
	if err != nil {
		t.Errorf("Expected no error, got %v", err)
	}
	t.Log(response)
}

func TestFindRecording(t *testing.T) {
	// 创建一个模拟的 Device 对象
	d := &Device{
		DeviceParams: onvif.DeviceParams{
			Xaddr:    "192.168.12.52",
			Username: "dctdev",
			Password: "dacenT2017",
		},
	}
	// 模拟时间 三小时之前
	start := time.Now().Add(-time.Hour * 3)
	end := time.Now().Add(-time.Hour * 2)
	response, err := d.FindRecording(start, end)
	if err != nil {
		t.Errorf("Expected no error, got %v", err)
		return
	}

	for _, recordingToken := range response {
		uri, uerr := d.GetReplayUri(recordingToken, UdpUnicastStreamSetup)
		if uerr != nil {
			t.Errorf("Expected no error, got %v", uerr)
			return
		}
		fmt.Println(recordingToken, uri)
	}
}

func TestGetStreamUri(t *testing.T) {
	d := &Device{
		DeviceParams: onvif.DeviceParams{
			Xaddr:    "192.168.12.52",
			Username: "dctdev",
			Password: "dacenT2017",
		},
	}

	uri, err := d.GetStreamUriUdp("")
	if err != nil {
		t.Errorf("Expected no error, got %v", err)
		return
	}
	fmt.Println(uri)
}

func TestGetPtzNodes(t *testing.T) {
	d := NewDevice(onvif.DeviceParams{
		Xaddr:    "192.168.12.52",
		Username: "dctdev",
		Password: "dacenT2017",
	})
	nodes, err := d.PTZNodes()
	if err != nil {
		t.Fatal(err)
	}
	t.Log(nodes)
}

func TestZoom(t *testing.T) {
	d := NewDevice(onvif.DeviceParams{
		Xaddr:    "192.168.12.52",
		Username: "dctdev",
		Password: "dacenT2017",
	})
	err := d.Zoom(-0.5)
	if err != nil {
		t.Fatal(err)
	}
}
