_G[gTable] = {};

local aTmpVal   = gTable;
cm.SetupTable(_G[aTmpVal]);


-- Called on Init
_G[gTable].OnInit = function(pObj)
   pObj.mState = 0;
end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)

end


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

   if pObj.mState == 0 then
      if cm.GetGlobalBoolIndirect(pObj, "DeActivatorFlag1") then
         pObj.mState = 1;
         cm.DisableObject(pObj);
         return;
      end
   end

end


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

   -- Check if the token is activated
   if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      IcPlaySample("PH_SFX_DOES_NOT_WORK");
      return true;
   end


	pObj.mDiscovered = true;
	if pName == "" then
	
      local aSpawnObj = pObj.mCsData:GetString("SpawnObject");
      if aSpawnObj == "" then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         return true;
      else

         if not cm.SpawnObject(pObj, 0, pObj.mSmPos, "SpawnObject") then
            return true;
         end

         local aFilledCount = cm.GetGlobalIntIndirect(pObj, "FilledCount") - 1;
         cm.SetGlobalValIndirect(pObj, "FilledCount", aFilledCount);

         if aSpawnObj == pObj.mCsData:GetString("Solution") then
            local aCount = cm.GetGlobalIntIndirect(pObj, "SolutionCounter1");
            assert(aCount > 0);
            aCount = aCount - pObj.mCsData:GetInt("SolutionWeight");
            cm.SetGlobalValIndirect(pObj, "SolutionCounter1", aCount);
         end

         pObj.mCsData:SetVal("SpawnObject", "");
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
			pObj.mCurCell = pObj.mCurCell - 1;
			pObj:SetActive(0, pPolyNum,      false);
			pObj:SetActive(0, pPolyNum - 1,  true);
			pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_OBJ_PICKUP");
			return true;

      end
		return true;

	elseif pName == pObj.mCsData:GetString("KeyObject1")
      or pName == pObj.mCsData:GetString("KeyObject2")
      or pName == pObj.mCsData:GetString("KeyObject3")
      or pName == pObj.mCsData:GetString("KeyObject4")
      or pName == pObj.mCsData:GetString("KeyObject5") then
      
         local aSpawnObj = pObj.mCsData:GetString("SpawnObject");
         if aSpawnObj ~= "" then
            cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
            return false;
         end


         pObj.mCsData:SetVal("SpawnObject", pName);
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
			pObj.mCurCell	 = pObj.mCurCell + 1;
			pObj:SetActive(0, pPolyNum,      false);
			pObj:SetActive(0, pPolyNum + 1,  true);
         IcPlaySample("PH_SFX_OBJ_USED");

         local aFilledCount = 1 + cm.GetGlobalIntIndirect(pObj, "FilledCount");
         cm.SetGlobalValIndirect(pObj, "FilledCount", aFilledCount);

         local aSolved = false;
         if pName == pObj.mCsData:GetString("Solution") then
            local aCount = cm.GetGlobalIntIndirect(pObj, "SolutionCounter1");
            aCount = aCount + pObj.mCsData:GetInt("SolutionWeight");
            cm.SetGlobalValIndirect(pObj, "SolutionCounter1", aCount);
            if aCount == 11111 then
               cm.SetGlobalValIndirect(pObj, "DeActivatorFlag1", true);
               cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
               aSolved = true;
            end
         end
         
         if not aSolved then
            if not cm.GetGlobalBoolIndirect(pObj, "HintShown") then
               cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
               cm.SetGlobalValIndirect(pObj, "HintShown", true);
            end

	         if (aFilledCount == 5) then
	            cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
	         end
         end

   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
         return true;

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

   return false;
end



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

	pG:PushState();

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

	if (pObj.mCurImg == 1) then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));

		local aBoxImg = IcImageWrap("PH_IMG_CMN_ZOOM_BOX");
		assert(aBoxImg);
		aRect = aBoxImg:GetCelRect(0);
		aRpos = pObj.mFrmPos;
		pG:DrawImage(aBoxImg:GetImage(), aRpos.x, aRpos.y, aRect);

		aImg = IcImageWrap(pObj.mBigImage);
		aRpos= pObj.mBigPos;
		assert(aImg);
		aRect = aImg:GetCelRect(pObj.mCurCell);
		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);
	end

   pG:PopState();

end

