package HikSDK

import (
	"errors"
	"time"
)

type Nvr struct {
	userId     LONG
	deviceInfo NET_DVR_DEVICEINFO
}

type TimeRange struct {
	StartYear   DWORD
	StartMonth  DWORD
	StartDay    DWORD
	StartHour   DWORD
	StartMinute DWORD
	StartSecond DWORD
	EndYear     DWORD
	EndMonth    DWORD
	EndDay      DWORD
	EndHour     DWORD
	EndMinute   DWORD
	EndSecond   DWORD
}
type WebTimeRang struct {
	StartTime time.Time
	EndTime   time.Time
}

func NewNvr(Ip string, Port int, Username, Password string) (*Nvr, error) {
	userId, deviceInfo, err := login(Ip, Port, Username, Password)
	if err != nil {
		return nil, err
	}
	return &Nvr{
		userId:     userId,
		deviceInfo: deviceInfo,
	}, nil
}

func (this *Nvr) GetTimeZone() (int, error) {
	if this == nil {
		return 0, errors.New("Nvr is nil")
	}
	return getTimeZone(this.userId), nil
}

func (this *Nvr) CheckTimeRegionWithMonth(Year, Month, Channel uint16) ([]uint8, error) {
	if this == nil {
		return nil, errors.New("Nvr is nil")
	}
	return QueryMonth(this.userId, WORD(Year), BYTE(Month), DWORD(Channel))
}

func (this *Nvr) CheckTimeRegionWithDay(Year, Month, Day, Channel uint16) ([]WebTimeRang, error) {
	if this == nil {
		return nil, errors.New("Nvr is nil")
	}
	Handle := QueryDayHandle(this.userId, DWORD(Year), DWORD(Month), DWORD(Day), LONG(Channel))
	res, state, err := QueryDayNextFile(Handle)
	if err != nil {
		return []WebTimeRang{}, err
	}
	ByUTCDiff, err := this.GetTimeZone()
	if err != nil {
		return nil, err
	}
	var webres []WebTimeRang
	for state > 0 {
		var StartTimeObj time.Time
		var EndTimeObj time.Time

		if state == 1001 {
			break
		}
		if state == 1003 {
			break
		}
		if state == 1004 {
			break
		}
		if state == 1000 {
			if uint16(res.StartDay) != Day {
				StartTimeObj = time.Date(int(res.StartYear), time.Month(res.StartMonth), int(res.StartDay+1), 0, 0, 0, 0, time.UTC)
			} else {
				StartTimeObj = time.Date(int(res.StartYear), time.Month(res.StartMonth), int(res.StartDay), int(res.StartHour), int(res.StartMinute), int(res.StartSecond), 0, time.UTC)
			}
			if uint16(res.EndDay) != Day {
				EndTimeObj = time.Date(int(res.EndYear), time.Month(res.EndMonth), int(res.EndDay-1), 23, 59, 59, 59, time.UTC)
			} else {
				EndTimeObj = time.Date(int(res.EndYear), time.Month(res.EndMonth), int(res.EndDay), int(res.EndHour), int(res.EndMinute), int(res.EndSecond), 0, time.UTC)
			}
			StartTimeObj = StartTimeObj.Add(time.Minute * time.Duration(-ByUTCDiff))
			EndTimeObj = EndTimeObj.Add(time.Minute * time.Duration(-ByUTCDiff))
			webres = append(webres, WebTimeRang{
				StartTime: StartTimeObj,
				EndTime:   EndTimeObj,
			})
		}
		if state == 1002 {
			res, state, err = QueryDayNextFile(Handle)
			if err != nil {
				return []WebTimeRang{}, err
			}
			time.Sleep(time.Millisecond * 5)
			continue
		}
		res, state, err = QueryDayNextFile(Handle)
		if err != nil {
			return []WebTimeRang{}, err
		}
		time.Sleep(time.Millisecond * 5)
	}
	err = FindClose(Handle)
	if err != nil {
		return nil, err
	}
	return webres, nil
}
func (this *Nvr) Logout() error {
	if this == nil {
		return errors.New("Nvr is nil")
	}
	return logout(this.userId)
}
func (this *Nvr) Status() bool {
	if this == nil {
		return false
	}
	return deviceOnline(this.userId)
}
