#!/usr/bin/env python
#
# Copyright (c) 2014,2017 - Adjacent Link LLC, Bridgewater, New Jersey
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
# * Neither the name of Adjacent Link LLC nor the names of its
#   contributors may be used to endorse or promote products derived
#   from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

from __future__ import absolute_import, division, print_function
import sys
from optparse import OptionParser
from emane.events import EventService
from emane.events import AntennaProfileEvent

usage = """emaneevent-antennaprofile [OPTION]... NEMID[:NEMID] \\
         PROFILE ANTENNAAZIMUTH ANTENNAELEVATION

       PROFILE := 'profile'=ID

       ANTENNAAZIMUTH := 'azimuth'=DEGREES

       ANTENNAELEVATION := 'elevation'=DEGREES

       DEGREES := degrees (float)

       ID := Profile Id (uint)
"""

description="Publish an antenna profile event containing NEMs in a \
specified range. Unless one or more targets are specified the antenna \
profile event will be sent to the 'all NEMs' event address. This is \
always what you want to do since all NEMs must be aware of all other \
NEM antenna profiles."

epilog="""
The NEM range specification creates a list of NEMs that will have the
specified antenna profile information.

For example

  emaneevent-antennaprofile 1:10 profile=1 azimuth=45.0 elevation=0.0

Will create a single antenna profile event that contains 10 entries
for NEM 1 to 10, all with the same profile and pointing information.

You can use the '--target' option and the '--reference' to specifically
target one or more NEMs in the range with one of more of the
location values. The NEMs specified with '--target' do not have to be
in the range.

For example

  emaneevent-antennaprofile 1:10 profile=1 azimuth=45.0 elevation=0.0 \\
    -t 3 -t 4 -r 8 -r 9 -r 10

will send antenna profile events to NEM 3 and 4 containing the profile
information for NEMs 8, 9 and 10.

  emaneevent-antennaprofile 13 profile=1 azimuth=45.0 elevation=0.0 \\
     -t 7 -t 8

will send an antenna profile event to NEM 7 and 8 with the profile \\
information for NEM 13.

  emaneevent-antennaprofile 15 -t 0 profile=1 azimuth=45.0 \\
    elevation=0.0

will send an antenna profile event to all NEMs with the profile information
for NEM 15.

"""

class LocalParser(OptionParser):
    def format_epilog(self, formatter):
        return self.epilog

optionParser = LocalParser(usage=usage,
                           description=description,
                           epilog=epilog)

optionParser.add_option("-p",
                        "--port",
                        action="store",
                        type="int",
                        dest="port",
                        default=45703,
                        help="Event channel listen port [default: %default]")

optionParser.add_option("-g",
                        "--group",
                        action="store",
                        type="string",
                        dest="group",
                        default="224.1.2.8",
                        help="Event channel multicast group [default: %default]")

optionParser.add_option("-i",
                        "--device",
                        action="store",
                        type="string",
                        dest="device",
                        help="Event channel multicast device")

optionParser.add_option("-t",
                        "--target",
                        action="append",
                        type="int",
                        dest="target",
                        help="Only send an event to the target")

optionParser.add_option("-r",
                        "--reference",
                        action="append",
                        type="int",
                        dest="reference",
                        help="Send events to NEMs in the range but only include information for the reference NEM.")

(options, args) = optionParser.parse_args()

service = EventService((options.group,options.port,options.device))

if len(args) < 2:
    print("missing arguments", file=sys.stderr)
    exit(1)

nems = args[0].split(':')

if len(nems) == 0 or len(nems) > 2:
    print("invalid NEMID format:",args[0], file=sys.stderr)
    exit(1)

try:
    nems = [int(x) for x in nems]
except:
    print("invalid NEMID format:",args[0], file=sys.stderr)
    exit(1)

if len(nems) > 1:
    nems = list(range(nems[0],nems[1]+1))

if not nems:
    print("invalid target format:",args[0], file=sys.stderr)
    exit(1)

kwargs  = {}

for component in args[1:]:
    try:
        (param,value) = component.split("=")
    except:
       print("invalid component format:",component, file=sys.stderr)
       exit(1)

    if param == 'azimuth' or \
       param == 'elevation':
        try:
            kwargs[param] = float(value)
        except:
            print("invalid %s format:"% param,value, file=sys.stderr)
            exit(1)
    elif  param == 'profile':
       try:
           kwargs[param] = int(value)
       except:
           print("invalid %s format:"% param,value, file=sys.stderr)
           exit(1)
    else:
        print("invalid location component:",param, file=sys.stderr)
        exit(1)

if nems[0] == 0:
    print("0 is not a valid NEMID", file=sys.stderr)
    exit(1)

if options.target:
    targets = options.target
else:
    targets = [0]

if options.reference:
    references = options.reference
else:
    references = nems

for i in targets:
    event = AntennaProfileEvent()
    for j in nems:
        if j in references:
            try:
                event.append(j,**kwargs);
            except Exception as exp:
                print("error:",exp, file=sys.stderr)
                exit(1)


    service.publish(i,event)
