package us.ihmc.aci.envMonitor.provider.arl;

import java.net.URI;
import us.ihmc.aci.envMonitor.AsyncProvider;
import us.ihmc.aci.util.ByteArrayHandler;
import us.ihmc.aci.util.SNMPHelper;
import us.ihmc.aci.util.ConfigLoader;
import us.ihmc.aci.util.SNMPWalker;
import us.ihmc.aci.AttributeList;
import us.ihmc.ds.fgraph.FGraph;

public class BBNAdHocSNMPProvider extends AsyncProvider
{
    public BBNAdHocSNMPProvider (String routerIP)
        throws Exception
    {
        _poolInterval = ConfigLoader.getInstance().getPropertyAsInt("arl.provider.POS.poolInterval",_poolInterval);
        _debug = ConfigLoader.getInstance().getPropertyAsBoolean("arl.provider.POS.debug");
        System.out.println("PoolInterval: " + _poolInterval);
        debugMsg ("Debug is ON");

        String shost = getLocalGatewayIP();
        _snmpWalker = new SNMPWalker (shost);

                                        s

        _snmpHelper = new SNMPHelper (routerIP);
        _snmpHelper.addTrapInterruption(this);

        String gssServerHost = ConfigLoader.getInstance().getProperty("gss.server.default.host");
        int gssPort = ConfigLoader.getInstance().getPropertyAsInt("gss.server.default.port");
        if (gssServerHost == null) {
            throw new Exception ("Uknown gssServer host - Faild to locate property (gss.server.default.host)");
        }
        _fgraph = FGraph.getClient(new URI("tcp://" + gssServerHost + ":" + gssPort));
        _nodeName = _environmentalMonitor.getNodeName();
        _nodeIP = (String) _fgraph.getVertexAttribute(_nodeName, AttributeList.NODE_ADHOC_IP);
    }

    public void run()
    {
        while (_running) {
            try {
                Thread.sleep(_sleepTimeout);
                int[] neighborIDList = getListOfNeighbors();
                if (neighborIDList != null) {
                    //set on fgraph instead of printing on screen
                    debugMsg ("Updating rachability ------------------");
                    for (int i=0; i<neighborIDList.length; i++) {
                        debugMsg (""+neighborIDList[i]);
                    }
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //--------------------------------------------------------------------
    public void processTrap (SNMPTrapPDU trapPDU)
    {
        System.out.println ("Trap received");
    }

    //--------------------------------------------------------------------
    private int[] getListOfNeighbors ()
    {
        int[] nodeList = null;
        byte[] rawNodeID = new byte[4];
        try {
            SNMPObject[] varlist = _snmpHelper.getSNMPSequence(BBN_NEIGHBOR_COST_TABLE);
            debugMsg("List of neighbors as (SNMPObjects): " + varlist.length);
            for (int i=0; i<varlist.length; i++) {
                SNMPSequence entry = (SNMPSequence) varlist[i];
                SNMPObjectIdentifier objId = (SNMPObjectIdentifier) entry.getSNMPObjectAt(0);
                debugMsg("Object-Identifier: "+objId.toString());
                SNMPOctetString OctVal = (SNMPOctetString) entry.getSNMPObjectAt(1);
                debugMsg(OctVal.toHexString());
                byte[] binfo = (byte[]) OctVal.getValue();
                for (int k=0; k<binfo.length; k++) {
                    debugMsg ("binfo[" + k + "] " + binfo[k]);
                }
                if (binfo.length > 0) {
                    nodeList = new int[(int)(binfo.length/2)];
                    debugMsg ("nodeList size: " + nodeList.length);
                    for (int j=0; j<nodeList.length; j++) {
                        System.arraycopy(binfo, j*2, rawNodeID, 2, 2);
                        for (int k=0; k<rawNodeID.length; k++) {
                            debugMsg ("rawNode[" + k + "] " + rawNodeID[k]);
                        }
                        int nodeID = ByteArrayHandler.byteArrayToInt(rawNodeID, 0);
                        debugMsg ("nodeID: " + nodeID);
                        nodeList[j] = nodeID;
                    }
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return nodeList;
    }

    public void terminate()
    {
        _running = false;
    }

    //--------------------------------------------------------------------
    //debug
    private void debugMsg (String msg)
    {
        if (_debug) {
            System.out.println("[SNMPProv] " + msg);
        }
    }

    private boolean _debug;
    private FGraph _fgraph;
    private SNMPHelper _snmpHelper;
    private String _nodeName;
    private String _nodeIP;
    private boolean _running = true;
    private long _sleepTimeout = 1000;

    private static final String ENTERPRISE_OID =        "1.3.6.1.4.1";
    private static final String BBN_ENTERPRISE_OID =    "1.3.6.1.4.1.14";
    private static final String BBN_GATEWAY_OID =       "1.3.6.1.4.1.14.5";
    private static final String BBN_ROBOT_OID =         "1.3.6.1.4.1.14.5.15";
    private static final String BBN_INFOEXCHANGE_OID =  "1.3.6.1.4.1.14.5.15.3";
    private static final String BBN_NEIGHOR_LIST_OID =  "1.3.6.1.4.1.14.5.15.3.2";
    private static final String BBN_PATH_LIST_OID =     "1.3.6.1.4.1.14.5.15.3.4";
    private static final String BBN_NUM_NODES_OID =     "1.3.6.1.4.1.14.5.15.3.1";
    private static final String BBN_PATHCOST_TBL_OID =  "1.3.6.1.4.1.14.5.15.3.6";
    private static final String BBN_COST_ENTRY_OID =    "1.3.6.1.4.1.14.5.15.3.6.1";
    private static final String BBN_LIST_ENTRY_OID =    "1.3.6.1.4.1.14.5.15.3.6.1";    //same
    private static final String BBN_NODE_ENERGY_OID =   "1.3.6.1.4.1.14.5.15.3.6.1.7";
    private static final String BBN_FWRDTABLE_NUMBER =  "1.3.6.1.4.1.14.5.15.3.6.1.3";


 private static final String BBN_NEIGHBOR_COST_TABLE =  "1.3.6.1.4.1.14.5.15.3.10.1.4";
    //westhock java... snmp

    private static final String _nbrCost = "1.3.6.1.4.1.14.5.15.3.10.1.4";
    private static final String _gpsPosition = "1.3.6.1.4.1.14.5.15.3.4";
}
