package org.iotivity.bridge.homeserver.model;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

public class OCFDevice {

    protected static final String TAG = OCFDevice.class.getName() ;

    // device key name
    static public final String NAME_KEY = "n";
    static public final String DI_KEY = "di";

    // resource key name
    static public final String RESOURCE_NAME_KEY = "n" ;
    static public final String RESOURCE_INTERFACE_KEY = "if" ;
    static public final String RESOURCE_TYPE_KEY = "rt" ;

    protected String device_name ;
    protected String device_type ;

    protected ArrayList<Resource> resources = new ArrayList<Resource>() ;

    public class Resource {

        private String resource_name ;
        private String resource_uri ;
        private String resource_rt ;
        private String resource_if ;
        private Properties properties = new Properties() ;

        public Resource(String resource_name, String resource_uri, String resource_rt, String resource_if) {
            this.resource_name = resource_name ;
            this.resource_uri = resource_uri ;
            this.resource_rt = resource_rt ;
            this.resource_if = resource_if ;
        }

        public String getResourceName() { return resource_name; }
        public void setResourceName(String resource_name) {
            this.resource_name = resource_name ;
        }

        public String getResourceUri() {
            return resource_uri;
        }
        public void setResourceUri(String resource_uri) {
            this.resource_uri = resource_uri;
        }

        public String getResourceType() {
            return resource_rt;
        }
        public void setResourceType(String resource_rt) {
            this.resource_rt = resource_rt;
        }

        public String getResourceInterface() {
            return resource_if;
        }

        public void setResourceInterface(String resource_if) {
            this.resource_if = resource_if;
        }

        public Properties getProperties() {
            return properties;
        }

        public void addOrReplaceProperty(String key, Object value) {
            properties.put(key, value);
        }
    };

    public OCFDevice(String device_name, String device_type) {

        this.device_name = device_name ;
        this.device_type = device_type ;
    }

    public String getDeviceName() {
        return device_name;
    }
    public void setDeviceName(String name) {
        this.device_name = name ;
    }

    public String getDeviceType() {
        return device_type;
    }
    public void setDeviceType(String device_type) {
        this.device_type = device_type;
    }

    public ArrayList<Resource> getResources() {
        return resources;
    }

    public Resource getResource(String resource_uri) {
        for(Resource resource : resources) {
            if (resource.getResourceUri().equalsIgnoreCase(resource_uri)) {
                return resource ;
            }
        }
        return null ;
    }

    public void addResource(String resource_name, String resource_uri, String resource_type, String resource_if) {
        resources.add(new Resource(resource_name, resource_uri, resource_type, resource_if));
    }

    public void removeResource(int index) {
        resources.remove(index);
    }

    public void removeResource(String uri) {
        ArrayList<Resource> resources1 = (ArrayList<Resource>)resources.clone();
        for(int loop = 0 ; loop < resources1.size() ; loop++) {
            Resource resource1 = resources1.get(loop);
            if(resource1.getResourceUri().equals(uri)) resources.remove(loop) ;
        }
    }

    public void addProperty(String resource_uri, String key, Object value) {
        Resource resource = getResource(resource_uri);
        resource.addOrReplaceProperty(key, value);
    }

    public void addProperties(String resource_uri, String properties) {
        Resource resource = getResource(resource_uri);
        try {
            JSONObject props = new JSONObject(properties);
            Iterator<String> keys = props.keys();
            while(keys.hasNext()) {
                String key = keys.next() ;
                Object value = props.get(key);
                resource.addOrReplaceProperty(key, value);
            }
        } catch (JSONException e) {
            Log.e(TAG, "JSON Error :", e);
        }
    }

    public String getProperties(String resource_uri) {
        Resource resource = getResource(resource_uri);
        if(resource!=null) {
            Properties props = resource.getProperties();
            if(props.size() == 0) return null ;
            else {
                JSONObject obj = new JSONObject(props);
                return obj.toString();
            }
        }
        return null ;
    }

}
