"""Add resource record."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import helpers
# pylint: disable=redefined-builtin


@click.command()
@click.argument('record')
@click.argument('type')
@click.argument('data')
@click.option('--zone',
              help="Zone name or identifier that the resource record will be associated with.\n"
                   "Required for all record types except PTR")
@click.option('--ttl',
              default=900,
              show_default=True,
              help='TTL value in seconds, such as 86400')
@click.option('--priority',
              default=10,
              show_default=True,
              help='The priority of the target host. (MX or SRV type only)')
@click.option('--protocol',
              type=click.Choice(['tcp', 'udp', 'tls']),
              default='tcp',
              show_default=True,
              help='The protocol of the service, usually either TCP or UDP. (SRV type only)')
@click.option('--port',
              type=click.INT,
              help='The TCP/UDP/TLS port on which the service is to be found. (SRV type only)')
@click.option('--service',
              help='The symbolic name of the desired service. (SRV type only)')
@click.option('--weight',
              default=5,
              show_default=True,
              help='Relative weight for records with same priority. (SRV type only)')
@environment.pass_env
def cli(env, record, type, data, zone, ttl, priority, protocol, port, service, weight):
    """Add resource record.

    Each resource record contains a RECORD and DATA property, defining a resource's name and it's target data.
    Domains contain multiple types of resource records so it can take one of the following values: A, AAAA, CNAME,
    MX, SPF, SRV, and PTR.

    About reverse records (PTR), the RECORD value must to be the public Ip Address of device you would like to manage
    reverse DNS.

        slcli dns record-add 10.10.8.21 PTR myhost.com --ttl=900

    Examples:

        slcli dns record-add myhost.com A 192.168.1.10 --zone=foobar.com --ttl=900

        slcli dns record-add myhost.com AAAA 2001:DB8::1 --zone=foobar.com

        slcli dns record-add 192.168.1.2 MX 192.168.1.10 --zone=foobar.com --priority=11 --ttl=1800

        slcli dns record-add myhost.com TXT "txt-verification=rXOxyZounZs87oacJSKvbUSIQ" --zone=2223334

        slcli dns record-add myhost.com SPF "v=spf1 include:_spf.google.com ~all" --zone=2223334

        slcli dns record-add myhost.com SRV 192.168.1.10 --zone=2223334 --service=foobar --port=80 --protocol=TCP

    """

    manager = SoftLayer.DNSManager(env.client)
    type = type.upper()

    if zone and type != 'PTR':
        zone_id = helpers.resolve_id(manager.resolve_ids, zone, name='zone')

        if type == 'MX':
            manager.create_record_mx(zone_id, record, data, ttl=ttl, priority=priority)
        elif type == 'SRV':
            manager.create_record_srv(zone_id, record, data, protocol, port, service,
                                      ttl=ttl, priority=priority, weight=weight)
        else:
            manager.create_record(zone_id, record, type, data, ttl=ttl)

    elif type == 'PTR':
        manager.create_record_ptr(record, data, ttl=ttl)
    else:
        raise exceptions.CLIAbort("%s isn't a valid record type or zone is missing" % (type))

    click.secho("%s record added successfully" % (type), fg='green')

@@ -2738,24 +2738,31 @@
 client)%0A    
+record_
 type = type.
@@ -2786,16 +2786,23 @@
 one and 
+record_
 type != 
@@ -2897,16 +2897,23 @@
      if 
+record_
 type == 
@@ -3011,32 +3011,39 @@
 y)%0A        elif 
+record_
 type == 'SRV':%0A 
@@ -3265,32 +3265,39 @@
 one_id, record, 
+record_
 type, data, ttl=
@@ -3311,16 +3311,23 @@
    elif 
+record_
 type == 
@@ -3490,15 +3490,20 @@
 %22 %25 
-(
+record_
 type)
-)
 %0A%0A  
@@ -3553,14 +3553,19 @@
 %22 %25 
-(
+record_
 type
-)
 , fg

Use record_type instead of only type since this is a built