package service

import (
	"fmt"
	"github.com/goccy/go-json"
	"iot_manager_service/app/device/dao"
	"iot_manager_service/app/device/edge_service"
	"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 InfoBoardService = new(infoBoardService)

type infoBoardService struct{}

func (s *infoBoardService) Get(id int) (model.InfoBoardDetail, *common.Errors) {
	// 创建查询实例
	device := &dao.InfoBoard{
		ID: id,
	}
	getDevice, err := device.GetDevice()
	if err != nil {
		return model.InfoBoardDetail{}, common.FailResponse(err.Error(), nil)
	}
	detail := s.rewriteBoardDetail(getDevice)
	return detail, nil
}

func (s *infoBoardService) CreateOrUpdate(userId int, tenantId string, req dao.InfoBoard) *common.Errors {
	// 创建查询实例
	device := req
	device.TenantId = tenantId
	device.UpdateUser = userId
	device.UpdateTime = time.Now()
	if req.ID == 0 {
		device.CreateTime = time.Now()
		device.CreateUser = userId
		if device.IsExistedBySN() {
			logger.Logger.Errorf("Create GetDeviceID err \n")
			return common.ParamsInvalidResponse("编码不能重复,请重新填写!", 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.DeviceTypeInfoBoard, common.GetDeviceObject(device.ID, device.InfoName), common.OperationSuccess)
		return common.SuccessResponse(common.Succeeded, nil)
	}
	if device.IsExistedByNameAndCode() {
		return common.ParamsInvalidResponse("编码不能重复,请重新填写!", 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.DeviceTypeInfoBoard, common.GetDeviceObject(device.ID, device.InfoName), common.OperationSuccess)
	return common.SuccessResponse(common.Succeeded, nil)
}

func (s *infoBoardService) List(searchValue string, current, size int) ([]model.InfoBoardDetail, int64, *common.Errors) {
	device := dao.InfoBoard{}
	if searchValue != "" {
		device.Sn = searchValue
		device.InfoName = searchValue
	}

	offset := (current - 1) * size
	limit := size
	devices, total, err := device.GetDevices(offset, limit)
	if err != nil {
		return nil, 0, common.FailResponse(err.Error(), nil)
	}
	var details []model.InfoBoardDetail
	for _, d := range devices {
		detail := s.rewriteBoardDetail(d)
		details = append(details, detail)
	}
	return details, total, nil
}

// 从缓存中读取真实状态
func (s *infoBoardService) rewriteBoardDetail(d dao.InfoBoard) model.InfoBoardDetail {
	endTime, state := cache.GetDeviceState(d.Sn)

	detail := model.InfoBoardDetail{
		InfoBoard:    d,
		RunState:     state,
		NetworkState: state,
		EndLineTime:  endTime,
	}
	cacheMap := cache.GetDeviceLedData(d.Sn)

	if cacheMap != nil {
		marshal, err := json.Marshal(cacheMap)
		if err != nil {
			logger.Logger.Errorf("rewriteBoardDetail err1 = %v \n", err)
			return detail
		}

		var cltData model.CltledData
		err = json.Unmarshal(marshal, &cltData)
		if err != nil {
			logger.Logger.Errorf("rewriteBoardDetail err2 = %v \n", err)
			return detail
		}
		detail.Power = "边缘程序未升级"

		if cltData.RealWidth != "" {
			detail.ResolutionName = fmt.Sprintf("%v * %v", cltData.RealWidth, cltData.RealHeight)
			detail.OnTheAir = cltData.Playing

			detail.Temperature = cltData.Colortemperature
			f, err := strconv.ParseFloat(cltData.Brightness, 64)
			if err != nil {
				logger.Logger.Errorf("rewriteBoardDetail err3 = %v \n", err)
				return detail
			}
			mapRange := common.MapRange(f, 0, 255, 1, 100)
			detail.Luminance = fmt.Sprintf("%v %%", int(mapRange))

			f, err = strconv.ParseFloat(cltData.Musicvolume, 64)
			if err != nil {
				logger.Logger.Errorf("rewriteBoardDetail err3 = %v \n", err)
				return detail
			}
			mapRange = common.MapRange(f, 0, 15, 1, 100)
			detail.Volume = fmt.Sprintf("%v %%", int(mapRange))

			detail.RebootTime = cltData.RebootTime
			detail.Power = "开机"
			if cltData.Powerstatus != "1" {
				detail.Power = "关机" //真实其实是睡眠状态
			}
			if cltData.WakeupTime != "" {
				detail.NeverCloseDown = 2
				//detail.BootStartTime = cltData.WakeupTime
				//detail.BootEndTime = cltData.SleepTime
				detail.RealStartAndEndTime = fmt.Sprintf("%v-%v", cltData.WakeupTime, cltData.SleepTime)
			}
		}
	}

	return detail
}

func (s *infoBoardService) Remove(userId int, tenantId string, id int) *common.Errors {
	// 创建查询实例
	device := &dao.InfoBoard{
		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.DeviceTypeInfoBoard, common.GetDeviceObject(device.ID, device.InfoName), common.OperationSuccess)
	return nil
}

func (s *infoBoardService) GetByGateway(gatewayId int) []dao.InfoBoard {
	device := &dao.InfoBoard{
		GatewayId: gatewayId,
	}
	return device.GetDevicesByGateway()
}

func (s *infoBoardService) GetByLampPole(lampPoleId int) []dao.InfoBoard {
	device := &dao.InfoBoard{
		LampPoleId: lampPoleId,
	}
	return device.GetDevicesByLampPole()
}

func (s *infoBoardService) GetByResolution(tenantId string, resolution int) []dao.InfoBoard {
	device := &dao.InfoBoard{
		Resolution: resolution,
		TenantId:   tenantId,
	}
	return device.GetDevicesByResolution()
}

func (s *infoBoardService) GetByIds(tenantId string, ids string) []dao.InfoBoard {
	device := &dao.InfoBoard{
		TenantId: tenantId,
	}
	return device.GetDevicesByIds(ids)
}

// EdgeCmd 发送远程 命令 ,如果是  cmdNum=101 or 6 时 ,必需要带id
func (s *infoBoardService) EdgeCmd(tenant, sn string, cmdNum, id int) {
	action := "cmd"
	var edgePost edge_service.CltLedControlReqPost
	edgePost.Codes = []string{sn}
	param := make(map[string]interface{})
	switch cmdNum {
	case 1: //关机
		param["command"] = "sleep"
		s.reqEdge(tenant, edgePost, param, action)
	case 2: //开机
		param["command"] = "wakeup"
		s.reqEdge(tenant, edgePost, param, action)
	case 3: //重启
		param["command"] = "reboot"
		s.reqEdge(tenant, edgePost, param, action)
	//case 4: //重新节目,不在此处,处理了
	case 6: //更新排程
		device := &dao.InfoBoard{
			ID: id,
		}
		dev, _ := device.GetDevice()
		action = "ssched"
		if dev.NeverCloseDown == 1 {
			param["sleep"] = ""
			param["wakeup"] = ""
		} else {
			sleep, wakeup := ledTimeFormat(dev.BootEndTime, dev.BootStartTime)
			param["sleep"] = sleep
			param["wakeup"] = wakeup
		}
		param["reboot"] = "12:30:59" //每日重启时间
		s.reqEdge(tenant, edgePost, param, action)
	case 101: //更新亮度 音量, 这里需要做成定时任务去跑
		device := &dao.InfoBoard{
			ID: id,
		}
		dev, err := device.GetDevice()
		if err == nil && dev.Condition != "" {
			split := strings.Split(dev.Condition, ",")
			brightness := split[0]
			musicvolume := split[1]
			if isNight() {
				//晚上不同亮度和音量
				brightness = split[2]
				musicvolume = split[3]
			}
			// 亮度
			f, _ := strconv.ParseFloat(brightness, 64)
			mapRange := common.MapRange(f, 0, 100, 0, 255)
			param1 := make(map[string]interface{})
			param1["brightness"] = int(mapRange)
			action = "sb"
			s.reqEdge(tenant, edgePost, param1, action)
			//音量
			f1, _ := strconv.ParseFloat(musicvolume, 64)
			mapRange2 := common.MapRange(f1, 0, 100, 0, 15)
			param2 := make(map[string]interface{})
			param2["musicvolume"] = int(mapRange2)
			action = "svol"
			s.reqEdge(tenant, edgePost, param2, action)
		}
	}
}

// 公用请求边缘云端
func (s *infoBoardService) reqEdge(tenant string, edgePost edge_service.CltLedControlReqPost, param map[string]interface{}, action string) {
	edgePost.Param = param
	req := edge_service.CltLedControlReq{
		Tenant:   tenant,
		Action:   action,
		PostData: edgePost,
	}
	edge_service.CltLedControlService{}.RequestApi(req)
}

// 发布led信息屏节目
func (s *infoBoardService) EdgePushLedProgram(tenant, sn, playJson string) {
	var edgePost edge_service.CltLedControlReqPost
	edgePost.Codes = []string{sn}
	param := make(map[string]interface{})
	json.Unmarshal([]byte(playJson), &param)
	s.reqEdge(tenant, edgePost, param, "spgms")
}

// 判断是否晚上
func isNight() bool {
	now := time.Now()
	hour := now.Hour()
	if hour >= 18 || hour < 6 {
		return true
	} else {
		return false
	}
}

// ledTimeFormat led开始 结束时间格式化
func ledTimeFormat(stime string, etime string) (string, string) {
	startTime := fmt.Sprintf("%v %v:59", time.Now().Format("2006-01-02"), stime)
	endTime := fmt.Sprintf("%v %v:59", time.Now().Format("2006-01-02"), etime)

	ktime, _ := time.Parse("2006-01-02 15:04:05", startTime)
	jtime, _ := time.Parse("2006-01-02 15:04:05", endTime)

	atoi1, _ := strconv.Atoi(ktime.Format("15"))
	atoi2, _ := strconv.Atoi(jtime.Format("15"))

	return fmt.Sprintf("%v:%v:59", atoi1, ktime.Format("4")), fmt.Sprintf("%v:%v:59", atoi2, jtime.Format("4"))
}

func (s *infoBoardService) DeviceCount() dao.DeviceStatus {
	var rsp dao.DeviceStatus
	d := dao.InfoBoard{}
	rsp.Set(d.DeviceCount1())
	return rsp
}

func (s *infoBoardService) ListDevices() []dao.Device {
	d := dao.InfoBoard{}
	return d.ListDevices1()
}