import whatap.pack.tagcount_pack as tagcount_pack
import whatap.osinfo as osinfo
import whatap.agent.secure.security_master as secure
from whatap.util.hash_util import HashUtil as hashutil
from whatap.value.list_value import ListValue
from whatap.value.null_value import NullValue


class TaskInfraNicInfo:
    NIC_INFO = "infra_nic_info"

    def __init__(self, interval=60):
        self.intervalValue = interval
        self.recType = 2

    def interval(self):
        return self.intervalValue

    def process(self, os_, pcode, oid, now):
        nic_list = osinfo.GetNicInfoList()
        if not nic_list:
            return None

        secu = secure.GetSecurityMaster()
        p = tagcount_pack.TagCountPack()
        p.pcode = pcode
        p.oid = oid
        p.time = now
        p.category = TaskInfraNicInfo.NIC_INFO
        p.putTag("oname", str(secu.oname))
        p.putTag("!rectype", str(self.recType))

        idList = ListValue()
        nicNameList = ListValue()
        ipAddressList = ListValue()
        macAddressList = ListValue()
        linkList = ListValue()
        stateList = ListValue()
        duplexList = ListValue()
        speedList = ListValue()
        modeList = ListValue()
        windowsDriverList = ListValue()

        for nic in nic_list:
            key = hashutil.hashFromString("{}{}".format(oid, nic.nicName))
            idList.addLong(key)
            nicNameList.addString(nic.nicName)
            ipAddressList.addString(nic.ipAddress if nic.ipAddress else "")
            macAddressList.addString(nic.macAddress if nic.macAddress else "")
            linkList.addString(nic.link if nic.link else "")
            stateList.addString(nic.state if nic.state else "")

            if nic.duplex is not None:
                duplexList.addString(nic.duplex)
            else:
                duplexList.addValue(NullValue())

            if nic.speed is not None and nic.speed > 0:
                speedList.addFloat(nic.speed)
            else:
                speedList.addValue(NullValue())

            if nic.mode is not None:
                modeList.addString(nic.mode)
            else:
                modeList.addValue(NullValue())

            windowsDriverList.addValue(NullValue())

        p.put("@id", idList)
        p.put("nicName", nicNameList)
        p.put("ipAddress", ipAddressList)
        p.put("macAddress", macAddressList)
        p.put("link", linkList)
        p.put("state", stateList)
        p.put("duplex", duplexList)
        p.put("speed", speedList)
        p.put("mode", modeList)
        p.put("windowsDriver", windowsDriverList)

        return [p]
