package org.iotivity.bridge.homeserver.model;

import java.util.ArrayList;

public class Bridge {

    public static final int BRIDGE_INDEX = 0;
    public static final String PLATFORM_MFG_NAME = "SignalInfoComm";

    // Virtual OCF Device List Resource definition
    public static final String VODLIST_RESOURCE_NAME = "vodlist";
    public static final String VODLIST_RT = "oic.r.vodlist";
    public static final String VODLIST_URI = "/OVDListResURI";

    // Virtual OCF Device List Resource properties
    public static final String VODLIST = "vodlist"; // required "vods"
    public static final String VODS = "vods"; // array of vodentry
    public static final String VOD_ENTRY = "vodentry"; // required "n", "di", "echoname"

    // VOD Entry resource properties
    public static final String VOD_NAME = "n";
    public static final String VOD_DI = "di";
    public static final String VOD_ECONAME = "econame";

    // Bridge SecureMode Resource definition
    public static final String SECUREMODE_RESOURCE_NAME = "secureMode";
    public static final String SECUREMODE_RT = "oic.r.securemode";
    public static final String SECUREMODE_URI = "/SecureModeResURI";

    //public static final String SECUREMODE = "secureMode";

    // Bridge Info variables
    private String name ;
    private String econame ; // TODO : check if di must need ??
    private String di ;  // TODO : check if di must need ??

    // SecureMode
    private boolean secureMode;
    // Virtual OCF Device List
    private ArrayList<VirtualOCFDevice> vod_lists = new ArrayList<VirtualOCFDevice>();


    public Bridge(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public boolean isSecureMode() {
        return secureMode;
    }
    public void setSecureMode(boolean secureMode) {
        this.secureMode = secureMode;
    }

    public String getEcoName() {
        return econame;
    }
    public void setEcoName(String econame) {
        this.econame = econame;
    }

    public String getDeviceId() {
        return di;
    }
    public void setDeviceId(String di) {
        this.di = di;
    }

    public ArrayList<VirtualOCFDevice> getVirtualOCFDeviceList() {
        return vod_lists;
    }

    public void clearVirtualOCFDeviceList() {
        vod_lists.clear();
    }

    public boolean addOrReplaceVirtualOCFDevice(VirtualOCFDevice new_vod) {
        for(VirtualOCFDevice vod : vod_lists) {
            if(vod.getVODIndex() == new_vod.getVODIndex()) {
                vod.setVirtualDeviceId(new_vod.getVirtualDeviceId());
                vod.setDiscovered(new_vod.isDiscovered());
                vod.setAddedToBridge(new_vod.isAddedToBridge());
                return true ;
            }
        }
        return this.vod_lists.add(new_vod);
    }

    public boolean removeVirtualOCFDevice(VirtualOCFDevice vod) {

        return this.vod_lists.remove(vod);
    }

    public boolean removeVirtualOCFDevice(int vod_index) {
        VirtualOCFDevice vod = getVirtualOCFDevice(vod_index);
        if(vod != null) {
            return removeVirtualOCFDevice(vod);
        }
        return false;
    }

    public VirtualOCFDevice getVirtualOCFDevice(int vod_index) {
        for (VirtualOCFDevice vod : vod_lists) {
            if(vod.getVODIndex() == vod_index) return vod ;
        }
        return null ;
    }

    public VirtualOCFDevice getVirtualOCFDevice(String vod_id) {
        for (VirtualOCFDevice vod : vod_lists) {
            if(vod.getVirtualDeviceId().equalsIgnoreCase(vod_id)) return vod ;
        }
        return null ;
    }

}
