
-- Code for the talking heads in Stage 2, Level 2

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


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
   cm.SetGlobalValIndirect(pObj, "PuzzleFlag", 0);
end

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

   if pObj.mState == 2 then
      local aPuzzleStatus = cm.GetGlobalIntIndirect(pObj, "PuzzleFlag");
      if aPuzzleStatus == 0 then
			pObj.mState    = 0;
			pObj.mCurCell  = 0;
      end
	end


end

-- Called after a level is exited
_G[gTable].OnLevelExit = function(pObj)

   if pObj.mState == 2 then
      local aPuzzleStatus = cm.GetGlobalIntIndirect(pObj, "PuzzleFlag");
      if aPuzzleStatus == 0 then
			pObj.mState    = 0;
			pObj.mCurCell  = 0;
      end
	end

end


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

   if not pObj.mCsData:GetBool("IsActivated") then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mCsData:SetVal("IsActivated", true);
      end
      return;
   end

	if pObj.mState == 1 then

	   if not pObj.mAlpha:AncIsChanging() then
	      pObj.mState = pObj.mState + 1;

	      local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag");
	      local aPuzzIndex  = pObj.mCsData:GetInt("PuzzleNum");
	      pObj.mPdata.mCsData:SetVal(aPuzzleFlag, 0);
			AddRiddleWidget(pObj.mPdata, aPuzzleFlag, pObj.mSmallImage, aPuzzIndex);

	      IcPlaySample("PH_SFX_CLOSER_LOOK");
	   end

	elseif pObj.mState == 2 then

      local aPuzzleStatus = cm.GetGlobalIntIndirect(pObj, "PuzzleFlag");

      -- Riddle successfully solved
      if aPuzzleStatus == 1 then

         -- Update puzzle counters
         local aCounter = 0;
         local aCounterFlag = pObj.mCsData:GetString("CounterFlag");
         if pObj.mPdata.mCsData:IfExistsInt(aCounterFlag) then
            aCounter = pObj.mPdata.mCsData:GetInt(aCounterFlag);
         end
         aCounter = aCounter + 1;
         pObj.mPdata.mCsData:SetVal(aCounterFlag, aCounter);
         
         if pObj.mCsData:GetString("PuzzleFlag") == pObj.mCsData:GetString("PuzzleFlag3") then
            pObj.mState = pObj.mState + 1;
            cm.DisableObject(pObj);
            
            if aCounter == 10 then
               cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
               IcPlaySample("PH_SFX_GOOD_JOB");
            end
         else
            pObj.mState = 0;
         end

	      pObj:StartFadeRects(PhFadeAnotherTimeLong);
		   pObj.mCurCell  = 0;

      -- Riddle not solved
      elseif aPuzzleStatus == -1 then
	      pObj:StartFadeRects(PhFadeAnotherTimeLong);
         pObj.mState    = 0;
		   pObj.mCurCell  = 0;

	      if pObj.mCsData:GetString("PuzzleFlag") == pObj.mCsData:GetString("PuzzleFlag0") then
	         cm.SetGlobalValIndirect(pObj, "PuzzleFlag0", 1);
	         local aCounter = 0;
	         local aCounterFlag = pObj.mCsData:GetString("CounterFlag");
	         if pObj.mPdata.mCsData:IfExistsInt(aCounterFlag) then
	            aCounter = pObj.mPdata.mCsData:GetInt(aCounterFlag);
	         end
	         aCounter = aCounter + 1;
	         pObj.mPdata.mCsData:SetVal(aCounterFlag, aCounter);
	      end

		end
	end

end


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

   -- If not active, show a message & exit
	if not pObj.mCsData:GetBool("IsActivated") then

      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      IcPlaySample("PH_SFX_DOES_NOT_WORK");
   	if pName == "" then
         return true;
      end
   	return false;

	end

	pObj.mDiscovered = true;
	if pName == "" then

		if pObj.mState == 0 then

	      local aFlag = "";
	      for aI = 0, 3 do
	         aFlag       = string.format("PuzzleFlag%d", aI);
	         local aNum  = string.format("PuzzleNum%d",  aI);
	         if cm.GetGlobalIntIndirect(pObj, aFlag) ~= 1 then
	            pObj.mCsData:SetVal("PuzzleFlag", pObj.mCsData:GetString(aFlag));
	            pObj.mCsData:SetVal("PuzzleNum", pObj.mCsData:GetInt(aNum));
	            break;
	         end
	      end
	      
	      pObj:StartFadeRects(PhFadeAnotherTimeLong);
	      pObj.mState     = pObj.mState + 1;
			pObj.mCurCell   = pObj.mCurCell + 1;
			IcPlaySample("PH_SFX_OBJ_USED");
			return true;

		end
		
		IcPlaySample("PH_SFX_DOES_NOT_WORK");
		return true;

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

	return false;

end


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

	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	local aRpos = pObj.mSmPos;
	local aRect = aImg:GetCelRect(pObj.mCurCell);

   if pObj.mCsData:GetBool("Changing") then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
		local aRectNum = pObj.mCsData:GetInt("OldRectCell");
		local aRect2   = aImg:GetCelRect(aRectNum);
		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect2);
		pG:SetColor(Color(255, 255, 255, 255 - pObj.mAlpha:AncGetVal()));
	end

	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end



