package mapgens

import (
	"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
	"lab.zaar.be/thefish/alchemyst-go/engine/types"
	"lab.zaar.be/thefish/alchemyst-go/util"
	"lab.zaar.be/thefish/alchemyst-go/util/delaunay"
)

func DelaunayMstExtGen(l *gamemap.Level) (*gamemap.Level, []gamemap.Room) {

	rng := util.NewRNG()

	//fill with walls
	for i := 0; i < l.W; i ++ {
		for j := 0; j < l.H; j++ {
			l.SetTileByXY(i, j, gamemap.NewWall())
		}
	}
	rooms := GetRandomRoomList(rng, l, maxrooms, minRoomSize, maxRoomSize)

	BlitToLevel(l, rooms)

	centers := make([]types.Coords, 0)
	for _, room := range rooms {
		centers = append(centers, room.Center)
	}
	edges := delaunay.GetMst(centers, l.W, l.H, l.W) //get negative Weights
	outlyingCorridors := make([]types.Edge, 0)
	outlyingCorrCount := 0
	for _, edge := range edges {
		MedianStraight(rng, l, rooms, centers, edge)
		outlyingCorridors = append(outlyingCorridors, edge)
		outlyingCorrCount++
		if outlyingCorrCount > 8 {
			break
		}
	}

	edges = delaunay.GetMst(centers, l.W, l.H, 0) //get negative Weights
	for _, edge := range edges {
		MedianStraight(rng, l, rooms, centers, edge)
	}

	return l, rooms
}

