package types

type Rect struct {
	X, Y, W, H int
}

type RectFill struct {
	Top, Bottom, Left, Right, TopLeft, TopRight, BottomLeft, BottomRight, Body interface{}
}

func NewRect(x, y, w, h int) Rect {
	return Rect{x, y, w, h}
}

func NewCenteredRect(source Rect, w, h int) Rect {
	targetX := source.X + source.W / 2 - w / 2
	targetY := source.Y + source.H / 2 - h / 2
	return Rect{targetX, targetY, w, h}
}

func (r *Rect) Blit(fillage RectFill, layer Putable) {

	if fillage.Body != "" {
		for i := r.X+1; i < r.X+r.W - 1; i++ {
			for j := r.Y + 1; j < r.Y+r.H; j++ {
				layer.Put(i, j, fillage.Body)
				//lii.Put(i, j, "X");
			}
		}
	}

	for i := r.X; i < r.X+r.W - 1; i++ {
		layer.Put(i, r.Y, fillage.Top)
		//lii.Put(i, Y-1, "Q");
		layer.Put(i, r.Y+r.H - 1, fillage.Bottom)
		//lii.Put(i, Y+H, "H");
	}

	for j := r.Y; j < r.Y+r.H; j++ {
		layer.Put(r.X, j, fillage.Left)
		//lii.Put(X-1, j, "U");
		layer.Put(r.X+r.W - 1, j, fillage.Right)
		//lii.Put(X+W, j, "M");
	}

	layer.Put(r.X, r.Y, fillage.TopLeft)
	//lii.Put(X-1, Y-1, "T");
	layer.Put(r.X, r.Y+r.H - 1, fillage.BottomLeft)
	//lii.Put(X-1, Y+H, "q");
	layer.Put(r.X+r.W - 1, r.Y, fillage.TopRight)
	//lii.Put(X+W, Y-1, "L");
	layer.Put(r.X+r.W - 1, r.Y+r.H - 1, fillage.BottomRight)
}

func (self *Rect) Intersects(other Rect) bool {
	if self.X <= (other.X+other.W) &&
		(self.X+self.W) >= other.X &&
		self.Y <= (other.Y+other.Y) &&
		(self.Y+self.H) >= other.Y {
		return true
	}
	return false
}

func (r *Rect) InBounds (c Coords) bool {
	return c.X >= r.X && c.X <= (r.X + r.W -1) && c.Y >= r.Y && c.Y <= (r.Y + r.H -1)
}