package clientP2P;
import java.util.List;
import java.util.ArrayList;
import java.net.Socket;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import protocolP2P.ProtocolP2PPacketTCP;
import protocolP2P.ProtocolP2PPacket;
import protocolP2P.Payload;
import protocolP2P.LoadRequest;
import protocolP2P.FilePart;
import localException.InternalError;
import localException.ProtocolError;
import localException.TransmissionError;
import localException.VersionError;
import localException.SizeError;
import localException.SocketClosed;
import remoteException.EmptyDirectory;
import remoteException.EmptyFile;
import remoteException.InternalRemoteError;
import remoteException.VersionRemoteError;
import remoteException.ProtocolRemoteError;
import remoteException.NotFound;
import remoteException.NotATracker;
import tools.Logger;
import tools.LogLevel;
import clientP2P.ClientDownloadPart;

/** Class to download file parts on tcp.
* @author	Louis Royer
* @author	Flavien Haas
* @author	JS Auge
* @version	1.0
*/
public class ClientDownloadPartTCP extends ClientDownloadPart {

	private Socket socket;

	/** Constructor with filename, socket, and part subdir
	 * @param filename	name of file to download
	 * @param socket	socket to use
	 * @param partsSubdir	directory to store .part files
	 * @param logger	Logger
	 */
	public ClientDownloadPartTCP(ClientDownload manager, String filename, Socket socket, String partsSubdir, Logger logger) {
		super(manager, filename, partsSubdir, logger);
		this.socket = socket;
	}


	
	/** Get the socket */
	protected Object getSocket() {
		return (Object) socket;
	}
	
	/** Close the socket
	 */
	protected void closeSocket() throws IOException {
		socket.close();
	}

	/** Implementation of writeLog
	 * @param text	Text to log
	 * @param logLevel	level of logging
	 */
	protected void writeLog(String text, LogLevel logLevel) {
		logger.writeTCP(text, logLevel);
	}

	/** Implementation of writeLog
	 * @param e	exception to log
	 * @param logLevel	level of logging
	 */
	protected void writeLog(Exception e, LogLevel logLevel) {
		logger.writeTCP(e, logLevel);
	}


	/** Create packets
	 * @param payload	Payload
	 */
	protected < T extends Payload > ProtocolP2PPacket<T> createProtocolP2PPacket(T payload) {
		return (ProtocolP2PPacket<T>)new ProtocolP2PPacketTCP<T>(payload);
	}
}
