package auxilary

import (
	"os"

	log "github.com/sirupsen/logrus"
)

// 卫星时间起点 北京时间 2000-01-01 20:00:00
var (
	ReferenceTime2000 = 946728000
)

func ExtractAux(auxfile string) ([]*AuxFrameHead, []*AuxFocalBox, []*AuxPlatform, error) {
	data, err := os.ReadFile(auxfile)
	if err != nil {
		log.Println("read aux data from", auxfile, "error:", err.Error())
		return nil, nil, nil, err
	}

	var afh []*AuxFrameHead
	var afb []*AuxFocalBox
	var aps []*AuxPlatform

	for i := 0; i < len(data); i += 24 + 128 + 512 {
		if i+24+128+512 > len(data) {
			break
		}
		var head AuxFrameHead
		head.Decode(data[i : i+24])
		afh = append(afh, &head)

		var box AuxFocalBox
		box.Decode(data[i+24 : i+24+128])
		afb = append(afb, &box)

		var plat AuxPlatform
		plat.Decode(data[i+24+128 : i+24+128+512])
		aps = append(aps, &plat)
	}

	log.Printf("extract %d aux frame heads, %d aux focal boxes, %d aux platforms from %s",
		len(afh), len(afb), len(aps), auxfile)

	return afh, afb, aps, err
}

// 长光卫星姿态和GPS数据更新频率为 4 次/秒
func transfromGPSandAttMicrosec(microsec uint32) uint32 {
	unit := uint32(250000)
	microsec = (microsec / unit) * unit
	return microsec
}
