package dynamo.torrents.transmission;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import dynamo.core.Enableable;
import dynamo.core.configuration.Configurable;
import dynamo.core.manager.DownloadableFactory;
import dynamo.core.model.TaskExecutor;
import dynamo.jdbi.SearchResultDAO;
import dynamo.manager.DownloadableManager;
import dynamo.model.Downloadable;
import dynamo.model.DownloadableStatus;
import dynamo.model.result.SearchResult;
import dynamo.model.result.SearchResultType;

public class TransmissionCheckDaemonExecutor extends TaskExecutor<TransmissionCheckDaemonTask> implements Enableable {

	@Configurable(ifExpression="dynamo.torrents.transmission.DownloadTorrentTransmissionExecutor", defaultValue="2.0")
	private float limitUploadRatio = 2.0f;

	public float getLimitUploadRatio() {
		return limitUploadRatio;
	}
	
	public void setLimitUploadRatio(float limitUploadRatio) {
		this.limitUploadRatio = limitUploadRatio;
	}
	
	@Override
	public boolean isEnabled() {
		return Transmission.getInstance().isEnabled();
	}
	
	private SearchResultDAO searchResultDAO;

	public TransmissionCheckDaemonExecutor(TransmissionCheckDaemonTask task, SearchResultDAO searchResultDAO) {
		super(task);
		this.searchResultDAO = searchResultDAO;
	}

	@Override
	public void execute() throws Exception {

		List<SearchResult> results = searchResultDAO.getActiveSearchResults( SearchResultType.TORRENT );
		Transmission transmission = Transmission.getInstance();
		List<TransmissionResponseTorrent> torrents = transmission.getTorrents();

		for (SearchResult searchResult : results) {
			boolean torrentFound = false;
			
			Downloadable downloadable = DownloadableFactory.getInstance().createInstance( searchResult.getDownloadableId() );
			if (downloadable == null) {
				searchResultDAO.deleteResultForDownloadableId( searchResult.getDownloadableId() );
				continue;
			}

			for (TransmissionResponseTorrent torrent : torrents) {
				
				if (Integer.parseInt(searchResult.getClientId()) != torrent.getId()) {
					continue;
				}
				
				torrentFound = true;
				if (torrent.getDoneDate() > 0) {	
					Path sourceFolder = Paths.get( torrent.getDownloadDir() );
					List<Path> files = new ArrayList<>();
					for (ApiFile file : torrent.getFiles()) {
						if (file.getLength() > 0) {
							String fileName = file.getName().replace('?', '');
							files.add( sourceFolder.resolve(fileName) );
						}
					}
					// MAYBE_FIXME : sourceFolder is local to the transmission server, not to the dynamo server
					// This only works if transmission and dynamo are on the same server !
					
					boolean move = false;
					if (limitUploadRatio <= 0) {
						move = true;
						transmission.remove( torrent.getId(), false );
					}

					DownloadableManager.getInstance().downloaded(task, downloadable, searchResult, sourceFolder, files, move );
					searchResultDAO.freeClientId(searchResult.getClientId());
				}
			}

			if (!torrentFound) {
				// torrent was deleted manually ?
				searchResultDAO.freeClientId(searchResult.getClientId());
				if ( downloadable.getStatus() == DownloadableStatus.SNATCHED){
					DownloadableManager.getInstance().blackListSearchResult( searchResult.getUrl() );
					DownloadableManager.getInstance().want(downloadable);
				}
			}
		}
		
		List<SearchResult> blackListedResults = searchResultDAO.getBlackListedSearchResults( SearchResultType.TORRENT );
		for (SearchResult searchResult : blackListedResults) {
			if (searchResult.getClientId() != null) {
				for (TransmissionResponseTorrent torrent : torrents) {
					if (Integer.parseInt(searchResult.getClientId()) == torrent.getId()) {
						transmission.remove( torrent.getId(), true );
						searchResultDAO.freeClientId(searchResult.getClientId());
						break;
					}
				}
			}
		}
		
		for (TransmissionResponseTorrent torrent : torrents) {
			if (torrent.getDoneDate() > 0 && torrent.getUploadRatio() > limitUploadRatio) {
				
				// delete the files : FIXME : how to check that we have no file copy or move in progress ?
				searchResultDAO.freeClientId("" + torrent.getId());
				transmission.remove( torrent.getId(), true );
			}
		}
		
	}

}

@@ -2476,186 +2476,8 @@
 %09%09%7D%0A
-%09%09%09%09%09// MAYBE_FIXME : sourceFolder is local to the transmission server, not to the dynamo server%0A%09%09%09%09%09// This only works if transmission and dynamo are on the same server !%0A%09%09%09%09%09
 %0A%09%09%09
@@ -3799,29 +3799,24 @@
 oadRatio) %7B%0A
-%09%09%09%09%0A
 %09%09%09%09// delet
@@ -3825,84 +3825,41 @@
 the 
-files : FIXME : how to check that we have no file copy or move in progress ?
+torrent, the files will be moved 
 %0A%09%09%09
@@ -3945,35 +3945,36 @@
 orrent.getId(), 
-tru
+fals
 e );%0A%09%09%09%7D%0A%09%09%7D%0A%09%09

Fix torrent downloads