package com.devicehive.model;

/*
 * #%L
 * DeviceHive Frontend Logic
 * %%
 * Copyright (C) 2016 - 2017 DataArt
 * %%
 * 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.
 * #L%
 */

import static com.devicehive.configuration.Constants.*;

public class FilterEntity {

    private String deviceId;

    private String networkIds;

    private String deviceTypeIds;

    private String names;

    private boolean returnCommands;

    private boolean returnUpdatedCommands;

    private boolean returnNotifications;

    public FilterEntity(String deviceId, String networkIds, String deviceTypeIds, String names, boolean returnCommands,
                        boolean returnUpdatedCommands, boolean returnNotifications) {
        this.deviceId = deviceId;
        this.networkIds = networkIds;
        this.deviceTypeIds = deviceTypeIds;
        this.names = names;
        this.returnCommands = returnCommands;
        this.returnUpdatedCommands = returnUpdatedCommands;
        this.returnNotifications = returnNotifications;
    }

    public FilterEntity(String filterString) {
        String[] filters = filterString.split("/");

        String typesString = filters[0];
        if (typesString.equals(ANY)) {
            returnCommands = true;
            returnUpdatedCommands = true;
            returnNotifications = true;
        } else {
            if (typesString.contains(COMMAND)) {
                returnCommands = true;
            }

            if (typesString.contains(COMMAND_UPDATE)) {
                returnUpdatedCommands = true;
            }

            if (typesString.contains(NOTIFICATION)) {
                returnNotifications = true;
            }
        }

        networkIds = filters[1];
        if (networkIds.equals(ANY)) networkIds = null;

        deviceTypeIds = filters[2];
        if (deviceTypeIds.equals(ANY)) deviceTypeIds = null;

        deviceId = filters[3];
        if (deviceId.equals(ANY)) deviceId = null;

        names = filters[4];
        if (names.equals(ANY)) names = null;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getNetworkIds() {
        return networkIds;
    }

    public void setNetworkIds(String networkIds) {
        this.networkIds = networkIds;
    }

    public String getDeviceTypeIds() {
        return deviceTypeIds;
    }

    public void setDeviceTypeIds(String deviceTypeIds) {
        this.deviceTypeIds = deviceTypeIds;
    }

    public String getNames() {
        return names;
    }

    public void setNames(String names) {
        this.names = names;
    }

    public boolean isReturnCommands() {
        return returnCommands;
    }

    public void setReturnCommands(boolean returnCommands) {
        this.returnCommands = returnCommands;
    }

    public boolean isReturnUpdatedCommands() {
        return returnUpdatedCommands;
    }

    public void setReturnUpdatedCommands(boolean returnUpdatedCommands) {
        this.returnUpdatedCommands = returnUpdatedCommands;
    }

    public boolean isReturnNotifications() {
        return returnNotifications;
    }

    public void setReturnNotifications(boolean returnNotifications) {
        this.returnNotifications = returnNotifications;
    }
}

@@ -680,16 +680,42 @@
 L%25%0A */%0A%0A
+import java.util.Arrays;%0A%0A
 import s
@@ -1884,39 +1884,102 @@
         
-if (typesString.contain
+Arrays.stream(typesString.split(%22,%22)).forEach(type -%3E %7B%0A                if (type.equal
 s(COMMAN
@@ -1992,32 +1992,36 @@
                 
+   
+ 
 returnCommands =
@@ -2031,35 +2031,43 @@
 ue;%0A            
-%7D%0A%0A
+    %7D%0A%0A    
             if (
@@ -2070,31 +2070,22 @@
 if (type
-sString.contain
+.equal
 s(COMMAN
@@ -2093,24 +2093,28 @@
 _UPDATE)) %7B%0A
+    
             
@@ -2151,35 +2151,43 @@
 ue;%0A            
-%7D%0A%0A
+    %7D%0A%0A    
             if (
@@ -2194,23 +2194,14 @@
 type
-sString.contain
+.equal
 s(NO
@@ -2211,24 +2211,28 @@
 ICATION)) %7B%0A
+    
             
@@ -2275,17 +2275,37 @@
         
-%7D
+    %7D%0A            %7D);
 %0A       

Fix with incorrect command subscription creation