package main

import (
	"cicv-data-closedloop/pjisuv_msgs"
	"fmt"
	"math"
	"sync"
)

/*
   if obj_speed_dic is not {}:  #相邻两帧对于同一目标物速度变化率超过设定阈值
       if obj.id in obj_speed_dic:

           objspeed=pow(pow(obj.vxabs,2)+pow(obj.vyabs,2),0.5)
           diff_rate=abs(objspeed-obj_speed_dic[obj.id])/(obj_speed_dic[obj.id]+0.0001)
           if diff_rate>0.1 and objspeed>0.5 and obj_speed_dic[obj.id]>0.5:
               event_label='perceptionspeeddiffer'

               print(event_label)
               print(objspeed,"   ",obj_speed_dic[obj.id],"   ",diff_rate)
           else:
               print(objspeed,"   ",obj_speed_dic[obj.id],"   ",diff_rate)
*/

func Topic() string {
	return "/tpperception"
}

// 禁止存在下划线_
func Label() string {
	return "PerceptionSpeedDiffer"
}

func Rule(shareVars *sync.Map, data *pjisuv_msgs.PerceptionObjects) string {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered from panic:", r)
		}
	}()
	value, ok := shareVars.Load("ObjSpeedDicOfTpperception")
	if ok {
		objSpeedDicOfTpperception := value.(map[uint32]float64)
		for _, obj := range data.Objs {
			if len(objSpeedDicOfTpperception) != 0 {
				if _, exist := objSpeedDicOfTpperception[obj.Id]; exist {
					objSpeed := math.Pow(math.Pow(float64(obj.Vxabs), 2)+math.Pow(float64(obj.Vyabs), 2), 0.5)
					diffRate := math.Abs(objSpeed-objSpeedDicOfTpperception[obj.Id]) / (objSpeedDicOfTpperception[obj.Id] + 0.0001)
					if diffRate > 0.1 && objSpeed > 0.5 && objSpeedDicOfTpperception[obj.Id] > 0.5 {
						return Label()
					}
				}
			}
		}
	}

	return ""
}