// Path: app/src/main/java/com/nicolasbrailo/vlcfreemote/vlc_connector/http_utils/Wget.java
// public class Wget extends AsyncTask<String, Void, String> {
// 
//     public interface Callback {
//         void onConnectionError(final String message);
//         void onResponse(final String result);
//         void onAuthFailure();
//         void onHttpNotOkResponse();
//     }
// 
//     /**
//      * A callback for an observer that's only interested in the task's status and not its result
//      */
//     public interface CallbackWhenTaskFinished {
//         void onTaskFinished();
//     }
// 
//     private static final int CONN_TIMEOUT_MS = 10000;
//     private static final int HTTP_RESPONSE_UNAUTHORIZED = 401;
//     private static final int HTTP_RESPONSE_OK = 200;
// 
//     private final String auth;
//     private final Callback callback;
//     private final CallbackWhenTaskFinished cb2;
//     private Exception request_exception;
//     private int httpRetCode;
// 
//     public Wget(final String url, final String auth, Callback callback, CallbackWhenTaskFinished cb2) {
//         this.auth = auth;
//         this.callback = callback;
//         this.cb2 = cb2;
//         this.request_exception = null;
//         this.execute(url);
//     }
// 
//     private HttpURLConnection getConnection(final String url) throws IOException {
//         HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
//         conn.setReadTimeout(CONN_TIMEOUT_MS);
//         conn.setConnectTimeout(CONN_TIMEOUT_MS);
//         conn.setRequestMethod("GET");
//         conn.setRequestProperty("Authorization", auth);
//         conn.setDoInput(true);
//         conn.connect();
//         return conn;
//     }
// 
//     private String readAll(HttpURLConnection conn) throws IOException {
//         InputStream is = null;
// 
//         try {
//             is = conn.getInputStream();
//             java.util.Scanner s = new java.util.Scanner(is, "UTF-8").useDelimiter("\\A");
//             return s.hasNext() ? s.next() : "";
//         } finally {
//             if (is != null) {
//                 try {
//                     is.close();
//                 } catch (IOException e) {
//                     // Not much we can do
//                 }
//             }
//         }
//     }
// 
//     @Override
//     protected String doInBackground(String... urls) {
//         try {
//             HttpURLConnection conn = getConnection(urls[0]);
//             httpRetCode = conn.getResponseCode();
//             return readAll(conn);
// 
//         } catch (IOException e) {
//             request_exception = e;
//             return null;
//         }
//     }
// 
//     @Override
//     protected void onPostExecute(String result) {
//         // Let an observer know they're free to schedule new tasks
//         cb2.onTaskFinished();
// 
//         if (request_exception != null) {
//             callback.onConnectionError(request_exception.getMessage());
//         } else if (httpRetCode == HTTP_RESPONSE_UNAUTHORIZED) {
//             callback.onAuthFailure();
//         } else if (httpRetCode == HTTP_RESPONSE_OK) {
//             callback.onResponse(result);
//         } else {
//             callback.onHttpNotOkResponse();
//         }
//     }
// }


import com.nicolasbrailo.vlcfreemote.vlc_connector.http_utils.Wget;
package com.nicolasbrailo.vlcfreemote.vlc_connector;


/**
 * Encapsulates a Vlc command.
 */
public interface VlcCommand {
    /**
     * A callback to invoke on generic failure conditions (ie connection fail, system fail, etc)
     * The idea is to write command-specific callbacks on each command implementation and have an
     * object with this interface handle generic cases which are not meaningful for any specific
     * command (eg there's no meaningful answer from a DirList command to an OOM error)
     */
    interface GeneralCallback {
        /**
         * Invoked when VLC responds to an http request with a 401 status code (unauthorized)
         */
        void onAuthError();

        /**
         * Invoked whenever the connection to a remote vlc server failed for whatever reason.
         */
        void onConnectionError(final String msg);

        /**
         * Invoked when a command couldn't be completed due to a system error (eg failed to
         * load a dyn library, error on the sql query to create a schema... stuff that means
         * system error or application bug). Usually the reasonable thing to do is log en error
         * and crash, or at least disconnect.
         * @param e: Found exception (may be null if not applicable)
         */
        void onSystemError(Exception e);
    }

    /**
     * Should return a URI for a Vlc http command, with no server. (EG: requests/foo.xml)
     * @return URI
     */
    String getCommandPath();

    /**
     * Constructs a callback to handle the output of a Vlc command
     * @param generalCallback A callback for events that don't pertain this command.
     * @return A callback that wraps the general one and dispatches events as needed
     */
// Path: app/src/main/java/com/nicolasbrailo/vlcfreemote/vlc_connector/http_utils/Wget.java
// public class Wget extends AsyncTask<String, Void, String> {
// 
//     public interface Callback {
//         void onConnectionError(final String message);
//         void onResponse(final String result);
//         void onAuthFailure();
//         void onHttpNotOkResponse();
//     }
// 
//     /**
//      * A callback for an observer that's only interested in the task's status and not its result
//      */
//     public interface CallbackWhenTaskFinished {
//         void onTaskFinished();
//     }
// 
//     private static final int CONN_TIMEOUT_MS = 10000;
//     private static final int HTTP_RESPONSE_UNAUTHORIZED = 401;
//     private static final int HTTP_RESPONSE_OK = 200;
// 
//     private final String auth;
//     private final Callback callback;
//     private final CallbackWhenTaskFinished cb2;
//     private Exception request_exception;
//     private int httpRetCode;
// 
//     public Wget(final String url, final String auth, Callback callback, CallbackWhenTaskFinished cb2) {
//         this.auth = auth;
//         this.callback = callback;
//         this.cb2 = cb2;
//         this.request_exception = null;
//         this.execute(url);
//     }
// 
//     private HttpURLConnection getConnection(final String url) throws IOException {
//         HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
//         conn.setReadTimeout(CONN_TIMEOUT_MS);
//         conn.setConnectTimeout(CONN_TIMEOUT_MS);
//         conn.setRequestMethod("GET");
//         conn.setRequestProperty("Authorization", auth);
//         conn.setDoInput(true);
//         conn.connect();
//         return conn;
//     }
// 
//     private String readAll(HttpURLConnection conn) throws IOException {
//         InputStream is = null;
// 
//         try {
//             is = conn.getInputStream();
//             java.util.Scanner s = new java.util.Scanner(is, "UTF-8").useDelimiter("\\A");
//             return s.hasNext() ? s.next() : "";
//         } finally {
//             if (is != null) {
//                 try {
//                     is.close();
//                 } catch (IOException e) {
//                     // Not much we can do
//                 }
//             }
//         }
//     }
// 
//     @Override
//     protected String doInBackground(String... urls) {
//         try {
//             HttpURLConnection conn = getConnection(urls[0]);
//             httpRetCode = conn.getResponseCode();
//             return readAll(conn);
// 
//         } catch (IOException e) {
//             request_exception = e;
//             return null;
//         }
//     }
// 
//     @Override
//     protected void onPostExecute(String result) {
//         // Let an observer know they're free to schedule new tasks
//         cb2.onTaskFinished();
// 
//         if (request_exception != null) {
//             callback.onConnectionError(request_exception.getMessage());
//         } else if (httpRetCode == HTTP_RESPONSE_UNAUTHORIZED) {
//             callback.onAuthFailure();
//         } else if (httpRetCode == HTTP_RESPONSE_OK) {
//             callback.onResponse(result);
//         } else {
//             callback.onHttpNotOkResponse();
//         }
//     }
// }


// Path: app/src/main/java/com/nicolasbrailo/vlcfreemote/vlc_connector/VlcCommand.java
import com.nicolasbrailo.vlcfreemote.vlc_connector.http_utils.Wget;

package com.nicolasbrailo.vlcfreemote.vlc_connector;


/**
 * Encapsulates a Vlc command.
 */
public interface VlcCommand {
    /**
     * A callback to invoke on generic failure conditions (ie connection fail, system fail, etc)
     * The idea is to write command-specific callbacks on each command implementation and have an
     * object with this interface handle generic cases which are not meaningful for any specific
     * command (eg there's no meaningful answer from a DirList command to an OOM error)
     */
    interface GeneralCallback {
        /**
         * Invoked when VLC responds to an http request with a 401 status code (unauthorized)
         */
        void onAuthError();

        /**
         * Invoked whenever the connection to a remote vlc server failed for whatever reason.
         */
        void onConnectionError(final String msg);

        /**
         * Invoked when a command couldn't be completed due to a system error (eg failed to
         * load a dyn library, error on the sql query to create a schema... stuff that means
         * system error or application bug). Usually the reasonable thing to do is log en error
         * and crash, or at least disconnect.
         * @param e: Found exception (may be null if not applicable)
         */
        void onSystemError(Exception e);
    }

    /**
     * Should return a URI for a Vlc http command, with no server. (EG: requests/foo.xml)
     * @return URI
     */
    String getCommandPath();

    /**
     * Constructs a callback to handle the output of a Vlc command
     * @param generalCallback A callback for events that don't pertain this command.
     * @return A callback that wraps the general one and dispatches events as needed
     */
    Wget.Callback getWgetCallback(final GeneralCallback generalCallback);