
_G[gTable] = {};

local aTmpVal = gTable;
local aUsed   = false;
local aState  = 0;
cm.SetupTable(_G[aTmpVal]);


-- Called on Init
_G[gTable].OnInit = function(pObj)

   aUsed            = false;
	pObj.mState      = 0;
   aState           = pObj.mState;
	pObj.mDiscovered = true;
	pObj.mCsData:SetVal("Used", aUsed);
	pObj.mAlpha:AncSetupChange(0, 255.0, PhMaterializeTime, IcAutoNumChgr.IC_ANCM_LINEAR);
	_G[aTmpVal].MoveToSlot(pObj, PhKeyToSlotTime, true);

end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
   aUsed    = pObj.mCsData:GetBool("Used");
   aState   = pObj.mState;
end


-- Called on use
_G[gTable].OnUse = function(pObj)


   if aState == 0 then
      cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
      IcPlaySample("PH_SFX_DOES_NOT_WORK");
      return;

   elseif aState == 1 then
   	aState         = aState + 1;
   	pObj.mState    = aState;
      pObj.mCurCell  = pObj.mCurCell + 1;
     	cm.SetGlobalValIndirect(pObj, "SetFlag2", false);
      cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
      cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
   	IcPlaySample("PH_SFX_OBJ_USED");
      return;

   elseif aState == 2 then
      local aCount      = pObj.mCsData:GetInt("UseCount");
      local aCycleCount = pObj.mCsData:GetInt("UseCycle");
      local aMaxCount   = pObj.mCsData:GetInt("MaxUseCount");
      aCount = aCount + 1;

      if aCount < aCycleCount then
         pObj.mCsData:SetVal("UseCount", aCount);
        	pObj.mPdata:UnMouseObject();
      	IcPlaySample("PH_SFX_POUR");
      	return;
      else

         aMaxCount = aMaxCount - 1;
         if aMaxCount > 0 then
         	aState        = 0;
         	pObj.mState   = aState;
         	pObj.mCurCell = 0;
            aCount        = 0;

            pObj.mCsData:SetVal("UseCount", aCount);
            pObj.mCsData:SetVal("MaxUseCount", aMaxCount);
         	cm.SetGlobalValIndirect(pObj, "SetFlag1", false);
         	pObj.mPdata:UnMouseObject();
         	IcPlaySample("PH_SFX_POUR");
            return;
         end

      end

   end

	aState      = aState + 1;
	pObj.mState = aState;
   aUsed = true;
	pObj.mPdata:UnMouseObject();
	pObj.mCsData:SetVal("Used", aUsed);
	pObj.mPdata.mBar:RemoveSlot(pObj.mId);
	pObj.mAlpha:AncSetupChange(255.0, 0.0, PhKeyVanishAfterUse, IcAutoNumChgr.IC_ANCM_LINEAR);
	IcPlaySample("PH_SFX_POUR");

end


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

   cm.MoveToSlotUpdate(pObj);

	if aUsed then
		if not pObj.mAlpha:AncIsChanging() then
			pObj.mIsDone = true;
		end
	end

end


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

	if pName == "" then

		pObj.mPdata:MouseTheObject(pObj);
		IcPlaySample("PH_SFX_OBJ_PICKUP");
		return true;

	elseif pName == pObj.mCsData:GetString("KeyObject1") then
	
      if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return false;
      end

      aState         = aState + 1;
      pObj.mState    = aState;
      pObj.mCurCell  = pObj.mCurCell + 1;
      pObj:StopParticleEmission(false);

   	_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   	cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
		return true;

	elseif pName == pObj.mCsData:GetString("KeyObject2") then

      if aState == 0 then

         pObj.mCsData:SetVal("UseCycle", 1);
         pObj.mCsData:SetVal("UseCount", 0);
         aState         = aState + 1;
         pObj.mState    = aState;
         pObj.mCurCell  = pObj.mCurCell + 1;
         pObj:StopParticleEmission(false);

      	_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
      	cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
		   return true;
   	else
         cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return false;
      end
		return true;

	elseif pName == pObj.mCsData:GetString("KeyObject3") then

      if aState == 0 then

         pObj.mCsData:SetVal("UseCycle", 1);
         pObj.mCsData:SetVal("UseCount", 0);
         aState         = aState + 1;
         pObj.mState    = aState;
         pObj.mCurCell  = pObj.mCurCell + 1;
         pObj:StopParticleEmission(false);

      	_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
      	cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
		   return true;
   	else
         cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return false;
      end
		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("Used") or pObj.mCsData:GetBool("MovingToSlot") then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
	end
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);


end


-- Called on moused
_G[gTable].OnMoused =  function(pObj)

end

-- Called on Unmoused
_G[gTable].OnUnMoused =  function(pObj)
	if not pObj.mCsData:GetBool("Used") then
		_G[aTmpVal].MoveToSlot(pObj, PhKeyToSlotTime, false);
	end
end


-- Moves the key to a slot
_G[gTable].MoveToSlot = cm.MoveToSlot;

