package apps.stisser.karissa.feelgood;

/**
 * Created by yfadila on 3/4/2017.
 */

import android.os.Handler;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessagesBroker {

    public interface MessageCallBack {
        void onMessage(Message message);
    }

    public static Map<String, MessageCallBack> callBackMap = new HashMap<>();

    public static List<Message> getMessages(String chatRoom, MessageCallBack callBack){
        if (callBack !=null) {
            callBackMap.put(chatRoom, callBack);
        }
        List<Message> msgs = new ArrayList<>();
        for(int i=0; i< allMessages.size(); i++) {
            //(int id, String time, String from, String content, String topic)
            if (allMessages.get(i).getAsJsonArray().get(2).toString().replace("\"","").equals(chatRoom)) {
                Message e = new Message(allMessages.get(i).getAsJsonArray());
                msgs.add(e);
            }
        }
        return  msgs;
    }

    private static int lastId = 0;
    private static final String BASE_URL = "http://fadila.net/feelgood/index.php/messages";
    private static Gson gson = new Gson();
    private static JsonArray allMessages = new JsonArray();
    private static final Handler h = new Handler();
    public static JsonArray fetchMessages() {
        JsonArray records = null;

        try {
            URL url = new URL(BASE_URL + "?filter=id,gt," + lastId);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output = br.readLine();
            JsonElement jelem = gson.fromJson(output, JsonElement.class);
            records = jelem.getAsJsonObject().getAsJsonObject("messages").getAsJsonArray("records");
            if (records.size() > 0)
            {
                lastId = Integer.parseInt(records.get(records.size() - 1).getAsJsonArray().get(0).toString());
            }

            conn.disconnect();
        } catch (MalformedURLException e) {

            e.printStackTrace();


        } catch (IOException e) {

            e.printStackTrace();
        }
        return records;
    }
    // bad practice, This is for 1st POC stage only.
    public static void addMessage(Message msg) {

        try {
            URL object = new URL(BASE_URL);
            HttpURLConnection con = (HttpURLConnection) object.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestMethod("POST");

            OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
            wr.write(msg.getStringToPost());
            wr.flush();
            int HttpResult = con.getResponseCode();
            if (HttpResult != HttpURLConnection.HTTP_OK) {
                System.out.println(con.getResponseMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void addMessageSync(final Message msg) {
        h.post(new Runnable() {
            @Override
            public void run() {
                addMessage(msg);
            }
        });
    }
    public static void pollMessages(){
        final int delay = 2000; //milliseconds
        h.postDelayed(new Runnable(){
            public void run(){
                JsonArray newMessgaes = fetchMessages();
                if (newMessgaes.size() != 0) {
                    allMessages.addAll(newMessgaes);
                    for (int i=0; i<newMessgaes.size(); i++) {
                        String chatroom = newMessgaes.get(i).getAsJsonArray().get(2).toString();
                        if (callBackMap.get(chatroom) != null)
                            callBackMap.get(chatroom).onMessage(new Message(newMessgaes.get(i).getAsJsonArray()));
                    }
                }
                h.postDelayed(this, delay);
            }
        }, delay);
    }

    public static void main(String[] args) throws InterruptedException {
        addMessage(new Message(943,"","111111111112","slama","lilya"));

    }
}

@@ -81,36 +81,8 @@
 */%0A%0A
-import android.os.Handler;%0A%0A
 impo
@@ -181,37 +181,8 @@
 t;%0A%0A
-import org.json.JSONObject;%0A%0A
 impo
@@ -498,16 +498,111 @@
 til.Map;
+%0Aimport java.util.concurrent.ScheduledThreadPoolExecutor;%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Apublic
@@ -1644,24 +1644,121 @@
 Array();%0A   
+ private static ScheduledThreadPoolExecutor executor = %09new ScheduledThreadPoolExecutor(2);%0A   //
  private sta
@@ -4016,17 +4016,18 @@
 dMessage
-S
+As
 ync(fina
@@ -4051,22 +4051,32 @@
         
-h.post
+executor.execute
 (new Run
@@ -4200,24 +4200,25 @@
   %7D);%0A    %7D%0A
+%0A
     public s
@@ -4298,30 +4298,44 @@
 %0A       
- h.postDelayed
+executor.scheduleAtFixedRate
 (new Run
@@ -4925,16 +4925,18 @@
         
+//
 h.postDe
@@ -4979,22 +4979,50 @@
       %7D,
- 
 delay
+,delay, TimeUnit.MILLISECONDS
 );%0A    %7D
@@ -5096,16 +5096,40 @@
 ption %7B%0A
+        pollMessages();%0A
         
@@ -5134,24 +5134,29 @@
   addMessage
+Async
 (new Message
@@ -5168,20 +5168,11 @@
 %22%22,%22
-111111111112
+vvv
 %22,%22s
@@ -5184,21 +5184,20 @@
 ,%22lilya%22));%0A
-%0A
     %7D%0A%7D%0A

use executor