#include "SimCore/Messages/Emission.hpp"

namespace SimCore 
{

    Emission::Emission(std::string name)
    :ID_(SimCore::Identifier())
    {
        this->name_.setValue(name);
    }
   
    SimCore::Emission::Emission(std::string name, SimCore::Identifier id)
    :ID_(id)
    {
        this->name_.setValue(name);
    }
   
    messages::SimTrack::Emission SimCore::Emission::packEmission()
    {   
        messages::SimTrack::Emission msg;
        msg.set_name(this->name_.getValue());

        msg.mutable_identifier()->set_number(this->ID_.getNumber());
        msg.mutable_identifier()->set_external((uint32_t)this->ID_.isExternal());
        msg.mutable_identifier()->set_parent(this->ID_.getParentNumber());
        msg.mutable_identifier()->set_uuid(this->ID_.getUUID());   

        msg.set_range(Range.getValue());
        msg.set_frequency(Frequency.getValue());
        msg.set_bandwidth(Bandwidth.getValue());

        if (msg.has_pulsewidth())
        {
             msg.set_pulsewidth(PulseWidth.getValue());
        }
        if (msg.has_pulserepitition())
        {
        msg.set_pulserepitition(PulseRepitition.getValue());
        }

        if (msg.has_radiatedpower())
        {
        msg.set_radiatedpower(RadiatedPower.getValue());
        }

        if (msg.has_searchinterval())
        {
            msg.set_searchinterval(searchInterval.getValue());   
        }

        if (msg.has_numberofbeams())
        {
            msg.set_numberofbeams(NumberOfBeams.getValue());
        }
            
        return msg;

    }

    Emission Emission::unpackEmission(messages::SimTrack::Emission msg)
    {
        SimCore::Emission emission(msg.name(),SimCore::Identifier(msg.identifier()));

        emission.Range.setValue(msg.range());
        emission.Frequency.setValue(msg.frequency());
        emission.Bandwidth.setValue(msg.bandwidth());
        emission.PulseWidth.setValue(msg.pulsewidth());
        emission.PulseRepitition.setValue(msg.pulserepitition());
        emission.RadiatedPower.setValue(msg.radiatedpower());
        emission.NumberOfBeams.setValue(msg.numberofbeams());
        emission.searchInterval.setValue(msg.searchinterval());

        return emission;
    }

    SimCore::Identifier Emission::getID() const
    {
        return this->ID_;
    }

    std::string Emission::getName()
    {
        return this->name_.getValue();
    }

    bool operator==(const Emission &lhs, const Emission &rhs)
    {
        if (lhs.getID().getUUID() == rhs.getID().getUUID())
        {
            return true;
        }
        
        return false;
    }
}