package com.github.privacystreams.communication;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.github.privacystreams.accessibility.BaseAccessibilityEvent;
import com.github.privacystreams.accessibility.utils.AccessibilityUtils;
import com.github.privacystreams.core.Callback;
import com.github.privacystreams.core.Item;
import com.github.privacystreams.core.commons.common.ItemCommons;
import com.github.privacystreams.core.commons.comparison.Comparisons;
import com.github.privacystreams.core.providers.MultiItemStreamProvider;
import com.github.privacystreams.core.purposes.Purpose;

import java.util.List;


/**
 * Created by fanglinchen on 1/31/17.
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class IMUpdatesProvider extends MultiItemStreamProvider {
    private int totalNumberOfMessages=0;

    public static final String APP_PACKAGE_WHATSAPP = "com.whatsapp";
    public static final String APP_PACKAGE_FACEBOOK_MESSENGER = "com.facebook.orca";


    private void saveNewMessage(List<AccessibilityNodeInfo> nodeInfoList, String contactName, String packageName) {
        totalNumberOfMessages += 1;
        AccessibilityNodeInfo nodeInfo = nodeInfoList.get(nodeInfoList.size() - 1);
        String messageContent = nodeInfoList.get(nodeInfoList.size() - 1).getText().toString();
        Message.Type messageType = AccessibilityUtils.isIncomingMessage(nodeInfo,packageName) ? Message.Type.RECEIVED : Message.Type.SENT;
        System.out.println("add new message");
        this.output(new Message(messageType,messageContent,packageName,contactName,System.currentTimeMillis()));

    }
    @Override
    protected void provide() {

        getUQI().getDataItems(BaseAccessibilityEvent.asUpdates(),
                Purpose.internal("Event Triggers"))
                .filter(ItemCommons.isFieldIn(BaseAccessibilityEvent.PACKAGE_NAME,
                        new String[]{APP_PACKAGE_WHATSAPP, APP_PACKAGE_FACEBOOK_MESSENGER}))
                .filter(Comparisons.eq(BaseAccessibilityEvent.EVENT_TYPE, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED))
                .filter(Comparisons.gt(BaseAccessibilityEvent.ITEM_COUNT, 2))
                .forEach(new Callback<Item>() {
                    @Override
                    protected void onSuccess(Item input) {

                        AccessibilityNodeInfo rootView =
                                input.getValueByField(BaseAccessibilityEvent.ROOT_VIEW);
                        String packageName = input.getValueByField(BaseAccessibilityEvent.PACKAGE_NAME);

                        List<AccessibilityNodeInfo> nodeInfos =
                                AccessibilityUtils.getMessageList(rootView,packageName);
                        String contactName = AccessibilityUtils
                                .getContactNameInChat(rootView,packageName);

                        AccessibilityNodeInfo textBox = AccessibilityUtils
                                .getTextBox(rootView, packageName);

                        int eventItemCount = input.getValueByField(BaseAccessibilityEvent.ITEM_COUNT);
                        eventItemCount-=2;
                        if(textBox==null || nodeInfos==null || nodeInfos.size()==0){
                            return;
                        }
                        if(totalNumberOfMessages==0){
                            initializing(eventItemCount);
                        }
                        else if (eventItemCount - totalNumberOfMessages == 1) {
                            saveNewMessage(nodeInfos, contactName,packageName);

                        }
                    }
                });

    }
    public boolean initializing(int eventItemCount) {
        try {
            totalNumberOfMessages = eventItemCount;
            return true;        }
        catch (Exception e) {
            Log.e("Exception", e.toString());
            return false;
        }
    }
}
@@ -119,33 +119,8 @@
 pi;%0A
-import android.util.Log;%0A
 impo
@@ -165,24 +165,24 @@
 ilityEvent;%0A
+
 import andro
@@ -960,16 +960,76 @@
 sages=0;
+%0A    private int result=0;%0A    private String detPackage=%22%22;
 %0A%0A    pu
@@ -1654,55 +1654,8 @@
 NT;%0A
-        System.out.println(%22add new message%22);%0A
     
@@ -2700,17 +2700,204 @@
 _NAME);%0A
+                        if(!packageName.equals(detPackage))%7B%0A                            totalNumberOfMessages=0;%0A                        %7D%0A                        detPackage=packageName;
 %0A
-
         
@@ -3376,107 +3376,44 @@
 t = 
-input.getValueByField(BaseAccessibilityEvent.ITEM_COUNT);%0A                        eventItemCount-=2
+getEventItemCount(packageName,input)
 ;%0A  
@@ -3549,32 +3549,33 @@
               %7D%0A
+%0A
                 
@@ -3922,24 +3922,373 @@
        %7D);%0A%0A
+    %7D%0A    public int getEventItemCount(String pckName,  Item input)%7B%0A        int temp = input.getValueByField(BaseAccessibilityEvent.ITEM_COUNT);%0A        if(pckName.equals(APP_PACKAGE_WHATSAPP))%7B%0A            result=temp-2;%0A        %7Delse if(pckName.equals(APP_PACKAGE_FACEBOOK_MESSENGER))%7B%0A            result=temp-1;%0A        %7D%0A        return result;%0A
     %7D%0A    pu
@@ -4427,32 +4427,32 @@
  true;        %7D%0A
+
         catch (E
@@ -4469,54 +4469,8 @@
 ) %7B%0A
-            Log.e(%22Exception%22, e.toString());%0A
     

update im update provider to support more back and forth switching between different im apps