package main

import (
	"math"
	"github.com/gen2brain/raylib-go/raylib"
)

type Sketch struct {
	sourceWidth int32
	sourceHeight int32
	layerTools map[string]LayerTools
	layerToolsOrdered []LayerTools
	composite rl.RenderTexture2D
}

func NewSketch(sourceWidth, sourceHeight int32) Sketch {
	return Sketch {
		sourceWidth: sourceWidth,
		sourceHeight: sourceHeight,
		layerTools: make(map[string]LayerTools),
		layerToolsOrdered: []LayerTools {},
		composite: rl.LoadRenderTexture(sourceWidth, sourceHeight),
	}
}

func (s *Sketch) CreateLayer(name string, layer Layer, sourceWidth int32, sourceHeight int32) {
	texture := rl.LoadRenderTexture(sourceWidth, sourceHeight)
	layerTools := LayerTools {
		name:    name,
		texture: &texture,
		layer:   layer,
	}
	s.layerToolsOrdered = append(s.layerToolsOrdered, layerTools)
	s.layerTools[name] = layerTools
}

func (s *Sketch) Draw(ctx *RenderCtx) {
	// render onto all layer textures
	for _, instance := range s.layerToolsOrdered {
		instance.layer.Update(ctx)
		if instance.layer.IsDirty() {
			layer := instance.layer
			rl.BeginTextureMode(*instance.texture)
			layer.Draw(ctx)
			rl.EndTextureMode()
		}
	}

	// composite all layers to screen

	screen := rl.Rectangle {
		X:      0,
		Y:      0,
		Width:  float32(ctx.TargetWidth),
		Height: float32(ctx.TargetHeight),
	}

	// copy from full texture for compositing, with vertical flipping
	src := rl.Rectangle {
		X: 0, Y: 0,
		Width: float32(ctx.SourceWidth),
		Height: -float32(ctx.SourceHeight),
	}
	dst := rl.Rectangle {
		X: 0, Y: 0,
		Width: float32(ctx.SourceWidth),
		Height: float32(ctx.SourceHeight),
	}

	viewport := s.CalcViewport(ctx)

	rl.BeginTextureMode(s.composite)
	rl.ClearBackground(rl.Black)
	for _, instance := range s.layerToolsOrdered {
		rl.DrawTexturePro(instance.texture.Texture, src, dst, rl.Vector2{}, 0, rl.White)
	}
	rl.EndTextureMode()

	rl.DrawTexturePro(s.composite.Texture, viewport, screen, rl.Vector2{}, 0, rl.White)
}

func (s *Sketch) CalcViewport(ctx *RenderCtx) rl.Rectangle {
	viewportWidth := rl.Clamp(float32(ctx.SourceWidth) / ctx.Cam.Zoom, 0, float32(ctx.SourceWidth))
	viewportHeight := rl.Clamp(float32(ctx.SourceHeight) / ctx.Cam.Zoom, 0, float32(ctx.SourceHeight))
	return rl.Rectangle{
		X:      rl.Clamp(ctx.Cam.LookAt.X - viewportWidth/2.0, 0, float32(ctx.SourceWidth)-viewportWidth),
		Y:      rl.Clamp(ctx.Cam.LookAt.Y - viewportHeight/2.0, 0, float32(ctx.SourceHeight)-viewportHeight),
		Width:  float32(viewportWidth),
		Height: -float32(viewportHeight),
	}
}

func (s *Sketch) Update(ctx *RenderCtx) {
	if rl.IsMouseButtonDown(rl.MouseRightButton) {
		// get mouse delta from last frame
		delta := rl.GetMouseDelta()
		// compute the amount to move scaled by the camera zoom
		delta = rl.Vector2Scale(delta, -sourceScale/ctx.Cam.Zoom)
		delta.Y = -delta.Y
		ctx.Cam.LookAt = rl.Vector2Add(ctx.Cam.LookAt, delta)
	}

	// clamp LookAt to be somewhere on the texture
	ctx.Cam.LookAt.X = rl.Clamp(ctx.Cam.LookAt.X, 0, float32(ctx.SourceWidth-1))
	ctx.Cam.LookAt.Y = rl.Clamp(ctx.Cam.LookAt.Y, 0, float32(ctx.SourceHeight-1))

	// Zoom based on mouse wheel
	wheel := rl.GetMouseWheelMove()
	if wheel != 0 {
		const zoomIncrement float32 = 0.05
		if wheel > 0 {
			ctx.Cam.Zoom *= 1+zoomIncrement
		} else {
			ctx.Cam.Zoom *= 1-zoomIncrement
		}
	}

	// clamp zoom to > 1 so we don't ever zoom out more than necessary
	ctx.Cam.Zoom = rl.Clamp(ctx.Cam.Zoom, 1, math.MaxInt64)

}

type SketchCapture struct {
	compositeImage *rl.Image
	layerImages []*rl.Image
}

func (s *Sketch) Capture() *SketchCapture {
	composite := rl.LoadImageFromTexture(s.composite.Texture)
	layerImages := make([]*rl.Image, len(s.layerToolsOrdered))
	for i, layerTool := range s.layerToolsOrdered {
		layerImages[i] = rl.LoadImageFromTexture(layerTool.texture.Texture)
	}
	return &SketchCapture {
		compositeImage: composite,
		layerImages: layerImages,
	}
}


type LayerTools struct {
	name    string
	layer   Layer
	texture *rl.RenderTexture2D
}

/** Layer **/

type Layer interface {
	Update(ctx *RenderCtx)
	Draw(ctx *RenderCtx)
	IsDirty() bool
}

type TestPattern struct{
	dirty bool
}

func (tp *TestPattern) Update(ctx *RenderCtx) {
	;
}

func (tp *TestPattern) Draw(ctx *RenderCtx) {

	rl.ClearBackground(rl.Black)
	centerX := float32(ctx.SourceWidth) / 2
	centerY := float32(ctx.SourceHeight) / 2

	rl.DrawRectangleRec(rl.Rectangle{X: 0, Y: 0, Width: centerX, Height: centerY}, rl.Red)
	rl.DrawRectangleRec(rl.Rectangle{X: centerX, Y: 0, Width: centerX, Height: centerY}, rl.Green)
	rl.DrawRectangleRec(rl.Rectangle{X: 0, Y: centerY, Width: centerX, Height: centerY}, rl.Blue)
	rl.DrawRectangleRec(rl.Rectangle{X: centerX, Y: centerY, Width: centerX, Height: centerY}, rl.White)

	rl.DrawLine(0, 0, ctx.SourceWidth, ctx.SourceHeight, rl.Black)

	rl.PushMatrix()
	rl.Translatef(centerX, centerY, 0)
	rl.SetLineWidth(4.0)
	rl.DrawLine(-10000, 0, 10000, 0, rl.Red)
	rl.DrawLine(0, -10000, 0, 10000, rl.Green)
	rl.DrawRectangleLines(-50, -50, 100, 100, rl.Magenta)
	rl.PopMatrix()

	tp.dirty = false
}

func (tp *TestPattern) IsDirty() bool {
	return tp.dirty
}

/** Ports **/

type Ports map[string]Signal

func MakePorts() Ports {
	return make(Ports)
}

/**
 * materialize current value for all ports
 **/
func (p Ports) Eval(t float64) map[string]float64 {
	out := make(map[string]float64, len(p))
	for name, sig := range p {
		out[name] = sig.Eval(t)
	}
	return out
}

/** RenderCtx **/

type RenderCtx struct {
	TargetWidth  int32
	TargetHeight int32
	SourceWidth  int32
	SourceHeight int32
	Time         float64
	Ports        map[string]float64
	Cam          *TextureCam
}
