package com.ustwo.clockwise.companion.permissions;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.google.android.gms.wearable.DataMap;
import com.ustwo.clockwise.common.Constants;
import com.ustwo.clockwise.common.MessageReceivedHandler;
import com.ustwo.clockwise.common.WearableAPIHelper;
import com.ustwo.clockwise.common.permissions.PermissionRequestActivity;
import com.ustwo.clockwise.common.permissions.PermissionRequestItem;
import com.ustwo.clockwise.wearable.permissions.PermissionsRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class CompanionPermissionRequestMessageHandler implements MessageReceivedHandler {
    private static final String TAG = CompanionPermissionRequestMessageHandler.class.getSimpleName();

    private Context mContext;

    public CompanionPermissionRequestMessageHandler(Context context) {
        mContext = context;
    }

    @Override
    public void onMessageReceived(String path, DataMap map, WearableAPIHelper apiHelper) {
        if(path.equals(Constants.DATA_PATH_COMPANION_PERMISSION_REQUEST)) {
            boolean justChecking = map.getBoolean(Constants.DATA_KEY_JUST_CHECKING);
            byte[] requestBytes = map.getByteArray(Constants.DATA_KEY_PERMISSION_REQUEST);
            PermissionsRequest request = PermissionsRequest.deserialize(requestBytes);

            // if requesting silently, just get the results and return them instead of invoking the activity
            if(request.shouldRequestSilently()) {
                HashMap<String, Boolean> companionPermissionResults = new HashMap<>();
                for(PermissionRequestItem requestItem : request.getRequestItems()) {
                    if(requestItem.isWearable()) continue;

                    for(String companionPermission : requestItem.getPermissions()) {
                        if (ContextCompat.checkSelfPermission(mContext, companionPermission) == PackageManager.PERMISSION_GRANTED) {
                            companionPermissionResults.put(companionPermission, true);
                        } else {
                            companionPermissionResults.put(companionPermission, false);
                        }
                    }
                }

                DataMap dataMap = new DataMap();
                dataMap.putLong(Constants.DATA_KEY_TIMESTAMP, System.currentTimeMillis());
                ArrayList<DataMap> results = new ArrayList<>();
                for(String permission : companionPermissionResults.keySet()) {
                    DataMap resultMap = new DataMap();
                    resultMap.putString(Constants.DATA_KEY_COMPANION_PERMISSION, permission);
                    resultMap.putBoolean(Constants.DATA_KEY_COMPANION_PERMISSION_GRANTED, companionPermissionResults.get(permission));
                    results.add(resultMap);
                }
                dataMap.putDataMapArrayList(Constants.DATA_KEY_COMPANION_PERMISSION_RESULTS, results);
                apiHelper.putMessage(Constants.DATA_PATH_COMPANION_PERMISSION_RESPONSE, dataMap.toByteArray(), null);
                //apiHelper.putDataMap(Constants.DATA_PATH_COMPANION_PERMISSION_RESPONSE, dataMap, null);
            } else {
                Intent i = new Intent(mContext.getApplicationContext(), PermissionRequestActivity.class);
                i.putExtra(PermissionRequestActivity.EXTRA_COMPANION_JUST_CHECKING, justChecking);
                i.putExtra(PermissionRequestActivity.EXTRA_PERMISSION_REQUEST, requestBytes);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.getApplicationContext().startActivity(i);
            }
        }
    }

    @Override
    public List<String> getSupportedPaths() {
        return Arrays.asList(Constants.DATA_PATH_COMPANION_PERMISSION_REQUEST);
    }
}

@@ -1517,17 +1517,54 @@
 Bytes);%0A
-%0A
+            if(request != null) %7B%0A    
         
@@ -1668,34 +1668,39 @@
 ity%0A            
+  
+  
 if
+ 
 (request.shouldR
@@ -1715,24 +1715,28 @@
 lently()) %7B%0A
+    
             
@@ -1826,19 +1826,24 @@
         
+    
 for
+ 
 (Permiss
@@ -1912,34 +1912,39 @@
                 
+    
 if
+ 
 (requestItem.isW
@@ -1977,35 +1977,40 @@
                 
+  
+  
 for
+ 
 (String companio
@@ -2075,24 +2075,28 @@
             
+   
+ 
 if (ContextC
@@ -2212,32 +2212,36 @@
                 
+    
 companionPermiss
@@ -2299,32 +2299,36 @@
                 
+    
 %7D else %7B%0A       
@@ -2312,32 +2312,36 @@
        %7D else %7B%0A
+    
                 
@@ -2428,34 +2428,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -2480,24 +2480,28 @@
             
+    
 %7D%0A%0A         
@@ -2499,32 +2499,36 @@
                 
+ 
+   
 DataMap dataMap 
@@ -2536,32 +2536,36 @@
  new DataMap();%0A
+    
                 
@@ -2655,16 +2655,20 @@
         
+    
 ArrayLis
@@ -2723,19 +2723,24 @@
         
+ 
+   
 for
+ 
 (String 
@@ -2803,32 +2803,36 @@
                 
+   
+ 
 DataMap resultMa
@@ -2842,32 +2842,36 @@
  new DataMap();%0A
+    
                 
@@ -2960,32 +2960,36 @@
                 
+ 
+   
 resultMap.putBoo
@@ -3103,24 +3103,28 @@
             
+    
 results.add(
@@ -3143,34 +3143,40 @@
                 
-%7D%0A
+    %7D%0A  
                 
@@ -3167,32 +3167,34 @@
                 
+  
 dataMap.putDataM
@@ -3260,24 +3260,28 @@
 , results);%0A
+    
             
@@ -3402,16 +3402,20 @@
         
+ 
+   
 //apiHel
@@ -3500,32 +3500,36 @@
 l);%0A            
+   
+ 
 %7D else %7B%0A       
@@ -3513,32 +3513,36 @@
        %7D else %7B%0A
+    
                 
@@ -3639,32 +3639,36 @@
                 
+    
 i.putExtra(Permi
@@ -3742,32 +3742,36 @@
                 
+   
+ 
 i.putExtra(Permi
@@ -3848,16 +3848,20 @@
         
+    
 i.setFla
@@ -3903,32 +3903,36 @@
                 
+  
+  
 mContext.getAppl
@@ -3966,16 +3966,259 @@
 ity(i);%0A
+                %7D%0A            %7D else %7B%0A                // send back empty data map if we receive request with bad data%0A                apiHelper.putMessage(Constants.DATA_PATH_COMPANION_PERMISSION_RESPONSE, new DataMap().toByteArray(), null);%0A
         

check for bad PermissionRequestItem