namer={}

function namer:define()
	self.enabled 		= false
	self.world 			= 1
	self.index 			= 1
	self.type 			= "nil"
	self.name 			= ""
	self.text 			= ""
	self.alertText 		= ""
	self.alertTimer 	= 0
	self.alertMax 		= 180
	self.backOpacity 	= 0
	self.maxOpacity  	= 0.7
	self.mode  			=  "new"
	self.width 			= 250
	self.height 		= 160
	self.x 				= 400
	self.y 				= 800
	self.target 		= {x = 0, y=0}
	self.options 		= {}
end

-----------------------------------------------------------
-- OPEN AND CLOSE
-----------------------------------------------------------
function namer.open(target)
	namer.enabled 	= true
	namer.text 		= ""
	namer.width 	= editingLevel and sidebar.width-16 or 250
	namer.simple	= target  or false
	namer.target 	= target  or  {x = (screenWidth-namer.width)/2, y = (screenHeight-namer.height)/2}

end

function namer:close()
	self.enabled 	= false
	unlockMouse() 
end

-----------------------------------------------------------
-- NEW OR EDIT
-----------------------------------------------------------
function namer:new(noc)
	self.open() 
	self.world  	= noc 
	self.type  		= "level"
	self.mode 		= "new"

	self.options = {
		{ name="Okay" 	, x=0 , y=0 , width=0 , height=0 , col={227,227,227} }
	}
end 

function namer:edit(block)
	self.open() 	
	self.world  	= block.stackNum 
	self.index  	= block.index
	self.mode 		= "edit"
	self.text 		= block.name
	self.key 		= block.key
	self.type 		= "level"

	self.options = {
		{ name="Delete" , x=0 , y=0 , width=0 , height=0 , col={237,36,6} },
		{ name="Edit" 	, x=0 , y=0 , width=0 , height=0 , col={223,223,80} },
		{ name="Play" 	, x=0 , y=0 , width=0 , height=0 , col={57,227,57} },
		{ name="Okay" 	, x=0 , y=0 , width=0 , height=0 , col={227,227,227} }
	}
end 

function namer:warning(warning,func)

	editor.closeContextMenu()
	self.open()
	self.mode 		= "warning"
	self.type 		= ""
	self.warnFunc 	= func 
	self.text 		= "Are you sure you want to "..warning.."?"
	self.options = {
		{ name="Nah" , x=0 , y=0 , width=0 , height=0 , col={237,36,6} },
		{ name="Sure", x=0 , y=0 , width=0 , height=0 , col={57,227,57} }
	}

	if cmdDown or not editor.warnings then 
		namer.optionsClick("Sure") 
		return 
	end
end

function namer:alert(alert)
	self.open()
	self.mode 		= "alert"
	self.type 		= ""
	self.text 		= alert
	self.options = {
		{ name="Okay", x=0 , y=0 , width=0 , height=0 , col={57,227,57} }
	}
end

function namer:typeVal(var,val,target)
	editState 		= "Typing"
	self.open 		(target)
	self.mode 		= "typing"
	self.type 		= (target.item.category=="Variables") and "Value" or target.item.prop
	self.setText 	= function() val.val = self.text end 
	self.text 		= (val.val=="--") and "" or ""..val.val
	self.options = {
		{ name="Cancel" , 	x=0 , y=0 , width=0 , height=0 , col={237,36,6} },
		{ name="Set", 		x=0 , y=0 , width=0 , height=0 , col={57,227,57} }
	}
end

function namer:typeVar(var,val,target,id,thing,vtype)
	editState 		= "Typing"
	self.open 		(target)
	self.mode 		= "typing"
	self.type 		= var
	self.setText 	= 	function() 
							if vtype == "Number" then
								target.vars.variables[id][thing] = tonumber(self.text)
							else
								target.vars.variables[id][thing] = tostring(self.text)
							end
						end 
	self.text 		= tostring(val)
	self.options = {
		{ name="Cancel" , 	x=0 , y=0 , width=0 , height=0 , col={237,36,6} },
		{ name="Set", 		x=0 , y=0 , width=0 , height=0 , col={57,227,57} }
	}
end

function namer:typeString(var,val,target,func,update,num)
	func  			= func or function(x) end
	editState 		= "Typing"
	self.open 		(target)
	self.mode 		= "typing"
	self.type 		= var or ""
	self.num      = num
	self.updateFunc = update
	self.setText 	= function(x) 
		--editor.tempTip 		("Text Set",2.5)
		val = tostring(self.text) 
		func(val) 
	end 
	self.text 		= val
	self.options = {
		{ name="Cancel" , 	x=0 , y=0 , width=0 , height=0 , col={237,36,6} },
		{ name="Set", 		x=0 , y=0 , width=0 , height=0 , col={57,227,57} }
	}
end

-----------------------------------------------------------
-- TRANSITION NAMER WINDOW IN AND OUT 
-----------------------------------------------------------
function namer:fade()
	if  self.target and not self.target.hidden then 
		self.backOpacity = easeTo( self.backOpacity, 	self.enabled and self.maxOpacity or 0, 6 )
			editor.drawTip(lume.merge(self.target,{text=self.text,w=self.target.width,a=self.backOpacity*1.5}))
	end
end

-----------------------------------------------------------
-- OPTIONS
-----------------------------------------------------------
function namer:optionsUpdate()
	unlockMouse() 
	lmouse.setVisible(true)  
	
	for i=1, #self.options do
		local op = self.options[i]
		op.hovered = mouseIn({x=op.x,y=op.y,width=op.width,height=op.height})
		if op.hovered then 
			lmouse.setCursor(hand)
			mouse.set(function() 
				namer.optionsClick(op.name)
			end)
		end 
	end
	lockMouse() 	
end

function namer.optionsClick(name)
	if name=="Delete" 	then 	loader:removeFile(namer.block) end 
	if name=="Edit" 	then 	effects.fade(function() game.enterGame(namer.key) end)	end
	if name=="Play"		then 	effects.fade(function() game.enterGame(namer.key) end)	developer=false; end
	if name=="Sure" 	then 	namer.warnFunc() end 
	if name=="Set" 		then 	if namer.text~="" then namer.setText() else return end end
	if name=="Nah" 		then 	namer:close() end 
	if name=="Cancel" 	then 	namer:close() end 
	namer:close()
end


-----------------------------------------------------------
-- CHECK THINGS
-----------------------------------------------------------
function namer:update()
	unlockKeys()
	self:fade()
	if not self.enabled then return 0 end 

	lmouse.setCursor(defualtCursor)
	
	namer:optionsUpdate()
	self.alertTimer=self.alertTimer-1
	(self.updateFunc or nothing)(self.text)
	if keypressed("escape") then 
		self:close()
		return
	end 
	if self.simple and not mouseIn(self) then 
		mouse.pushClick(function()
			self:close()
			return 
		end)
		mouse.pushClickR(function()
			self:close()
			return 
		end)
	end

	for k,key in pairs(keysDown) do 
		if lume.find(keysPressed,key) or (keyDown(key,10) and ltimer.fourstep==2) then 
			if #key==1 then 
				if (not self.num) or (tonumber(key) and tonumber(key)>=0 and tonumber(key)<10)  then 
					self.text = self.text..key
				end
			elseif key=="backspace" then 
				if cmdDown then 
					self.text 	= ""
				else
					self.text 	= string.sub(self.text, 0, #self.text-1); 
				end
			end
		end
	end

	if keypressed("return") then 
		if self.mode=="warning" then self.warnFunc() end
		if self.mode=="typing" then if self.text ~="" then self:setText() else return end end
		startButton = true
		self:close()
	end 
	if self.enabled then 
		lockKeys()
	end
end

function namer:nameTaken()
	namer.alertText 	= "Name Taken!"
	namer.alertTimer 	= namer.alertMax
end


