package main

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

type Point struct {
	Latitude  float64
	Longitude float64
}

var (
	Maxlenobj  int32 = 0
	pointcurve       = Point{39.73004426154644, 116.49248639463602}
	pointlist1       = []Point{pointcurve}
)

// 定时任务触发器固定的
func Topic() string {
	return pjisuv_ticker.TickerTopic
}

// ******* 禁止存在下划线_
// 触发器标记
func Label() string {
	return "FrontVehicleBrakeInCurve"
}
func IfEnter(pointlist []Point, radius float64, lat, lon float64) bool {
	// 判断是否进入点列表中的区域
	point1 := Point{Latitude: lat, Longitude: lon}
	for _, point := range pointlist {
		d := distance(point1, point)
		if d <= radius {
			return true
		}
	}
	return false
}

// 计算两点之间的距离(米)
func distance(point1, point2 Point) float64 {
	// 经纬度转弧度
	lat1 := point1.Latitude * math.Pi / 180
	lon1 := point1.Longitude * math.Pi / 180
	lat2 := point2.Latitude * math.Pi / 180
	lon2 := point2.Longitude * math.Pi / 180

	// 计算距离
	dlon := lon2 - lon1
	dlat := lat2 - lat1
	a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Sin(dlon/2)*math.Sin(dlon/2)*math.Cos(lat1)*math.Cos(lat2)
	c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
	d := 6371000 * c

	return d
}

func Rule(shareVars *sync.Map) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered from panic:", r)
		}
	}()
	// 1 使用goroutine

	go func(shareVars *sync.Map) {
		// 2 定义触发器的间隔时间
		ticker := time.NewTicker(time.Duration(3) * time.Second)
		defer ticker.Stop()
		// 3 运行一个无限循环
		for {
			select {
			// 定时器触发时执行的代码
			case <-ticker.C:
				FinalCallback(shareVars)

			}
		}
	}(shareVars)
}
func isBrake(ObjectList [][]float32) bool {
	for i, speed := range ObjectList[3] {

		if math.Abs(float64(ObjectList[1][i])) <= 5.3 && speed >= 6/3.6 && ObjectList[0][i] >= 1.3 {
			for j := 0; j < len(ObjectList[0])-i-1; j++ {
				if math.Abs(float64(ObjectList[1][1+i+j])) <= 5.3 && ObjectList[3][1+i+j] <= 1/3.6 {
					return true
				}
			}
		}
	}
	return false
}

func FinalCallback(shareVars *sync.Map) {
	OutsideWorkshopFlag, ok := shareVars.Load("OutsideWorkshopFlag")
	ObjDicOfTpperception, ok1 := shareVars.Load("objDicOfTpperception")
	Latitude, ok2 := shareVars.Load("Latitude")
	Longitude, ok3 := shareVars.Load("Longitude")
	ObjDic := ObjDicOfTpperception.(map[uint32][][]float32)

	if ok && ok1 && ok2 && ok3 && OutsideWorkshopFlag.(bool) == true {
		enterflag := IfEnter(pointlist1, 30.0, Latitude.(float64), Longitude.(float64))
		if enterflag {
			for _, objValue := range ObjDic {
				Maxlenobj = max(Maxlenobj, int32(len(objValue[0])))
				if len(ObjDic[0]) <= 10 || !isBrake(objValue) {
					continue
				}
				event_lable := "FrontVehicleBrakeInCurve"
				fmt.Println(event_lable)
				//ObjDicOfTpperception = make(map[uint32][][]float32)
				pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()}
			}
		}
		if Maxlenobj >= 100 {
			ObjDicOfTpperception = make(map[uint32][][]float32)
			shareVars.Store("ObjDicOfTpperception", ObjDicOfTpperception)
			Maxlenobj = 0
		}
	}

}