/*
 * 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.network.serverpackets;

import java.util.ArrayList;
import java.util.List;

import com.l2jserver.gameserver.instancemanager.CastleManorManager;
import com.l2jserver.gameserver.model.SeedProduction;

/**
 * @author l3x
 */
public final class BuyListSeed extends L2GameServerPacket {
	private final int _manorId;
	private final long _money;
	private final List<SeedProduction> _list = new ArrayList<>();
	
	public BuyListSeed(long currentMoney, int castleId) {
		_money = currentMoney;
		_manorId = castleId;
		
		for (SeedProduction s : CastleManorManager.getInstance().getSeedProduction(castleId, false)) {
			if ((s.getAmount() > 0) && (s.getPrice() > 0)) {
				_list.add(s);
			}
		}
	}
	
	@Override
	protected void writeImpl() {
		writeC(0xe9);
		
		writeQ(_money); // current money
		writeD(_manorId); // manor id
		
		if (!_list.isEmpty()) {
			writeH(_list.size()); // list length
			for (SeedProduction s : _list) {
				writeD(s.getId());
				writeD(s.getId());
				writeD(0x00);
				writeQ(s.getAmount()); // item count
				writeH(0x05); // Custom Type 2
				writeH(0x00); // Custom Type 1
				writeH(0x00); // Equipped
				writeD(0x00); // Body Part
				writeH(0x00); // Enchant
				writeH(0x00); // Custom Type
				writeD(0x00); // Augment
				writeD(-1); // Mana
				writeD(-9999); // Time
				writeH(0x00); // Element Type
				writeH(0x00); // Element Power
				for (byte i = 0; i < 6; i++) {
					writeH(0x00);
				}
				// Enchant Effects
				writeH(0x00);
				writeH(0x00);
				writeH(0x00);
				writeQ(s.getPrice()); // price
			}
			_list.clear();
		} else {
			writeH(0x00);
		}
	}
}