package main

import (
	"sparrow/pkg/deviceStatus"
	"sparrow/pkg/online"
	"sparrow/pkg/otaUpgrade"
	"sparrow/pkg/rpcs"
	"sparrow/pkg/token"
)

type DeviceManager struct {
	onlineManager *online.Manager
	tokenHelper   *token.Helper
	otaManager    *otaUpgrade.OtaManager
	statusManager *deviceStatus.DevStatusManager
}

func NewDeviceManager(redishost string, port, db int) *DeviceManager {
	mgr := online.NewManager(redishost, port, db)
	helper := token.NewHelper(redishost, port, db)
	otaMgr := otaUpgrade.NewOtaManager(redishost, port, db)
	statusMgr := deviceStatus.NewDevStatusManager(redishost, port)
	return &DeviceManager{
		onlineManager: mgr,
		tokenHelper:   helper,
		otaManager:    otaMgr,
		statusManager: statusMgr,
	}
}

func (dm *DeviceManager) GenerateDeviceAccessToken(args rpcs.ArgsGenerateDeviceAccessToken, reply *rpcs.ReplyGenerateDeviceAccessToken) error {
	token, err := dm.tokenHelper.GenerateToken(args.Id)
	if err != nil {
		return err
	}

	reply.AccessToken = token
	return nil
}

func (dm *DeviceManager) ValidateDeviceAccessToken(args rpcs.ArgsValidateDeviceAccessToken, reply *rpcs.ReplyValidateDeviceAccessToken) error {
	return dm.tokenHelper.ValidateToken(args.Id, args.AccessToken)
}

func (dm *DeviceManager) GetOnline(args rpcs.ArgsGetOnline, reply *rpcs.ReplyGetOnline) error {
	return dm.onlineManager.GetOnline(args.Id, online.Status{
		ClientIP:          args.ClientIP,
		AccessRPCHost:     args.AccessRPCHost,
		HeartbeatInterval: args.HeartbeatInterval,
	})
}

func (dm *DeviceManager) GetOnlineV2(args rpcs.ArgsGetOnline, reply *rpcs.ReplyGetOnline) error {
	return dm.onlineManager.GetOnlineV2(args.Id, online.Status{
		ClientIP:          args.ClientIP,
		AccessRPCHost:     args.AccessRPCHost,
		HeartbeatInterval: args.HeartbeatInterval,
	})
}

func (dm *DeviceManager) HeartBeat(args rpcs.ArgsHeartBeat, reply *rpcs.ReplyHeartBeat) error {
	return dm.onlineManager.SetHeartbeat(args.Id)
}

func (dm *DeviceManager) GetOffline(args rpcs.ArgsGetOffline, reply *rpcs.ReplyGetOffline) error {
	return dm.onlineManager.GetOffline(args.Id)
}

func (dm *DeviceManager) GetDeviceOnlineStatus(args rpcs.ArgsGetDeviceOnlineStatus, reply *rpcs.ReplyGetDeviceOnlineStatus) error {
	status, err := dm.onlineManager.GetStatus(args.Id)
	if err != nil {
		return err
	}

	if status != nil {
		reply.ClientIP = status.ClientIP
		reply.AccessRPCHost = status.AccessRPCHost
		reply.HeartbeatInterval = status.HeartbeatInterval
	}

	return nil
}

func (dm *DeviceManager) SavaFile(args rpcs.ArgsOtaFile, reply *rpcs.ReplyGetOffline) error {
	err := dm.otaManager.SavaFile(args.FileId, args.FileData)
	if err != nil {
		return err
	}
	return nil
}

func (dm *DeviceManager) GetFile(args rpcs.ArgsOtaFile, reply *rpcs.ReplyOtaFile) error {
	file, err := dm.otaManager.GetFile(args.FileId)
	if err != nil {
		return err
	}
	if file != nil {
		reply.File = file.FileData
	}
	return nil
}

func (dm *DeviceManager) UpdateProgress(args rpcs.ArgsOtaProgress, reply *rpcs.ReplyGetOffline) error {
	err := dm.otaManager.UpdateProgress(args.DeviceId, args.Progress)
	if err != nil {
		return err
	}
	return nil
}

func (dm *DeviceManager) GetProgress(args rpcs.ArgsOtaProgress, reply *rpcs.ReplyOtaProgress) error {
	progress, err := dm.otaManager.GetProgress(args.DeviceId)
	if err != nil {
		return err
	}
	if progress != nil {
		reply.Progress = progress.Progress
	}
	return nil
}

func (dm *DeviceManager) GetDeviceStatus(args rpcs.ArgsGetStatus, reply *rpcs.ReplyStatus) error {
	status, err := dm.statusManager.GetDeviceStatus(args.Id)
	if err != nil {
		return err
	}
	if status != "" {
		reply.Status = status
	}
	return nil
}

func (dm *DeviceManager) GetDeviceInfo(args rpcs.ArgsGetStatus, reply *rpcs.ReplayInfo) error {
	info, err := dm.statusManager.GetDeviceInfo(args.Id)
	if err != nil {
		return err
	}
	if info != "" {
		reply.Info = info
	}
	return nil
}
