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 string, 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)
		}
		//新加的 灯控1 给一个默认策略 116
		light := dao.IntelligentLight{TenantId: tenantId}
		light.CreateDefaultStrategyByLight(device.ID, 2, 116)

		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)
	} else {
		if err := device.Update(); err != nil {
			logger.Logger.Errorf("Update err = %s \n", err.Error())
			return common.FailResponse(err.Error(), nil)
		}
	}
	if device.IsExistedByNameAndCode() {
		logger.Logger.Errorf("Update IsExistedByNameAndCode \n")
		return common.ParamsInvalidResponse(model.RepeatedName, 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, int64, *common.Errors) {
	// 创建查询实例
	device := &dao.LampPoleGroup{
		PoleGroupName: poleGroupName,
	}

	offset := (current - 1) * size
	limit := size
	devices, total, err := device.GetDevices(offset, limit)
	if err != nil {
		return nil, 0, common.FailResponse(err.Error(), nil)
	}
	for i, group := range devices {
		poleGroup := dao.LampPole{}
		poleGroup.GroupId = group.ID
		devices[i].CountLampPole, _ = poleGroup.GetGroupIdCount()
	}
	return devices, total, nil
}

func (s *lampPoleGroupService) Remove(userId int64, tenantId string, id int, name string) *common.Errors {
	device := &dao.LampPoleGroup{
		ID:         id,
		IsDeleted:  1,
		UpdateUser: userId,
		UpdateTime: common.Time(time.Now()),
	}
	fmt.Printf("device = %v", device)
	//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 string, ) ([]*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 string, ) string {
	return fmt.Sprintf(model.LampPoleGroupList, tenantId)
}

func (s *lampPoleGroupService) GetFiltration(tenantId string, ) ([]*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 string, ) string {
	return fmt.Sprintf(model.GroupFiltrationList, tenantId)
}

func (s *lampPoleGroupService) GetTree(tenantId string, ) ([]*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 string, ids []int) []*dao.LampPoleGroup {
	device := &dao.LampPoleGroup{
		TenantId:  tenantId,
		IsDeleted: 0,
	}
	return device.BatchGet(ids)
}
