package org.araymond.joal.core.ttorrent.client.announcer.response;

import org.araymond.joal.core.ttorrent.client.Client;
import org.araymond.joal.core.ttorrent.client.announcer.exceptions.TooMuchAnnouncesFailedInARawException;
import org.araymond.joal.core.ttorrent.client.announcer.request.Announcer;
import org.araymond.joal.core.ttorrent.client.announcer.request.SuccessAnnounceResponse;
import org.slf4j.Logger;

import static org.slf4j.LoggerFactory.getLogger;

public class ClientNotifier implements AnnounceResponseHandlerChainElement {
    private static final Logger logger = getLogger(ClientNotifier.class);
    private Client client;

    public void setClient(final Client client) {
        this.client = client;
    }

    @Override
    public void onAnnouncerWillAnnounce(final Announcer announcer) {
    }

    @Override
    public void onAnnounceStartSuccess(final Announcer announcer, final SuccessAnnounceResponse result) {
    }

    @Override
    public void onAnnounceStartFails(final Announcer announcer, final Throwable throwable) {
    }

    @Override
    public void onAnnounceRegularSuccess(final Announcer announcer, final SuccessAnnounceResponse result) {
    }

    @Override
    public void onAnnounceRegularFails(final Announcer announcer, final Throwable throwable) {
    }

    @Override
    public void onAnnounceStopSuccess(final Announcer announcer, final SuccessAnnounceResponse result) {
        if(logger.isDebugEnabled()) {
            logger.debug("Notify client that a torrent has stopped.");
        }
        this.client.onTorrentHasStopped(announcer.getTorrent());
    }

    @Override
    public void onAnnounceStopFails(final Announcer announcer, final Throwable throwable) {
    }

    @Override
    public void onTooManyAnnounceFailedInARaw(final Announcer announcer, final TooMuchAnnouncesFailedInARawException e) {
        if(logger.isDebugEnabled()) {
            logger.debug("Notify client that a torrent has stopped.");
        }
        this.client.onTorrentHasStopped(announcer.getTorrent());
    }
}

@@ -928,38 +928,180 @@
 ponse result) %7B%0A
+        if (result.getSeeders() == 0 %7C%7C result.getLeechers() == 0) %7B%0A            this.client.onNoMorePeers(announcer.getTorrent());%0A        %7D%0A
     %7D%0A
-
 %0A    @Override%0A 
@@ -1317,24 +1317,164 @@
 e result) %7B%0A
+        if (result.getSeeders() %3C 1 %7C%7C result.getLeechers() %3C 1) %7B%0A            this.client.onNoMorePeers(announcer.getTorrent());%0A        %7D%0A
     %7D%0A%0A    @
@@ -1867,37 +1867,24 @@
 ed(announcer
-.getTorrent()
 );%0A    %7D%0A%0A  
@@ -2277,46 +2277,35 @@
 onTo
-rrentHasStopped(announcer.getTorrent()
+oManyFailedInARaw(announcer
 );%0A 

Improve client notification (more accurate)