require 'json'
require 'securerandom' # SecureRandom.uuid 

require_relative 'oms_common'

module OMS
    class Antimalware

        def initialize(log)
            @log = log
        end

        # ------------------------------------------------------
        def transform_and_wrap(results, hostname, time)
            @log.info "Antimalware lib started..." + results.to_s
            if results.nil?
                @log.error "Antimalware Assessment failed; Empty input for result"
                wrapper = {
                    "DataType"=>"OPERATION_BLOB",
                    "IPName"=>"LogManagement",
                    "DataItems"=>[
                        {
                            "Timestamp" => OMS::Common.format_time(time),
                            "OperationStatus" => "Error",
                            "Computer" => hostname,
                            "Category" => "Antimalware",
                            "Solution" => "Antimalware",
                            "Detail" => "Antimalware Assessment failed: Empty output"
                        }
                    ]
                }
                # In this case result is null, return null for antimalware_blob and error log
                return nil, wrapper
            end

            antimalware_blob = {
                "DataType"=>"PROTECTION_STATUS_BLOB", 
                "IPName"=>"Antimalware",
                "DataItems"=>[
                    {
                        "DeviceName" => hostname,
                        "OSName" => "Linux",
                        "ProtectionStatusRank" => results["ProtectionStatusRank"],
                        "ProtectionStatus" => results["ProtectionStatus"],
                        "ProtectionStatusDetails" => results["ProtectionStatusDetails"],
                        "DetectionId" => results["DetectionId"],
                        "Threat" => results["Threat"],
                        "ThreatStatusRank" => results["ThreatStatusRank"],
                        "ThreatStatus" => results["ThreatStatus"],
                        "ThreatStatusDetails" => results["ThreatStatusDetails"],
                        "Signature" => results["Signature"],
                        "ScanDate" => results["ScanDate"],
                        "DateCollected" => results["DateCollected"],
                        "Tool" => results["Tool"],
                        "AMProductVersion" => results["AMProductVersion"]                                               
                    }                 
                ]
            }    

            if !results["Error"].nil?              
                msg = "Antimalware Assessment encountered some errors but still return the result: " + results["Error"]
                @log.error msg
                wrapper = {
                    "DataType"=>"OPERATION_BLOB",
                    "IPName"=>"LogManagement",
                    "DataItems"=>[
                        {
                            "Timestamp" => OMS::Common.format_time(time),
                            "OperationStatus" => "Error",
                            "Computer" => hostname,
                            "Category" => "Antimalware",
                            "Solution" => "Antimalware",
                            "Detail" => msg
                        }
                    ]
                }                   
                @log.info "Antimalware info collected with error"
                return antimalware_blob, wrapper
            else               
                return antimalware_blob, nil   
            end         
        end # transform_and_wrap
    end
end