_G[gTable] = {};

local aTmpVal  = gTable;

-- Particle system colors
local aCols    = {};
aCols[0] = { Color(77, 80,  5, 20), Color(239, 243, 58, 200), Color(87, 89, 11, 20) } -- Yellow
aCols[1] = { Color( 2, 83,  9, 20), Color(60,  242, 64, 255), Color(19, 94,  6, 20) } -- Green
aCols[2] = { Color(79,  0,  0, 20), Color(230,   0,  0, 240), Color(79,  0,  0, 20) } -- Red
aCols[3] = { Color(13,  7, 71, 20), Color(44,   69,245, 255), Color(6,   3, 75, 20) } -- Blue
aCols[4] = { Color(98,  4, 96, 20), Color(243,  56,243, 255), Color(95,  5, 92, 20) } -- Violet
cm.SetupTable(_G[aTmpVal]);


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState       = 0;
   pObj.mDiscovered  = true;
   local aFlameIndex = pObj.mCsData:GetInt("FlameIndex");
   _G[aTmpVal].SetupParticleSystem(pObj, aFlameIndex);
end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
   local aFlameIndex = pObj.mCsData:GetInt("FlameIndex");
   _G[aTmpVal].SetupParticleSystem(pObj, aFlameIndex);
end


-- Called on a update
_G[gTable].OnUpdate = function(pObj)

   if pObj.mState == 0 and cm.GetGlobalBoolIndirect(pObj, "SetFlag1") then
      pObj.mState = pObj.mState + 1;
   end


end


-- Called on a succesful click
_G[gTable].OnClick =  function(pObj, pName, pActorObj, pPolyNum, pX, pY)

   if pObj.mState == 1 then
   
      if pName ~= "" then
         pObj.mPdata:UnMouseObject();
		end
      cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
      return true;
   end

	pObj.mDiscovered = true;
	if pName == "" then
      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      return true;

	elseif pName == pObj.mCsData:GetString("KeyObject1") then
	
	   if not cm.GetGlobalBoolIndirect(pObj, "KeyObjectActive1") then
	      pObj.mPdata:UnMouseObject();
	      cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
	      return true;
	   end

      local aFlameIndex = pActorObj.mCsData:GetInt("FlameIndex");
      local aOldIndex   = pObj.mCsData:GetInt("FlameIndex");
      _G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);

		pObj.mCsData:SetVal("FlameIndex", aFlameIndex);
		pObj:SetEmitterColors(aCols[aFlameIndex][1], aCols[aFlameIndex][2], aCols[aFlameIndex][3]);
		
		local aSolIndex = pObj.mCsData:GetInt("SolutionIndex");
		if aFlameIndex == aSolIndex then
         local aAllSols = cm.GetGlobalIntIndirect(pObj, "BrazierCounter");
         aAllSols = aAllSols + pObj.mCsData:GetInt("SolutionWeight");
         cm.SetGlobalValIndirect(pObj, "BrazierCounter", aAllSols);
         
         if aAllSols == 11111 then
            cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
         end

      elseif aOldIndex == aSolIndex then
         local aAllSols = cm.GetGlobalIntIndirect(pObj, "BrazierCounter");
         aAllSols = aAllSols - pObj.mCsData:GetInt("SolutionWeight");
         cm.SetGlobalValIndirect(pObj, "BrazierCounter", aAllSols);
      end

		return true;

	else
		cm.ObjDoesNotWork(pObj);
		return false;
	end

	return false;
end



-- Called on draw
_G[gTable].OnDraw =  function(pObj, pG)

	local aRpos = pObj.mSmPos;
	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	local aRect = aImg:GetCelRect(pObj.mCurCell);
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end



-- Called on draw
_G[gTable].SetupParticleSystem =  function(pObj, pIndex)

	local aX	 = pObj.mCsData:GetInt("SpawnX0");
	local aY	 = pObj.mCsData:GetInt("SpawnY0");

   local aStr  = "PH_PSYS_S03_L03_BRAZIER_00";
   local aPos1 = IcVector2d(0,  0)  + pObj.mSmPos;
   local aPos2 = IcVector2d(aX, aY) + pObj.mSmPos;
   pObj.mSmPos = aPos2;
   pObj:SetupParticleSystem(aStr);
   pObj:SetEmitterColors(aCols[pIndex][1], aCols[pIndex][2], aCols[pIndex][3]);
   pObj:StartParticleEmission(0);
   pObj.mSmPos = aPos1;

end
