/*
 * Copyright © 2004-2021 L2J Server
 * 
 * This file is part of L2J Server.
 * 
 * L2J Server 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.
 * 
 * L2J Server 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 com.l2jserver.gameserver.model.conditions;

import com.l2jserver.gameserver.instancemanager.CastleManager;
import com.l2jserver.gameserver.instancemanager.FortManager;
import com.l2jserver.gameserver.instancemanager.TerritoryWarManager;
import com.l2jserver.gameserver.model.actor.L2Character;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.entity.Castle;
import com.l2jserver.gameserver.model.entity.Fort;
import com.l2jserver.gameserver.model.items.L2Item;
import com.l2jserver.gameserver.model.skills.Skill;

/**
 * Siege zone condition.
 * @author Gigiikun
 */
public final class ConditionSiegeZone extends Condition {
	// conditional values
	public static final int COND_NOT_ZONE = 0x0001;
	public static final int COND_CAST_ATTACK = 0x0002;
	public static final int COND_CAST_DEFEND = 0x0004;
	public static final int COND_CAST_NEUTRAL = 0x0008;
	public static final int COND_FORT_ATTACK = 0x0010;
	public static final int COND_FORT_DEFEND = 0x0020;
	public static final int COND_FORT_NEUTRAL = 0x0040;
	public static final int COND_TW_CHANNEL = 0x0080;
	public static final int COND_TW_PROGRESS = 0x0100;
	
	private final int _value;
	private final boolean _self;
	
	public ConditionSiegeZone(int value, boolean self) {
		_value = value;
		_self = self;
	}
	
	@Override
	public boolean testImpl(L2Character effector, L2Character effected, Skill skill, L2Item item) {
		L2Character target = _self ? effector : effected;
		Castle castle = CastleManager.getInstance().getCastle(target);
		Fort fort = FortManager.getInstance().getFort(target);
		
		if (((_value & COND_TW_PROGRESS) != 0) && !TerritoryWarManager.getInstance().isTWInProgress()) {
			return false;
		} else if (((_value & COND_TW_CHANNEL) != 0) && !TerritoryWarManager.getInstance().isTWChannelOpen()) {
			return false;
		} else if ((castle == null) && (fort == null)) {
			return (_value & COND_NOT_ZONE) != 0;
		}
		if (castle != null) {
			return checkIfOk(target, castle, _value);
		}
		return checkIfOk(target, fort, _value);
	}
	
	/**
	 * Check if ok.
	 * @param activeChar the active char
	 * @param castle the castle
	 * @param value the value
	 * @return true, if successful
	 */
	public static boolean checkIfOk(L2Character activeChar, Castle castle, int value) {
		if (!(activeChar instanceof L2PcInstance)) {
			return false;
		}
		
		L2PcInstance player = (L2PcInstance) activeChar;
		
		if (((castle == null) || (castle.getResidenceId() <= 0))) {
			return (value & COND_NOT_ZONE) != 0;
		} else if (!castle.getZone().isActive()) {
			return (value & COND_NOT_ZONE) != 0;
		} else if (((value & COND_CAST_ATTACK) != 0) && player.isRegisteredOnThisSiegeField(castle.getResidenceId()) && (player.getSiegeState() == 1)) {
			return true;
		} else if (((value & COND_CAST_DEFEND) != 0) && player.isRegisteredOnThisSiegeField(castle.getResidenceId()) && (player.getSiegeState() == 2)) {
			return true;
		} else {
			return ((value & COND_CAST_NEUTRAL) != 0) && (player.getSiegeState() == 0);
		}
	}
	
	/**
	 * Check if ok.
	 * @param activeChar the active char
	 * @param fort the fort
	 * @param value the value
	 * @return true, if successful
	 */
	public static boolean checkIfOk(L2Character activeChar, Fort fort, int value) {
		if (!(activeChar instanceof L2PcInstance)) {
			return false;
		}
		
		L2PcInstance player = (L2PcInstance) activeChar;
		
		if (((fort == null) || (fort.getResidenceId() <= 0))) {
			return (value & COND_NOT_ZONE) != 0;
		} else if (!fort.getZone().isActive()) {
			return (value & COND_NOT_ZONE) != 0;
		} else if (((value & COND_FORT_ATTACK) != 0) && player.isRegisteredOnThisSiegeField(fort.getResidenceId()) && (player.getSiegeState() == 1)) {
			return true;
		} else if (((value & COND_FORT_DEFEND) != 0) && player.isRegisteredOnThisSiegeField(fort.getResidenceId()) && (player.getSiegeState() == 2)) {
			return true;
		} else {
			return ((value & COND_FORT_NEUTRAL) != 0) && (player.getSiegeState() == 0);
		}
	}
	
}
