// MIT license. Run in an empty module with wavesfit-go v0.1.0 as a dependency.
package main

import (
	"context"
	"fmt"
	"math"

	wf "github.com/AlessandroLodi/wavesfit-go"
)

func main() {
	gaussian := wf.Voigt(.5, 1, 0, 1, 0) / wf.Voigt(0, 1, 0, 1, 0)
	lorentzian := wf.Voigt(.5, 1, 0, 0, 1) / wf.Voigt(0, 1, 0, 0, 1)
	ds := wf.DoniachSunjic(0, 1, 0, 1, 0, "higher")
	fmt.Printf("Gaussian half maximum: %.6f\n", gaussian)
	fmt.Printf("Lorentzian half maximum: %.6f\n", lorentzian)
	fmt.Printf("DS origin at alpha=0: %.6f\n", ds)
	x, y := make([]float64, 301), make([]float64, 301)
	for i := range x {
		x[i] = -4 + 8*float64(i)/300
		y[i] = .04 + wf.Voigt(x[i], 3, .4, .8, .3)
	}
	model, err := wf.DefaultModel("voigt", 1)
	if err != nil {
		panic(err)
	}
	result, err := wf.Fit(context.Background(), x, y, nil, model,
		wf.BackgroundConfig{Kind: "none"}, wf.FitOptions{BudgetSeconds: 5}, nil)
	if err != nil {
		panic(err)
	}
	if !result.Success || math.Abs(result.Parameters["p1.center"]-.4) > 1e-5 {
		panic("recovery failed")
	}
	fmt.Printf("Fit status: %s\n", result.Status)
	fmt.Printf("Recovered center: %.6f eV\n", result.Parameters["p1.center"])
	fmt.Printf("Recovered area: %.6f\n", result.Parameters["p1.area"])
}
