package main

import (
	"cicv-data-closedloop/pjisuv_msgs"
	"cicv-data-closedloop/pjisuv_param"
	"math"
	"reflect"
)

/*
   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"
}

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

func Rule(data *pjisuv_msgs.PerceptionObjects, param pjisuv_param.PjisuvParam) string {
	for _, obj := range data.Objs {
		if len(param.ObjSpeedDicOfTpperception) != 0 {
			if ContainsElement(param.ObjSpeedDicOfTpperception, obj.Id) {
				objSpeed := math.Pow(math.Pow(float64(obj.Vxabs), 2)+math.Pow(float64(obj.Vyabs), 2), 0.5)
				diffRate := math.Abs(objSpeed-param.ObjSpeedDicOfTpperception[obj.Id]) / (param.ObjSpeedDicOfTpperception[obj.Id] + 0.0001)
				if diffRate > 0.1 && objSpeed > 0.5 && param.ObjSpeedDicOfTpperception[obj.Id] > 0.5 {
					return "perceptionspeeddiffer"
				}
			}
		}
	}
	return ""
}

func ContainsElement(slice interface{}, element interface{}) bool {
	sliceValue := reflect.ValueOf(slice)
	if sliceValue.Kind() != reflect.Slice {
		return false
	}
	for i := 0; i < sliceValue.Len(); i++ {
		currentElement := sliceValue.Index(i).Interface()
		if reflect.DeepEqual(currentElement, element) {
			return true
		}
	}
	return false
}
