package com.rrs_apps.android.share_to_irc;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.rrs_apps.java.jirclib.IRCConnection;
import com.rrs_apps.java.jirclib.ssl.SSLIRCConnection;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EService;
import org.androidannotations.annotations.UiThread;
import org.parceler.Parcels;

import java.io.IOException;

@EService
public class ShareTextService extends Service implements SendTextAndFinishListener.ICallback {
    public static final String EXTRA_IRC_MESSAGE = "extra_irc_message";
    private static final int NOTIFICATION_ID = 0;

    private final String TAG = getClass().getName();

    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Received text to send to IRC");

        IRCMessage sendText = Parcels.unwrap(intent.getParcelableExtra(EXTRA_IRC_MESSAGE));

        sendTextToServer(sendText.getText(), sendText.getAddress(), sendText.getPort(), sendText.getNick(), sendText.usesSsl(), sendText.getPassword(), sendText.getChannels());

        return START_NOT_STICKY;
    }

    @Background
    void sendTextToServer(String text, String address, int port, String nick, boolean useSsl,
                          String password, final String[] channels) {
        Log.d(TAG, "Connecting to " + address + ":" + port + " as " + nick + ", SSL: " + useSsl);

        // Create connection
        final IRCConnection conn;
        if (useSsl) {
            conn = new SSLIRCConnection(address, port, port, password, nick, nick, nick);
        } else {
            conn = new IRCConnection(address, port, port, password, nick, nick, nick);
        }

        // Add listener that sends the text and finishes the activity upon connecting
        SendTextAndFinishListener ircListener = new SendTextAndFinishListener(this, text, channels, conn, getResources()
                .getString(R.string.quit_message));
        ircListener.setCallback(this);

        conn.addIRCEventListener(ircListener);

        try {
            conn.connect();

            buildAndDisplayNotification(R.string.sharing, true);
        } catch (IOException e) {
            e.printStackTrace();

            Toast.makeText(this, R.string.error_connecting, Toast.LENGTH_LONG).show();
        }
    }

    private void buildAndDisplayNotification(int messageId, boolean showProgress) {
        PendingIntent emptyIntent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(getResources().getString(messageId))
                .setContentIntent(emptyIntent);

        if (showProgress) {
            builder.setProgress(0, 0, true);
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }

    @Override
    @UiThread
    public void onSuccess() {
        Log.d(TAG, "Successfully shared to IRC");

        buildAndDisplayNotification(R.string.share_success, false);
    }

    @Override
    @UiThread
    public void onError() {
        Log.e(TAG, "Error sharing to IRC");

        buildAndDisplayNotification(R.string.share_failure, false);
    }
}

@@ -2531,63 +2531,71 @@
     
-e.printStackTrace();%0A%0A            Toast.makeText(this, 
+Log.i(TAG, %22Error connecting to IRC%22, e);%0A%0A            onError(
 R.st
@@ -2621,32 +2621,9 @@
 ng, 
-Toast.LENGTH_LONG).show(
+e
 );%0A 
@@ -3736,32 +3736,176 @@
         
-buildAnd
+onError(R.string.share_failure);%0A    %7D%0A%0A    private void onError(int errorMessageId) %7B%0A        onError(errorMessageId, null);%0A    %7D%0A%0A    /**%0A     * 
 Display
-N
+s a n
 otificat
@@ -3907,39 +3907,465 @@
 fication
-(R.string.share_failure
+ with an error message%0A     *%0A     * @param errorMessageId The resource ID of the error message to display in the notification%0A     * @param optionalException If not null, the exception that caused the error%0A     */%0A    @UiThread%0A    void onError(int errorMessageId, Exception optionalException) %7B%0A        if (optionalException != null) %7B%0A            optionalException.printStackTrace();%0A        %7D%0A%0A        buildAndDisplayNotification(errorMessageId
 , false)

Handle connection errors properly