/* This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2.universe.gameserver;

import java.util.logging.Level;
import java.util.logging.Logger;

import l2.universe.Config;
import l2.universe.gameserver.instancemanager.CastleManager;
import l2.universe.gameserver.model.L2Clan;
import l2.universe.gameserver.model.entity.Fort;

/**
 *
 * Vice - 2008
 * Class managing periodical events with castle
 *
 */
public class FortUpdater implements Runnable
{
	protected static Logger _log = Logger.getLogger(FortUpdater.class.getName());
	private L2Clan _clan;
	private Fort _fort;
	private int _runCount;
	private UpdaterType _updaterType;
	
	public enum UpdaterType
	{
		MAX_OWN_TIME, // gives fort back to NPC clan
		PERIODIC_UPDATE // raise blood oath/supply level
	}
	
	public FortUpdater(Fort fort, L2Clan clan, int runCount, UpdaterType ut)
	{
		_fort = fort;
		_clan = clan;
		set_runCount(runCount);
		_updaterType = ut;
	}
	
	public void run()
	{
		try
		{
			switch(_updaterType)
			{
				case PERIODIC_UPDATE:
					set_runCount(get_runCount() + 1);
					if (_fort.getOwnerClan() == null || _fort.getOwnerClan() != _clan)
						return;
					
					_fort.setBloodOathReward(_fort.getBloodOathReward() + Config.FS_BLOOD_OATH_COUNT);
					if (_fort.getFortState() == 2)
					{
						if (_clan.getWarehouse().getAdena() >= Config.FS_FEE_FOR_CASTLE)
						{
							_clan.getWarehouse().destroyItemByItemId("FS_fee_for_Castle", 57, Config.FS_FEE_FOR_CASTLE, null, null);
							CastleManager.getInstance().getCastleById(_fort.getCastleId()).addToTreasuryNoTax(Config.FS_FEE_FOR_CASTLE);
							_fort.raiseSupplyLvL();
						}
						else
							_fort.setFortState(1, 0);
					}
					_fort.saveFortVariables();
					break;
				case MAX_OWN_TIME:
					if (_fort.getOwnerClan() == null || _fort.getOwnerClan() != _clan)
						return;
					if (_fort.getOwnedTime() > Config.FS_MAX_OWN_TIME * 3600)
					{
						_fort.removeOwner(true);
						_fort.setFortState(0, 0);
					}
					break;
			}
		}
		catch (Exception e)
		{
			_log.log(Level.WARNING, "", e);
		}
	}

	public int get_runCount() {
		return _runCount;
	}

	public void set_runCount(int _runCount) {
		this._runCount = _runCount;
	}
}