package lib

import (
	"fmt"
	"gonum.org/v1/plot"
	"gonum.org/v1/plot/plotter"
	"gonum.org/v1/plot/vg"
	"image/color"
	"time"
)

func HorizontalLine(xmin, xmax, y float64) *plotter.Line {
	pts := make(plotter.XYs, 2)
	pts[0].X = xmin
	pts[0].Y = y
	pts[1].X = xmax
	pts[1].Y = y
	line, err := plotter.NewLine(pts)
	if err != nil {
		panic(err)
	}
	line.LineStyle.Dashes = []vg.Length{vg.Points(8), vg.Points(5), vg.Points(1), vg.Points(5)}
	line.Color = color.RGBA{R: 255, A: 255}
	return line
}

type TimeTicks struct{}

func (TimeTicks) Ticks(min, max float64) []plot.Tick {
	tks := plot.TimeTicks{}.Ticks(min, max)
	for i, t := range tks {
		//if t.Label == "" { // Skip minor ticks, they are fine.
		//	continue
		//}
		tks[i].Label = time.Unix(int64(t.Value), 0).Format("2006-01-02 15:04:05")
	}

	return tks
}

type CommaTicks struct{}

// Ticks computes the default tick marks, but inserts commas
// into the labels for the major tick marks.
func (CommaTicks) Ticks(min, max float64) []plot.Tick {
	tks := plot.DefaultTicks{}.Ticks(min, max)
	for i, t := range tks {
		//if t.Label == "" { // Skip minor ticks, they are fine.
		//	continue
		//}
		tks[i].Label = fmt.Sprintf("%.0f", t.Value)
	}

	return tks
}

// 生成随机颜色的辅助函数
func RandomColor(i int) color.RGBA {
	var colors []color.RGBA
	colors = append(colors,
		color.RGBA{R: 52, G: 152, B: 219, A: 255},
		color.RGBA{R: 230, G: 126, B: 34, A: 255},
		color.RGBA{R: 142, G: 68, B: 173, A: 255},
		color.RGBA{R: 211, G: 84, B: 0, A: 255},
		color.RGBA{R: 231, G: 76, B: 60, A: 255},
		color.RGBA{R: 26, G: 188, B: 156, A: 255},
		color.RGBA{R: 243, G: 156, B: 18, A: 255},
		color.RGBA{R: 22, G: 160, B: 133, A: 255},
		color.RGBA{R: 46, G: 204, B: 113, A: 255},
		color.RGBA{R: 39, G: 174, B: 96, A: 255},
		color.RGBA{R: 41, G: 128, B: 185, A: 255},
		color.RGBA{R: 155, G: 89, B: 182, A: 255},
		color.RGBA{R: 192, G: 57, B: 43, A: 255},
		color.RGBA{R: 241, G: 196, B: 15, A: 255},
	)

	return colors[i%len(colors)]
}
