package rrc

import (
	"os"
	"testing"

	"github.com/airbusgeo/godal"
	"gocv.io/x/gocv"
	"starwiz.cn/sjy01/image-proc/pkg/payload"
	"starwiz.cn/sjy01/image-proc/pkg/utils"
)

func TestFilter(t *testing.T) {
	os.MkdirAll("/Users/lan/workspace/sjy01/image-proc/data/fft", 0755)
	godal.RegisterAll()
	tif := "/Users/lan/workspace/sjy01/image-proc/data/052022-no-rrc/010/PAN/SJY01_PAN_20240520_115428_052022_103_010_L1A.tiff"
	// tif = "/Users/lan/workspace/sjy01/image-proc/data/051622/007/PAN/SJY01_PAN_20240516_101236_051622_096_007_L1A.tiff"
	// tif = "/Users/lan/workspace/sjy01/image-proc/data/060622-mm/003/PAN/SJY01_PAN_20240606_012004_060622_065_003_L1A.tiff"
	// tif = "/Users/lan/workspace/sjy01/image-proc/data/052022-no-rrc/010/MSS/SJY01_MSS_20240520_115428_052022_103_010_L1A.tiff"
	tif = "/Users/lan/workspace/sjy01/image-proc/data/052022-no-rrc/006/PAN/SJY01_PAN_20240520_115428_052022_103_006_L1A.tiff"
	tif = "/Users/lan/workspace/sjy01/image-proc/data/060622-RRC/002/PAN/SJY01_PAN_20240606_012004_060622_065_002_L1A.tiff"
	ds, err := godal.Open(tif)
	if err != nil {
		t.Error(err)
	}
	bands := ds.Bands()
	width := bands[0].Structure().SizeX
	height := bands[0].Structure().SizeY
	img := gocv.NewMatWithSize(height, width, gocv.MatTypeCV16UC1)

	data := make([]uint16, width*height)
	err = bands[0].Read(0, 0, data, width, height)
	if err != nil {
		t.Error(err)
	}

	// 将 16 位数据存储到图像的相应通道
	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			value := data[y*width+x]
			img.SetShortAt(y, x, int16(value))
		}
	}

	m0 := HFNoiseFilter(img, float64(img.Cols())*0.49)
	defer m0.Close()
	utils.SavePanToGDALGTiff(m0, 0, 0, "/Users/lan/workspace/sjy01/image-proc/data/fft/filtered.tiff", 1.3)
}

func TestMomentMatching(t *testing.T) {
	raw := "/Users/lan/workspace/sjy01/preprocessing/demo/output/051622/SJY01_PAN_20240516_101236_051622_096.RAW"
	data, err := os.ReadFile(raw)
	if err != nil {
		t.Error(err)
	}

	height := len(data) / (payload.PAN_PIXEL_WIDTH * 2)
	img, err := gocv.NewMatFromBytes(height, payload.PAN_PIXEL_WIDTH, gocv.MatTypeCV16U, data)
	if err != nil {
		t.Error(err)
	}

	DoMomentMatching(img)
}
