/*
 * REFERENCE / TEMPLATE ONLY - this is not a maintained integration and is
 * not compiled as part of alnino-bridge (hence the .txt extension).
 *
 * Delivers pending rows from alnino_donation_rewards to players. Copy this
 * into your gameserver's source tree (adjust the package below, and the
 * delivery mechanics - mail vs direct inventory, how you look up an online
 * player, how your pack schedules a repeating task) and wire it up the same
 * way your pack starts its other task managers. Written for aCis-based
 * Interlude servers.
 */
package net.sf.l2j.gameserver.taskmanager; // adjust to your pack's package layout

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

// adjust these imports to your pack's connection pool / player lookup APIs
import net.sf.l2j.commons.pool.ConnectionPool;
import net.sf.l2j.commons.pool.ThreadPool;
import net.sf.l2j.gameserver.model.World;
import net.sf.l2j.gameserver.model.actor.Player;

public class AlninoRewardTaskManager implements Runnable
{
	// Table name must match `database.table` in alnino-bridge's config.yml.
	private static final String SELECT = "SELECT id, item_id, item_count, char_name, reason FROM alnino_donation_rewards WHERE status=0;";
	private static final String MARK_DELIVERED = "UPDATE alnino_donation_rewards SET status=1, delivered_at=NOW() WHERE id=?;";

	protected AlninoRewardTaskManager()
	{
		ThreadPool.scheduleAtFixedRate(this, 5000, 5000);
	}

	@Override
	public void run()
	{
		try (Connection con = ConnectionPool.getConnection();
			PreparedStatement statement = con.prepareStatement(SELECT))
		{
			try (ResultSet rset = statement.executeQuery())
			{
				while (rset.next())
				{
					final int id = rset.getInt("id");
					final int itemId = rset.getInt("item_id");
					final int itemCount = rset.getInt("item_count");
					final String charName = rset.getString("char_name");
					final String reason = rset.getString("reason");

					if (itemId < 1 || itemCount < 1 || charName == null || charName.isEmpty())
					{
						markDelivered(id);
						continue;
					}

					final Player player = World.getInstance().getPlayer(charName);

					// Player offline: leave status=0 so this row is retried
					// next tick, instead of silently dropping the reward.
					if (player == null || !player.isOnline())
						continue;

					player.sendMessage(reason != null && !reason.isBlank() ? reason : "Donation received, thank you!");
					player.addItem(itemId, itemCount, true);
					markDelivered(id);
				}
			}
		}
		catch (Exception e)
		{
			// log via your pack's logger
		}
	}

	private static void markDelivered(int id)
	{
		try (Connection con = ConnectionPool.getConnection();
			PreparedStatement statement = con.prepareStatement(MARK_DELIVERED))
		{
			statement.setInt(1, id);
			statement.execute();
		}
		catch (SQLException e)
		{
			// log via your pack's logger
		}
	}

	public static AlninoRewardTaskManager getInstance()
	{
		return SingletonHolder.INSTANCE;
	}

	private static class SingletonHolder
	{
		protected static final AlninoRewardTaskManager INSTANCE = new AlninoRewardTaskManager();
	}
}
