package service

import (
	"fmt"
	"iot_manager_service/app/device/dao"
	"iot_manager_service/app/device/model"
	"iot_manager_service/app/system/service"
	"iot_manager_service/util/cache"
	"iot_manager_service/util/common"
	"iot_manager_service/util/logger"
	"time"
)

// 中间件管理服务
var LampPoleGroupService = new(lampPoleGroupService)

type lampPoleGroupService struct{}

func (s *lampPoleGroupService) Get(id int) (*dao.LampPoleGroup, *common.Errors) {
	// 创建查询实例
	device := &dao.LampPoleGroup{
		ID: id,
	}
	err := device.GetDevice()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	return device, nil
}

func (s *lampPoleGroupService) CreateOrUpdate(userId int64, tenantId int, req *dao.LampPoleGroup) *common.Errors {
	device := req
	device.TenantId = tenantId
	device.UpdateUser = userId
	device.UpdateTime = common.Time(time.Now())

	if req.ID == 0 {
		device.CreateTime = common.Time(time.Now())
		device.CreateUser = userId
		if device.IsExistedByName() {
			logger.Logger.Errorf("Create IsExistedByName \n")
			return common.ParamsInvalidResponse(model.RepeatedName, nil)
		}
		if err := device.Create(); err != nil {
			logger.Logger.Errorf("Create err = %s \n", err.Error())
			return common.FailResponse(err.Error(), nil)
		}
		service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
			common.DeviceTypeLampPoleGroup, common.GetDeviceObject(device.ID, device.PoleGroupName), common.OperationSuccess)
		return common.SuccessResponse(common.Succeeded, nil)
	}

	if device.IsExistedByNameAndCode() {
		logger.Logger.Errorf("Update IsExistedByNameAndCode \n")
		return common.ParamsInvalidResponse(model.RepeatedName, nil)
	}

	if err := device.Update(); err != nil {
		logger.Logger.Errorf("Update err = %s \n", err.Error())
		return common.FailResponse(err.Error(), nil)
	}
	service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
		common.DeviceTypeLampPoleGroup, common.GetDeviceObject(device.ID, device.PoleGroupName), common.OperationSuccess)
	return common.SuccessResponse(common.Succeeded, nil)
}

func (s *lampPoleGroupService) List(poleGroupName string, current, size int) ([]dao.LampPoleGroup, *common.Errors) {
	// 创建查询实例
	device := &dao.LampPoleGroup{
		PoleGroupName: poleGroupName,
	}

	offset := (current - 1) * size
	limit := size
	devices, err := device.GetDevices(offset, limit)
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	return devices, nil
}

func (s *lampPoleGroupService) Remove(userId int64, tenantId int, id int, name string) *common.Errors {
	device := &dao.LampPoleGroup{
		ID:         id,
		IsDeleted:  1,
		UpdateUser: userId,
		UpdateTime: common.Time(time.Now()),
	}

	//todo
	// service.lampPoleService.CountRelation()
	err := device.Delete()
	if err != nil {
		service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
			common.DeviceTypeLampPoleGroup, common.GetDeviceObject(device.ID, device.PoleGroupName), common.OperationFail)
		return common.FailResponse(err.Error(), nil)
	}
	service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
		common.DeviceTypeLampPoleGroup, common.GetDeviceObject(device.ID, device.PoleGroupName), common.OperationSuccess)
	return nil
}

func (s *lampPoleGroupService) GetList(tenantId int) ([]*dao.LampPoleGroup, *common.Errors) {
	var devices []*dao.LampPoleGroup
	err := cache.Redis.Get(getLampPoleGroupListRedisKey(tenantId)).Scan(&devices)
	if err == nil {
		return devices, nil
	}

	device := &dao.LampPoleGroup{
		TenantId:  tenantId,
		IsDeleted: 0,
	}
	devices, err = device.GetAllDevices()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	_ = cache.Redis.Set(getLampPoleGroupListRedisKey(tenantId), devices, 0).Err()

	return devices, nil
}

func getLampPoleGroupListRedisKey(tenantId int) string {
	return fmt.Sprintf(model.LampPoleGroupList, tenantId)
}

func (s *lampPoleGroupService) GetFiltration(tenantId int) ([]*dao.LampPoleGroup, *common.Errors) {
	var devices []*dao.LampPoleGroup
	err := cache.Redis.Get(getGroupFiltrationListRedisKey(tenantId)).Scan(&devices)
	if err == nil {
		return devices, nil
	}

	device := &dao.LampPoleGroup{
		TenantId:  tenantId,
		IsDeleted: 0,
	}

	//todo
	// get device_light_control group_id
	// groupIds := lightControl.GetGroupIds()

	devices, err = device.GetAllDevices()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	_ = cache.Redis.Set(getGroupFiltrationListRedisKey(tenantId), devices, 0).Err()

	return devices, nil
}

func getGroupFiltrationListRedisKey(tenantId int) string {
	return fmt.Sprintf(model.GroupFiltrationList, tenantId)
}

func (s *lampPoleGroupService) GetTree(tenantId int) ([]*dao.LampPoleGroup, *common.Errors) {
	// todo use redis cache
	device := &dao.LampPoleGroup{
		TenantId:  tenantId,
		IsDeleted: 0,
	}

	//todo lampPole getALlDevice

	devices, err := device.GetAllDevices()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}

	return devices, nil
}

func (s *lampPoleGroupService) GetByIds(tenantId int, ids []int) []*dao.LampPoleGroup {
	device := &dao.LampPoleGroup{
		TenantId:  tenantId,
		IsDeleted: 0,
	}
	return device.BatchGet(ids)
}