package FixMyStreet::Geocode::Address::Bing;
use parent 'FixMyStreet::Geocode::Address';

sub brand { "Bing Maps" }

sub label { _("Nearest road to the pin placed on the map (automatically generated by %s): %s") }

sub address {
    return $_[0]->{resourceSets}[0]{resources}[0]{address};
}

sub summary {
    my $address = $_[0]->address;
    return $address->{formattedAddress};
}

sub parts {
    my $address = $_[0]->address;
    my ($number, $street) = $address->{addressLine} =~ /\s*(\d*)\s*(.*)/;
    return {
        number => $number || '',
        street => $street || '',
        postcode => $address->{postalCode} || '',
    };
}

sub for_around {
    my $address = $_[0]->address;
    return {
        road => $address->{addressLine},
        full_address => $address->{formattedAddress},
    };
}

sub for_alert {
    my $self = shift;

    my $str = '';

    my $address = $self->address;
    my @address;
    push @address, $address->{addressLine} if $address->{addressLine} && $address->{addressLine} !~ /^Street$/i;
    push @address, $address->{locality} if $address->{locality};
    $str .= sprintf($self->label, $self->brand, join( ', ', @address ) ) if @address;

    return $str;
}

1;
