/* 
 * Copyright 2014 OpenMarket Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.matrix.androidsdk.rest.model.bingrules;

import android.text.TextUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.util.JsonUtils;

import java.util.HashMap;
import java.util.regex.Pattern;

public class EventMatchCondition extends Condition {
    public String key;
    public String pattern;

    private static HashMap<String, Pattern> mPatternByRule = null;

    public EventMatchCondition() {
        kind = Condition.KIND_EVENT_MATCH;
    }

    /**
     * Returns whether the given event satisfies the condition.
     * @param event the event
     * @return true if the event satisfies the condition
     */
    public boolean isSatisfied(Event event) {
        JsonObject eventJson = JsonUtils.toJson(event);
        // Extract the value that we want to match
        String fieldVal = extractField(eventJson, key);
        if (TextUtils.isEmpty(fieldVal)) {
            return false;
        }

        if (TextUtils.equals(pattern, fieldVal)) {
            return true;
        }

        if (null == mPatternByRule) {
            mPatternByRule = new HashMap<>();
        }

        Pattern patternEx = mPatternByRule.get(pattern);

        if (null == patternEx) {
            patternEx = Pattern.compile(globToRegex(pattern), Pattern.CASE_INSENSITIVE);
            mPatternByRule.put(pattern, patternEx);
        }

        return patternEx.matcher(fieldVal).matches();
    }

    private String extractField(JsonObject jsonObject, String fieldPath) {
        String[] fieldParts = fieldPath.split("\\.");
        JsonElement jsonElement = null;
        for (String field : fieldParts) {
            jsonElement = jsonObject.get(field);
            if (jsonElement == null) {
                return null;
            }
            if (jsonElement.isJsonObject()) {
                jsonObject = (JsonObject) jsonElement;
            }
        }
        return (jsonElement == null) ? null : jsonElement.getAsString();
    }

    private String globToRegex(String glob) {
        String res = glob.replace("*", ".*").replace("?", ".");

        // If no special characters were found (detected here by no replacements having been made),
        // add asterisks and boundaries to both sides
        if (res.equals(glob)) {
            res = ".*\\b" + res + "\\b.*";
        }
        return res;
    }
}

@@ -1386,106 +1386,413 @@
     
-JsonObject eventJson = JsonUtils.toJson(event);%0A        // Extract the value that we want to match
+String fieldVal = null;%0A%0A        // some informations are in the decrypted event (like type)%0A        if (event.isEncrypted() && (null != event.getClearEvent())) %7B%0A            JsonObject eventJson = JsonUtils.toJson(event.getClearEvent());%0A            fieldVal = extractField(eventJson, key);%0A        %7D%0A%0A        if (TextUtils.isEmpty(fieldVal)) %7B%0A            JsonObject eventJson = JsonUtils.toJson(event);
 %0A   
@@ -1788,38 +1788,35 @@
 event);%0A        
-String
+   
  fieldVal = extr
@@ -1833,32 +1833,43 @@
 ventJson, key);%0A
+        %7D%0A%0A
         if (Text

fix https://github.com/vector-im/vector-android/issues/739 [e2e] Ringtone from call is different according to the encryption state of the room