-----------------------------------------------------------
-- DRAWING PANEL
-----------------------------------------------------------
function editor.defineDrawingPanel()
	config.currentBrush = config.currentBrush or {}
	drawingPanel = 
	{ 
		name 	= "Drawing",
		hide 	= true,
		docked	= true,
		backing = false,
		color 	= config.currentColor or {h=0.5,s=0.5,v=0.5,r=64,g=128,b=128},
		vector = true,
		brush  	= config.currentBrush.brush or { 
			color 	 = {h=0.5,s=0.5,v=0.5,r=64,g=128,b=128},
			shape 	 = 1,
			flow 	 = 1,
			texture  = 0,
			follow	 = false,

			size 	 = 6,
			opacity  = 0.5,
			angle 	 = 0.5,
			flatness = 1,
			spacing  = 0,
			scatter  = 0,
		},

		pen = config.currentPen or {
			size = 2,
			ends = "round",
			group= "line",
			stabilizer=0,
			smoothing=0,
		},

		penGroups = {
			"rough",
			"line",
			"note"
		},

		penShortcuts = {
			"Z",
			"X",
			"C"
		},

		eraseGroups={
			"all pen",
			"last pen",
		},
  
		eraseGroup="last pen",
		selectGroup="last pen",

		penColors = {
			line = {0,0,0,1},
			rough = {0.6,0.6,0.9,1}, 
			note = {1,0.5,0.5}
		},

		penLayers = {
			rough = 0.4, 
			fill  = 0.3,
			line = 0.2,
			note = 0.1
		},

		sliding=nil,


		fillRadius = 0,
		sliders =  {
			size 		= {name="size",		 stroke=true, pct=0.2,  jitter=0 },
			opacity 	= {name="opacity",	 stroke=true, pct=1, 	jitter=0 },
			angle 		= {name="angle",	 stroke=true, pct=0.5, 	jitter=0 },
			flatness  	= {name="flatness",	 stroke=true, pct=1, 	jitter=0 },
			spacing  	= {name="spacing",	 stroke=true, pct=0, 	jitter=0 },
			scatter  	= {name="scatter",	 stroke=true, pct=0, 	jitter=0 },
		},

		eraser  	= { 
			size = 6,
		},

		states 	= 	{
	 		on 	= {r=130,g=100,b=130},
	 		off = {r=90,g=120,b=150},
	 		non = {r=80,g=80,b=80}
	 	},

	 	recentColors 	= config.recentColors  or {},
	 	recentBrushes 	= config.recentBrushes or {},
		wheel 			= {},
		palettes 		= {},
		currentPalette 	= 1,

		numPals = 15,
	 	height 	= 136,
	 	recentsHeight  = 20,
	 	squareSize = 30,

	 	open 	= false,
	 	y 		= 0,
	 	row 	= sidebar.row,

		fillTolerance = 0,
		fillGap = config.fillGap or 0,

	 	currentStrip 	= "",
	 	currentJitter 	= "",
	 	numStrips 	 	= 40,
	 	palw 		 	= 112,

	 	hue  		= config.currentColor and config.currentColor.h or 0.5,
	 	saturation 	= config.currentColor and config.currentColor.s or 0.5,
	 	value 	 	= config.currentColor and config.currentColor.v or 0.5,

	 	colorJitter = config.currentBrush.colorJitter or {
	 		hue  				= {jitter=0,stroke=true},
		 	saturation 	= {jitter=0,stroke=true},
		 	value 	 		= {jitter=0,stroke=true},
		 },

		tools = {"brush","erase","fill","move","select"},--,"verts"},
		fillModes = {"brush","line","last","both"},
		eraseModes={"stroke","segment","circle","pixel","both"},
		eraseModesVec={"stroke","segment","circle"},

		tool = "brush",
	 	eraseMode = "stroke",
	 	fillMode = "line",

	 	update 	= function(x) editor.drawingPanelUpdate(x) end
	}
	drawingPanel.pen.smoothing=0
	drawingPanel.sliders.size.min=0.5 		drawingPanel.sliders.size.max=24
	drawingPanel.sliders.opacity.min=0 		drawingPanel.sliders.opacity.max=1 
	drawingPanel.sliders.angle.min=-pi 		drawingPanel.sliders.angle.max=pi 
	drawingPanel.sliders.flatness.min=0 	drawingPanel.sliders.flatness.max=1 
	drawingPanel.sliders.spacing.min=0 		drawingPanel.sliders.spacing.max=1 
	drawingPanel.sliders.scatter.min=0 		drawingPanel.sliders.scatter.max=1 

end

function editor.setPenGroup(group)
	if shiftDown then 
		for s,stroke in ipairs(animationPanel.f.strokes) do
			if stroke.group == drawingPanel.pen.group then 
				stroke.group = group
			end
		end
		editor.refreshStrokes()
	else 
		-- drawingPanel.tool = "brush"
		drawingPanel.vector = true
		drawingPanel.pen.group = group
	end
	editor.tempTip("Pen Mode: "..capFirst(group))
end

function editor.drawingPanelUpdate(panel)

	panel.penLayers = {
			rough = 0.4, 
			fill  = 0.3,
			line = 0.2,
			note = 0.1
		}
		panel.penColors = {
			line = {0,0,0,1},
			rough = {0.6,0.7,1,0.1}, 
			note = {1,0.45,0.45}
		}
	panel.hide=(not animationPanel.open or panel.docked)

	if not animationPanel.open 	then return  end

	if animationPanel.l.format=="vector" then 
		drawingPanel.vector = true
	end

	editor.changeBrushSize 		(panel)
	
	if mouseIn(panel) 		then 	lmouse.setCursor(defaultCursor) end

	if panel.docked then 
		editor.togglePanel 			(panel)
		panel.name = "Drawing"
	else
		panel.width = 213
		panel.name = ""
	end

	editor.selectTool 			(panel)

	if panel.hide then return   end 

	gfx.fontSize(9)
	
	editor.toolSelect 			(panel)
	editor.brushSettings 		(panel)

end


function editor.selectTool(panel)
	if animationPanel.canvasClicked or shiftDown or cmdDown or lmouse.getRelativeMode() then return end
	
	if 		keypressed("e") then 
		panel.tool 		= "erase"
		
 	elseif 	keypressed("b") then 
 		panel.tool 		= "brush"

 	elseif 	keypressed("p") then 
 		panel.tool 		= "brush"

	elseif 	keypressed("d") then 
 		panel.tool 		= panel.tool == "brush" and "erase" or "brush"

	elseif 	keypressed("f") then 
 		panel.tool 		= "fill"
 		if panel.vector then 
 			editor.prepareFill(animationPanel) 
 		end
 		print("fill prepared")

 	elseif 	keypressed("t") then 
 		panel.tool 		= "move"

	elseif 	not cmdDown and keypressed("v") then 
 		panel.tool 		= "move"

 	elseif 	keypressed("l") then 
 		panel.tool 		= "select"

 	end
end

function editor.applyBrushJitter( panel, tip )
	-- for k,v in pairs(panel.sliders) do
	-- 	if v.stroke == tip then
	-- 		panel.brush[k] = lerp( v.min, v.max, mc(0,v.pct-(v.pct*math.random()*v.jitter),1))
	-- 	end
	-- end

	panel.brush.color.h = (panel.colorJitter.hue.stroke        	==tip) and panel.color.h 		+(math.random()-0.5)*panel.colorJitter.hue.jitter  		 	or panel.brush.color.h
	panel.brush.color.s = (panel.colorJitter.saturation.stroke 	==tip) and panel.color.s 		+(math.random()-0.5)*panel.colorJitter.saturation.jitter 	or panel.brush.color.s
	panel.brush.color.v = (panel.colorJitter.value.stroke 		==tip) and panel.color.v 		+(math.random()-0.5)*panel.colorJitter.value.jitter 		or panel.brush.color.v

	panel.brush.color.r,panel.brush.color.g,panel.brush.color.b = hsvToRgb(
		panel.brush.color.h,
		panel.brush.color.s,
		panel.brush.color.v,
		1
	)
	panel.brush.color.a = panel.brush.opacity
end

function editor.saveBrush(panel)
	
end

function editor.pushBrush(panel)
	local savedBrush = {
		brush  		= panel.brush,
		sliders 	= panel.sliders,
		colorJitter = panel.colorJitter
	}
	table.insert(panel.recentBrushes,1,savedBrush)
	panel.recentBrushes = lume.slice(panel.recentBrushes,1,10)
	return savedBrush
end


function  editor.showRecentColors(panel)
	if lume.count(panel.recentColors or {}) == 0 then 
		return nil
	end
	local colors = {}
	for i=1,9,1 do 
		if panel.recentColors[i] then 
			table.insert(colors,{
				col 		= panel.recentColors[i],
				func=function(x)
					panel.color.h = panel.recentColors[i].h
					panel.color.s = panel.recentColors[i].s
					panel.color.v = panel.recentColors[i].v
					panel.color.r	= panel.recentColors[i].r
					panel.color.g	= panel.recentColors[i].g
					panel.color.b	= panel.recentColors[i].b
					
					return ""
				end,
				update=function(x) 
					x.col.a=x.hovered and 0.8 or 1
				end
			})
		else
			table.insert(colors,{size=-1})
		end
	end
	return colors
end

function editor.contextPalettes()

	return nil
end

function editor.pushColor(panel,color)
	local color = color or panel.color
	local col = {
			h = color.h
		,	s = color.s
		,	v = color.v
		,	r = color.r
		,	g = color.g
		,	b = color.b
		, a = 1
	}
	panel.recentColors = panel.recentColors or {}
	for c,col2 in ipairs(panel.recentColors) do 
		if col.r == col2.r and col.g == col2.g and col.b == col2.b then 
			table.remove(panel.recentColors,c)
		end
	end
	table.insert(panel.recentColors,1,col)
	if panel.isPanel then 
		panel.recentColors = lume.slice(panel.recentColors,1,25)
	end
	return col
end


function brushVal()
	return 
end

function editor.changeBrushSize(panel)
	-- SIZE CHANGE
	local whichBrush =  panel.tool=="erase" and panel.eraser or panel.brush
	if not shiftDown and not cmdDown then 
		whichBrush.size= mc(0.1, keypressed("]") and whichBrush.size*1.5 or (keypressed("[") and whichBrush.size*0.7 or whichBrush.size), panel.sliders.size.max )
		if (lkeyboard.isDown("[") or lkeyboard.isDown("]")) and panel.tool=="brush" then panel.sliders.size.pct = panel.brush.size/panel.sliders.size.max  end
	end
end



function editor.pickColor(panel)
	if panel.isPanel then 
		local r,g,b,a = editor.pickerCanvas:newImageData():getPixel(0, 0)
		if not level.isAnim then 
			r,g,b=cpml.color.linear_to_gamma(r, g, b)
		end
		editor.colorToPick = {r,g,b}
		panel.color.r,panel.color.g,panel.color.b = unpack(editor.colorToPick)
		panel.color.h,panel.color.s,panel.color.v = rgbToHsv(panel.color.r,panel.color.g,panel.color.b,1)
		editor.pushColor(panel)
		animationPanel.canvasClicked=false
	else
		local r,g,b,a = canvasMain:newImageData():getPixel( mouse.x*game.resolution,mouse.y*game.resolution )
		panel.r,panel.g,panel.b = r*1.062,g*1.062,b*1.062
		panel.h,panel.s,panel.v = rgbToHsv(panel.r,panel.g,panel.b,1)
		editor.pushColor(editor,panel)
	end
end



