package service

import (
	"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"
	"strconv"
	"strings"
	"time"
)

// 中间件管理服务
var LightControlService = new(lightControlService)

type lightControlService struct{}

func (s *lightControlService) Get(id int) (*model.LightControlDetail, *common.Errors) {
	// 创建查询实例
	device := &dao.LightControl{
		ID: id,
	}
	err := device.GetDevice()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	endTime, state := cache.GetDeviceState(device.Sn)
	detail := model.LightControlDetail{
		LightControl: *device,
		EndLineTime:  endTime,
		NetworkState: state,
		RunState:     state,
	}
	return &detail, nil
}

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

	if device.LampPoleId != 0 {
		lampPole, err := LampPoleService.GetOne(device.LampPoleId)
		if err == nil {
			device.LampLat = lampPole.PoleLat
			device.LampLng = lampPole.PoleLng
			device.LampPoleName = lampPole.PoleName
			device.LampPoleSn = lampPole.PoleSN
			device.LampPoleLocation = lampPole.InstallLocation
			device.GroupId = lampPole.GroupId
		} else {
			logger.Logger.Errorf("LampPoleService.GetOne err = %v \n", err)
		}
	}

	if device.ControlType == model.ControlType_ZigBee {
		if device.ZigbeeId == 0 {
			return common.ParamsInvalidResponse(model.ZigbeeSelect, nil)
		}
		if device.IsOnDemand == 0 {
			if device.ControlNo == "" {
				return common.ParamsInvalidResponse(model.ControlNONull, nil)
			}
			if !checkControlNoIsCompliance(device.ControlNo) {
				return common.ParamsInvalidResponse(model.ControlNOInvalid, nil)
			}
		}
		zigbee, err := ZigbeeService.Get(device.ZigbeeId)
		if err != nil {
			return err
		}
		device.ZigbeeName = zigbee.Name
		device.ZigbeeSn = zigbee.Sn
		device.ChannelNum = zigbee.ChannelNum
		device.NetworkNum = zigbee.NetworkNum
	}

	if device.ID == 0 {
		device.CreateTime = time.Now()
		device.CreateUser = userId

		if device.IsExistedBySN() {
			logger.Logger.Errorf("Create IsExistedBySN \n")
			return common.ParamsInvalidResponse(model.RepeatedPrompts, nil)
		}
		logger.Logger.Errorf("device = %+v \n", device)
		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.DeviceTypeLightControl, common.GetDeviceObject(device.ID, device.Name), common.OperationSuccess)
		return common.SuccessResponse(common.Succeeded, 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.DeviceTypeLightControl, common.GetDeviceObject(device.ID, device.Name), common.OperationSuccess)
	return common.SuccessResponse(common.Succeeded, nil)
}

func (s *lightControlService) List(searchValue, controlType, zigbeeId string, current, size int) ([]model.LightControlDetail,
	*common.Errors) {
	device := dao.LightControl{}
	if searchValue != "" {
		device.Sn = searchValue
	}
	if controlType != "" {
		cType, err := strconv.Atoi(controlType)
		if err == nil {
			device.ControlType = cType
		}
	}
	if zigbeeId != "" {
		zId, err := strconv.Atoi(zigbeeId)
		if err == nil {
			device.ZigbeeId = zId
		}
	}

	offset := (current - 1) * size
	limit := size
	devices, err := device.GetDevices(offset, limit)
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	var details []model.LightControlDetail
	for _, d := range devices {
		detail := model.LightControlDetail{LightControl: d}
		endTime, state := cache.GetDeviceState(d.Sn)
		detail.RunState = state
		detail.EndLineTime = endTime
		detail.NetworkState = state
		details = append(details, detail)
	}
	return details, nil
}

func (s *lightControlService) Remove(userId int64, tenantId int, id int) *common.Errors {
	// 创建查询实例
	device := &dao.LightControl{
		ID:         id,
		IsDeleted:  1,
		UpdateUser: userId,
		UpdateTime: time.Now(),
	}
	err := device.Delete()
	if err != nil {
		return common.FailResponse(err.Error(), nil)
	}
	service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
		common.DeviceTypeLightControl, common.GetDeviceObject(device.ID, device.Name), common.OperationSuccess)
	return nil
}

func (s *lightControlService) GetList(tenantId int) ([]dao.LightControl, *common.Errors) {
	device := &dao.LightControl{
		TenantId:  tenantId,
		IsDeleted: 0,
	}
	devices, err := device.GetAllDevices()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}

	return devices, nil
}

func (s *lightControlService) Enable(id, status int) *common.Errors {
	// 创建查询实例
	device := &dao.LightControl{
		ID:       id,
		IsEnable: status,
	}

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

func (s *lightControlService) GetOne(id int) (*dao.LightControl, *common.Errors) {
	device := &dao.LightControl{
		ID: id,
	}
	err := device.GetDevice()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	return device, nil
}

func (s *lightControlService) GetByGroupId(groupId int) ([]dao.LightControl, *common.Errors) {
	// 创建查询实例
	device := &dao.LightControl{
		GroupId: groupId,
	}
	devices, err := device.GetDevicesByGroup()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	return devices, nil
}

func (s *lightControlService) GetByGateway(id int) []dao.LightControl {
	// 创建查询实例
	device := &dao.LightControl{
		GatewayId: id,
	}
	return device.GetDevicesByGateway()
}

func (s *lightControlService) GetByLampPole(id int) []dao.LightControl {
	// 创建查询实例
	device := &dao.LightControl{
		LampPoleId: id,
	}
	return device.GetDevicesByLampPole()
}

//检查灯控编号是否合规1-1——255-255
func checkControlNoIsCompliance(controlNo string) bool {
	arr := strings.Split(controlNo, "-")
	if len(arr) != 2 {
		return false
	}
	one, err := strconv.Atoi(arr[0])
	if err != nil || one < 1 || one > 255 {
		return false
	}
	two, err := strconv.Atoi(arr[1])
	if err != nil || two < 1 || two > 255 {
		return false
	}
	return true
}