--[[
* The MIT License
* Copyright (C) 2014 Bacioiu "Zapa" Ciprian (bacioiu.ciprian@gmail.com).  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom th be Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]

function interface:initMM( )
	
	self._tweenButtons = true
	buttonTextStyle = {
			font = "arial-rounded.TTF",
			size = 17,
			color = {0.1, 0.1, 0.9, 1},			
	}

	--interface:createMainPanel( )
	self:_createMainMenu( )

	local isSoundPlaying = sound:getCategoryPlaying(SOUND_MAIN_MENU)

	if isSoundPlaying == false then
		sound:playFromCategory(SOUND_MAIN_MENU)
	end

	self._bgColor =  MOAIColor.new ()
	self._startTimer = Game.worldTimer-3
	self._bgColor:setColor ( 0, 0, 0, 1 )

	self._bgColorTable = { }
	self._bgColorTable.r = 0
	self._bgColorTable.g = 0
	self._bgColorTable.b = 0
	--[[color:seekColor ( 0, 1, 0, 1, 1.5 )--]]
end

function interface:_setOptions( state )
	self._showOptions = state
	if state == false then
		print("IT IS false")
		Game:saveOptionsState( )
	end
end

function interface:_getOptions( )
	return self._showOptions
end

function interface:_createMainMenu( )
	local roots, widgets, groups = element.gui:loadLayout(resources.getPath("mainMenu.lua"), "")

	if (nil ~= widgets.background) then
	  self._mmBgPanel = widgets.background.window
	  print("BG PANEL REGISTERED")
	else
		print("ONE FAIL")
	end

	if (nil ~= widgets.btnBattle) then
	  freeBattleButton = widgets.btnBattle.window
	  print("BbtnBattle REGISTERED")
	else
		print("ONE FAIL")
	end

	if (nil ~= widgets.btnEditor) then
	  lvEditorButton = widgets.btnEditor.window
	  print("BbtnBattle REGISTERED")
	else
		print("ONE FAIL")
	end

	if (nil ~= widgets.btnOptions) then
	  optionsButton = widgets.btnOptions.window
	  print("BbtnBattle REGISTERED")
	else
		print("ONE FAIL")
	end

	if (nil ~= widgets.btnQuit) then
	  quitButton = widgets.btnQuit.window
	  print("btnQuit REGISTERED")
	else
		print("ONE FAIL")
	end

	if (nil ~= widgets.bgBar) then
		mmBgBar = widgets.bgBar.window
	else
		print("BG BAR FAIL")
	end

	if (nil ~= widgets.volSlider) then
		self._volSlider = widgets.volSlider.window
	else
		print("VOL SLIDER FAIL")
	end

	if (nil ~= widgets.btnConOn) then
		fullScreenOnBttn = widgets.btnConOn.window
	else
		print("no fullscreens sennor")
	end

	--btnConfOff

	if (nil ~= widgets.btnConfOff) then
		fullScreenOFfBttn = widgets.btnConfOff.window
	else
		print("no fullscreens sennor")
	end

	if (nil ~= widgets.btnZoomOn) then
		ZoomOnBttn = widgets.btnZoomOn.window
		ZoomOnBttn:registerEventHandler(ZoomOnBttn.EVENT_BUTTON_CLICK, nil, _handleZoomOnButtonPressed)
		
	else
		print("Zoom buttons activated sennor")
	end

	--btnConfOff

	if (nil ~= widgets.btnZoomOff) then
		ZoomOFfBttn = widgets.btnZoomOff.window
		ZoomOFfBttn:registerEventHandler(ZoomOFfBttn.EVENT_BUTTON_CLICK, nil, _handleZoomOffButtonPressed)
	else
		print("no zoom button sennor")
	end


	self._volSlider:setRange(1, 100)
	
	

	local tb, bool = Game:loadOptionsState( )
	if bool == true then
		self._volSlider:setCurrValue(tb.soundVolume*100)
		Game.masterVolume = tb.soundVolume
		Game.optionControls.soundVolume = tb.soundVolume
		Game.optionControls.uiZoomToggle = tb.uiZoomToggle
	else
		self._volSlider:setCurrValue( Game.masterVolume*100 )
		Game.optionControls.uiZoomToggle = false

	end
	sound:setGeneralVolume(Game.masterVolume)
	self._volSlider:registerEventHandler(self._volSlider.EVENT_SLIDER_VALUE_CHANGED, nil, _VolumeSliderValueChanged, self._volSlider)

	freeBattleButton:registerEventHandler(freeBattleButton.EVENT_BUTTON_CLICK, nil, _handleFreeBattleButtonPressed)
	lvEditorButton:registerEventHandler(lvEditorButton.EVENT_BUTTON_CLICK, nil, _handleLevelEditorPressed )
	optionsButton:registerEventHandler(optionsButton.EVENT_BUTTON_CLICK, nil, _handleOptionsPressed )
	quitButton:registerEventHandler(quitButton.EVENT_BUTTON_CLICK, nil, _handleQuitPressed )
	fullScreenOFfBttn:registerEventHandler(fullScreenOFfBttn.EVENT_BUTTON_CLICK, nil, _handleFullScrOffPressed )

	fullScreenOnBttn:registerEventHandler(fullScreenOnBttn.EVENT_BUTTON_CLICK, nil, _handleFullScrPressed )
end

function interface:createMainPanel( )
	mmPanel = element.gui:createImage()
	mmPanel:setDim(100, 100)
	mmPanel:setPos(0,0 )
	mmPanel:setImage(element.resources.getPath("main_menu/main_menu.png"))
	interface:createButtons( )
end

function interface:createButtons( )
	campaignButton = element.gui:createButton( )
	campaignButton:setDim(30, 18)
	campaignButton:setPos(2, 70)
	campaignButton:setText("CAMPAIGN")
	campaignButton:setNormalImage(element.resources.getPath("main_menu/button_temp.png") )
	campaignButton:setHoverImage(element.resources.getPath("main_menu/button_temp_hover.png") )
	campaignButton:setPushedImage(element.resources.getPath("main_menu/button_temp_hover.png") )
	--button_temp
	--button_temp_hover
	campaignButton:registerEventHandler(campaignButton.EVENT_BUTTON_CLICK, nil, _handleCampaignButtonPressed)

	freeBattleButton = element.gui:createButton( )
	freeBattleButton:setDim(30, 18)
	freeBattleButton:setPos(35, 70)
	freeBattleButton:setText("FREE BATTLE")
	freeBattleButton:setNormalImage(element.resources.getPath("main_menu/button_temp.png") )
	freeBattleButton:setHoverImage(element.resources.getPath("main_menu/button_temp_hover.png") )
	freeBattleButton:setPushedImage(element.resources.getPath("main_menu/button_temp_hover.png") )
	
	freeBattleButton:registerEventHandler(freeBattleButton.EVENT_BUTTON_CLICK, nil, _handleFreeBattleButtonPressed)

	lvEditorButton = element.gui:createButton( )
	lvEditorButton:setDim(30, 18)
	lvEditorButton:setPos(34+4+30, 70)
	lvEditorButton:setText("LEVEL EDITOR")
	lvEditorButton:registerEventHandler(lvEditorButton.EVENT_BUTTON_CLICK, nil, _handleLevelEditorPressed )
	lvEditorButton:setNormalImage(element.resources.getPath("main_menu/button_temp.png") )
	lvEditorButton:setHoverImage(element.resources.getPath("main_menu/button_temp_hover.png") )
	lvEditorButton:setPushedImage(element.resources.getPath("main_menu/button_temp_hover.png") )

	
	mmPanel:addChild(lvEditorButton)
	mmPanel:addChild(freeBattleButton)
	mmPanel:addChild(campaignButton)
end

local function SmudgeColor(colorA, colorB, percent)
  local r, g, b = colorA.r-(colorA.r-colorB.r)*percent, colorA.g-(colorA.g-colorB.g)*percent, colorA.b-(colorA.b-colorB.b)*percent
  return {r=r,g=g,b=b}
end

function fadeColor( time, prologue, attack, sustain, decay, epilogue, fade_in_r, fade_in_g, fade_in_b, fade_out_r, fade_out_g, fade_out_b )
   -- [0, prologue)
   if time < prologue then
      return fade_in_r, fade_in_g, fade_in_b, 1
   end
   
   -- (prologue, prologue + attack]
   time = time - prologue
   if time < attack then
      return fade_in_r, fade_in_g, fade_in_b, ( math.cos( ( time ) / attack * math.pi ) + 1 ) / 2 * 1
   end
   
   -- (prologue + attack, prologue + attack + sustain]
   time = time - attack
   if time < sustain then
      return fade_in_r, fade_in_g, fade_in_b, 0
   end
   
   -- (prologue + attack + sustain, prologue + attack + sustain + decay]
   time = time - sustain
   if time < decay then
      return fade_out_r, fade_out_g, fade_out_b, 1 - ( ( math.cos( ( time ) / decay * math.pi ) + 1 ) / 2 * 1 )
   end
   
   -- (prologue + attack + sustain + decay, prologue + attack + sustain + decay + epilogue]
   time = time - decay
   if time < epilogue then
      return fade_out_r, fade_out_g, fade_out_b, 1
   end
   
   -- End of fading, return all nils.
end


function interface:_updateMMButtons( )


	
	if Game.worldTimer < self._startTimer + 4 then
		self._bgColor:seekColor(self._bgColorTable.r , self._bgColorTable.g, self._bgColorTable.b, 1, 3.8)
		print("Random Color")
	else
		self._bgColorTable.r = math.random(0, 1), math.random(0, 1), math.random(0, 1)
		self._bgColorTable.g = math.random(0, 1), math.random(0, 1), math.random(0, 1)
		self._bgColorTable.b = math.random(0, 1), math.random(0, 1), math.random(0, 1)
		self._startTimer = Game.worldTimer
		print("Start Time = Game.worldTimer")
	end
		


	MOAIGfxDevice.setClearColor ( self._bgColor )


	
	if self._showOptions == true then
		mmBgBar:tweenPos(-32, 70)
	else
		mmBgBar:tweenPos(0, 70)
	end
end