package service

import (
	"iot_manager_service/app/device/service"
	"iot_manager_service/app/security/model"
	"iot_manager_service/util/cache"
	"iot_manager_service/util/common"
)

var SecurityService = new(securityService)

type securityService struct{}

func (s *securityService) GetCameraLiveList(tenantId string) ([]model.CameraLiveDetail, *common.Errors) {
	groups, err := service.LampPoleGroupService.GetList(tenantId)
	if err != nil {
		return nil, nil
	}

	var rsp []model.CameraLiveDetail
	for _, group := range groups {
		cameraDevices := service.CameraService.GetByLampPoleGroup(group.ID)
		var cameraList []model.CameraLive
		for _, cameraDevice := range cameraDevices {
			_, state := cache.GetDeviceState(cameraDevice.DeviceSN)
			poleGroupName := cameraDevice.DeviceName + "(离线)"
			if state == "1" {
				poleGroupName = cameraDevice.DeviceName + "在线"
			}
			cameraList = append(cameraList, model.CameraLive{
				Id:               cameraDevice.ID,
				DeviceName:       cameraDevice.DeviceName,
				DeviceSn:         cameraDevice.DeviceSN,
				PoleGroupName:    poleGroupName,
				MonitorAddress:   cameraDevice.MonitorAddress,
				StreamId:         cameraDevice.StreamId,
				RunState:         state,
				CameraType:       cameraDevice.CameraType,
				LampPoleLocation: cameraDevice.LampPoleLocation,
			})
		}
		rsp = append(rsp, model.CameraLiveDetail{
			PoleGroupName: group.PoleGroupName,
			CameraList:    cameraList,
		})
	}
	return rsp, nil
}
