/*
 * Copyright 2017 CHIMPS Lab, Carnegie Mellon University
 *
 * 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 edu.cmu.chimps.messageontap_api;

import android.text.TextUtils;

import java.util.Set;
import java.util.regex.Pattern;

public class Tag {

    private String mName;
    private String mEntityName;
    private String mPackageName;
    private Set<String> mRegularExpressions;
    private Integer usageCount;

    public Tag(String name, Set<String> RegularExpressions) {
        this.mName = name;
        this.mRegularExpressions = RegularExpressions;
        this.usageCount = 0;
    }

    public Tag(String name, String entityName) {
        this.mName = name;
        this.mEntityName = entityName;
        this.usageCount = 0;
    }

    public void setName(String name) {
        mName = name;
    }

    public String getName() {
        return mName;
    }

    public void setKeywordList(Set keywordList) {
        mRegularExpressions = keywordList;
    }

    public Set<String> getKeywordList() {
        return mRegularExpressions;
    }

    public void setEntityName(String entityName) {
        this.mEntityName = entityName;
    }

    public String getEntityName() {
        return mEntityName;
    }

    /**
     * Check whether this tag matches a word using regular expressions
     *
     * @param word the word to be checked
     * @return boolean whether it matches
     */
    private boolean matchRE(String word) {
        Set<String> keywordList = getKeywordList();

        for (String str : keywordList) {
            if (Pattern.matches(str, word))
                return true;
        }
        return false;
    }

    /**
     * Check whether this tag matches a word using entity recognition
     *
     * @param entity the entity to be checked
     * @return boolean whether it matches
     */
    private boolean matchEntity(String entity) {
        return TextUtils.equals(entity, getEntityName());
    }

    /**
     * Check whether this tag matches a word
     *
     * @param word   the word to be checked
     * @param entity the entity of the word to be checked
     * @return boolean whether it matches
     */
    public boolean matchWord(String word, String entity) {
        if (getKeywordList() != null)
            if (matchRE(word))
                return true;
        if (getEntityName() != null)
            if (getEntityName() != null && matchEntity(entity))
                return true;

        return false;
    }

    public void increaseUsageCount() {
        usageCount++;
    }

    public void decreaseUsageCount() {
        usageCount--;
    }

    public void resetUsageCount() {
        usageCount = 0;
    }

    public Integer getUsageCount() {
        return usageCount;
    }

    public String getPackageName() {
        return mPackageName;
    }

    public void setPackageName(String packageName) {
        this.mPackageName = packageName;
    }
}

@@ -930,16 +930,190 @@
 Count;%0A%0A
+    // The following variable should be ignored by plugin developers%0A    private String ENTITY_INTERNAL_KEY = %22MessageOnTap_entity_INTERNAL_vimIsTheBestLOLbyAdamYi_hahaha%22;%0A%0A
     publ
@@ -1144,17 +1144,17 @@
 String%3E 
-R
+r
 egularEx
@@ -1205,36 +1205,143 @@
     
-this.mRegularExpressions = R
+Boolean isCommonTag = false;%0A        String entity = null;%0A        if (regularExpressions.size() == 2) %7B%0A            for (String re : r
 egul
@@ -1353,26 +1353,30 @@
 ressions
-;%0A
+) %7B%0A  
         
 this.usa
@@ -1371,149 +1371,331 @@
     
-this.usageCount = 0
+      if (TextUtils.equals(re, ENTITY_INTERNAL_KEY))%0A                    isCommonTag = true
 ;%0A    
-%7D%0A%0A
     
-public Tag(String name, String entityName) %7B%0A        this.mName = name;%0A        this.mEntityName = entityName
+        else%0A                    entity = re;%0A            %7D%0A        %7D%0A        if (isCommonTag && entity != null)%0A            this.mEntityName = entity;%0A        else%0A            this.mRegularExpressions = regularExpressions
 ;%0A  
@@ -1878,35 +1878,49 @@
  set
-KeywordList(Set keywordList
+RegularExpressions(Set regularExpressions
 ) %7B%0A
@@ -1949,27 +1949,34 @@
 sions = 
-keywordList
+regularExpressions
 ;%0A    %7D%0A
@@ -2002,27 +2002,34 @@
 ing%3E get
-KeywordList
+RegularExpressions
 () %7B%0A   
@@ -2072,105 +2072,8 @@
  %7D%0A%0A
-    public void setEntityName(String entityName) %7B%0A        this.mEntityName = entityName;%0A    %7D%0A%0A
     
@@ -2372,90 +2372,45 @@
     
-Set%3CString%3E keywordList = getKeywordList();%0A%0A        for (String str : keywordList
+for (String str : mRegularExpressions
 ) %7B%0A
@@ -3106,19 +3106,26 @@
 (get
-KeywordList
+RegularExpressions
 () !

Update Tag class