----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
---------- old interface map -----------------------------------------------------
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
 

local global = {}

OldInterfaceMap = {}
OldInterfaceMap.width = 1280
OldInterfaceMap.height = 720

-- private :
OldInterfaceMap.SetEventFunction = function(name, sceneEventFunction, object)
    OldInterfaceMap[name] = sceneEventFunction
    OldInterfaceMap[name.."Object"] = object
end

OldInterfaceMap.InjectFunction = function(fName, a1, a2, a3, a4)
    if OldInterfaceMap[fName.."Object"] then
        OldInterfaceMap[fName.."Object"].temp = OldInterfaceMap[fName.."Object"][OldInterfaceMap[fName]]
        OldInterfaceMap[fName.."Object"]:temp(a1, a2, a3, a4)
    else    
        OldInterfaceMap[fName](a1, a2, a3, a4)
    end
end


-- public:
----  ASSESS METHODS -----

-- module
OldInterfaceMap.SetCuttentModule = function(module)
    OldInterfaceMap.sceneModule = module
end


-- events
OldInterfaceMap.SetSceneEventFunction = function(sceneEventFunction, object)
    OldInterfaceMap.SetEventFunction("sceneEventFunction", sceneEventFunction, object)
end

OldInterfaceMap.SetSceneUpdateFunction = function(sceneUpdateFunction, object)
    OldInterfaceMap.SetEventFunction("sceneUpdateFunction", sceneUpdateFunction, object)
end

OldInterfaceMap.InjectSceneEventFunction = function(elementName, itemName)
    OldInterfaceMap.InjectFunction("sceneEventFunction", elementName, itemName)
end

OldInterfaceMap.InjectSceneUpdateFunction = function()
    OldInterfaceMap.InjectFunction("sceneUpdateFunction")
end

-- indexed elements
OldInterfaceMap.indexedElements = {}

OldInterfaceMap.SetIndexedElements = function(indexedElements)
  OldInterfaceMap.indexedElements =  indexedElements
end
--------------------------

-- private:
-- states 
states = s

--sprite
function SpriteWrapper(sceneObject, name)
    sprite = {}
    sprite.sceneObject = sceneObject
    sprite.name = name
    
    function sprite:GetRowIndex()
        ret = self.sceneObject:GetZOrder()
        return ret
    end
    
    function sprite:SetRowIndex(index)
        self.sceneObject:SetZOrder(index)
    end    

    function sprite:SetPosition(x, y)
        if y == nil then
            y = x.y
            x = x.x
        end
        
        x = x/OldInterfaceMap.width
         y = y/OldInterfaceMap.height
        self.sceneObject:SetX(x)
        self.sceneObject:SetY(y)
    end  

    function sprite:GetBoundingRect()
        local x,y,w,h = self.sceneObject:GetX(), self.sceneObject:GetY(), self.sceneObject:GetWidth(), self.sceneObject:GetHeight()
        return {x=x*OldInterfaceMap.width,y=y*OldInterfaceMap.height},{x=(x+w)*OldInterfaceMap.width,y=(y+h)*OldInterfaceMap.height} -- min={x,y}, max={x,y}
    end  
    
    function sprite:GetPosition()
        return self.sceneObject:GetX() * OldInterfaceMap.width, self.sceneObject:GetY()*OldInterfaceMap.height
    end      
    
    function sprite:SetCurrentFrame(frameNumber)
       self.sceneObject:SetCurrentFrame(frameNumber-1)
    end   
    
    function sprite:GetCurrentFrame()
        return self.sceneObject:GetCurrentFrame()+1
    end  
    
    function sprite:SetAngle(degreeAngle)
        self.sceneObject:SetAngle(Ogre.Degree(degreeAngle))
    end       

    function sprite:GetAngle()
        return self.sceneObject:GetAngle():valueDegrees()
    end  
    
    function sprite:IsPointInside(x, y)
        return  self.sceneObject:IsInside(Ogre.Vector2(x/OldInterfaceMap.width, y/OldInterfaceMap.height))
    end  
    
    function sprite:GetActive()
        return true
    end 
    
    function sprite:GetName()
        return self.name
    end 
    
    function sprite:SetInteractive(value)
         self.sceneObject:SetInteractive(value)
    end 
    
    function sprite:Stop()
        --self.name
    end    
    
    return sprite
end

-- bkg.elements
bkg_elements = {}

-- public:
bkg = {}
bkg.elements = bkg_elements

bkg_elements_metatable = {

__index = function (table, key)

      if type(key) == "string" then 
          if OldInterfaceMap.sceneModule == nil or key == nil or OldInterfaceMap.sceneModule[key] == nil or OldInterfaceMap.sceneModule[key].self == nil then
            return nil
          end
          return SpriteWrapper(OldInterfaceMap.sceneModule[key].self, key)
      end
      -- 
      local all = OldInterfaceMap.indexedElements
      if type(key) == "number" then 
          for i=1, #all do
              local elemName = all[i]
              if bkg.elements[elemName]:GetRowIndex() == key then
                return bkg.elements[elemName]
              end
          end
      end
end
 
}


setmetatable(bkg_elements, bkg_elements_metatable)



bkg.SwapElements = function(index1, index2)

    local s1 = nil
    local s2 = nil
    local all = OldInterfaceMap.indexedElements
    for i=1,#all do
        if  bkg.elements[all[i]]:GetRowIndex() == index1 then
            s1 =  all[i]
        end
    end
    
    
    for i=1,#all do
        if  bkg.elements[all[i]]:GetRowIndex() == index2 then
            s2 =  all[i]
        end
    end
    
    bkg.elements[s1]:SetRowIndex(index2)
    bkg.elements[s2]:SetRowIndex(index1)
    

end




-- Inventory
global.Inventory = {}
function global.Inventory.Take(item)
end

function global.Inventory.Drop(item)
end

function global.Inventory.Release()
end

-- mouse
function global.GetMousePos()
    pos = Mouse.GetPosition()
    return pos.x * OldInterfaceMap.width, pos.y * OldInterfaceMap.height
end

function global.IsMouseButtonDown(number)
  return Mouse.IsMouseButtonDown(number)
end


--  sounds
function global.PlaySound2d(name)
end
function global.PlaySoundAsync(name)
end
--function global.PlaySound(name)
--end

--  time
function global.GetTime()
    return  Time:GetTime()/1000000
end

--  cursors
function global.SetElementCursor(elementName, cursorName)
end


--  others functions 

function global.FetchNextRandomElement(table)
	if #table == 0 then
        return nil 
    end

	local randomIndex = math.random(1, #table)
	local ret = table[randomIndex]

	table[randomIndex] = table[#table]
    table[#table] = nil

    return ret
end


function global.printmsg()
end

function global.PauseGame()
end

-- TO RENAME : Action -> OldAction
function global.OldAction(name, function_)
    action[name] = function_
    Action(name)
end

-- tasks

function global.StartNormalTask(totalTime, notmalFunction, notAnim, endFunction)
    totalTime = totalTime / 1000
    if notAnim then 
        notmalFunction(1) 
        if endFunction then
            endFunction()
        end 
        return
    end
   
    local startTime = GetTime()
    local normalizedTime = 0
    local tWhile_=
    tWhile(
    function () 
        local res = normalizedTime<1
        if not res and endFunction then
            endFunction()
        end
        return res
    end,
    function ()
        normalizedTime=(GetTime()-startTime)/totalTime
        if normalizedTime>1 then normalizedTime=1  end 
        notmalFunction(normalizedTime)
    end)

    local c=Cmd({tWhile_})
    c:SetNotSavable()
    c:SetHolded() 
    return c
end


--  utils


-- ex:  local var = (A,B)  -- var == B if A == nil otherwise A
global.Default = function(value,defValue,nilValueErrorFunction)
    if value ~= nil then return value end 
    if nilValueErrorFunction then nilValueErrorFunction(value,defValue) end 
    return defValue 
end


-- ex:   local var = _else_(A == B, A, B)  -- works like:  int var = A == B ? A : B  ,in C++ 
global._else_ = function(condition,valueA,valueB) if condition then return valueA else return valueB end  end


function global.DeepCopy(object)
    local lookup_table = {}
    local function _copy(object)
        if type(object) ~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        local new_table = {}
        lookup_table[object] = new_table
        for index, value in pairs(object) do
            new_table[_copy(index)] = _copy(value)
        end
        return setmetatable(new_table, getmetatable(object))
    end
    return _copy(object)
end

-- savable table Wrapper


function SavableTableWrapper(object, name)
    savableTable = {}
    savableTable.object = object
    savableTable.name = name
    
    mt = {
        __index = function (table, key)
            return object:Get(key)  
        end,
        
        __newindex = function(op,key, value)
            object:Set(key, value)
        end
    }
    
     
    setmetatable(savableTable, mt)
    return savableTable
end

function global.SavableTableExists(name)
   return ExistsSavableArray(name)
end
--[[

    st = OldSavableTable("stt")
    print(st)
    st["ula"] = 555
    print(st["ula"])
    
     st = OldSavableTable("stt")
      print(st["ula"])
    
      print(SavableTableExists("stt"))

]]
function OldSavableTable(name)
   return SavableTableWrapper(SavableTable(name), name) 
end


function global.Vector(x, y)
    return {x=x,y=y}
end

-- private:
for k, v in pairs(global) do
    if _G[k] == nil then
         _G[k] = v 
    else
       print("old_interface_map elements exists: ",k); 
    end
end


