#!/usr/bin/perl -w

use strict;
use Text::CSV::Simple;
use WWW::Mechanize;

my $default_username = "AutoKake";
my $default_comment = "Add travelcard zone.";
my $datafile = "rgldata-zones";

my $new_page_url = "http://london.randomness.org.uk/newpage.cgi";

my $parser = Text::CSV::Simple->new;
$parser->want_fields( 3, 5 );
$parser->field_map( qw( name zone ) );
my @data = $parser->read_file( $datafile );

print "Finished reading in data.\n";

if ( scalar @data ) {
  print scalar( @data ) . " records found.\n";
} else {
  print "Error: no records found.  Exiting.\n";
  exit 0;
}

shift @data; # Remove headings.
foreach my $datum ( @data ) {
  if ( !$datum ) {
    print "Warning: blank datum.\n";
    next;
  }

  my $name = $datum->{name};
  my $zone = $datum->{zone};

  if ( !$name ) {
    print "ERROR: blank name.  Exiting.\n";
    exit 0;
  }
  if ( !$zone ) {
    print "ERROR: blank zone.  Exiting.\n";
    exit 0;
  }

  if ( $zone == 7 ) { $zone = "A"; }
  elsif ( $zone == 8 ) { $zone = "B"; }
  elsif ( $zone == 9 ) { $zone = "C"; }
  elsif ( $zone == 10 ) { $zone = "D"; }

  if ( $zone !~ /^[1-6A-D](\.5)?$/ ) {
    print "ERROR: invalid zone $zone.  Exiting.\n";
    exit 0;
  }

  $name .= " Station";
  $name =~ s/&/and/g;
  $name =~ s/\bSt\. /St /;
  print "Checking for existence of $name...\n";
  if ( !page_exists( $name ) ) {
    print "Warning: $name doesn't exist; skipping.\n";
  } else {
    print "$name found; editing it...\n";
    add_zone( pagename => $name, zone => $zone );
  }
}

sub page_exists {
  my $pagename = shift;
  my $agent = WWW::Mechanize->new();
  $agent->get( $new_page_url );
  $agent->submit_form(
                       form_number => 2,
                       fields      => {
                                        pagename => $pagename,
                                      },
                     );
  $agent->follow_link( text => "(cancel edit)" );
  my $html = $agent->content();
  if ( $html =~ /We don't have a page called/ ) {
    return 0;
  }
  return 1;
}

sub add_zone {
  my %args = @_;
  my $pagename = $args{pagename};
  my $zone = $args{zone};

  my $agent = WWW::Mechanize->new();
  $agent->get( $new_page_url );
  $agent->submit_form(
                       form_number => 2,
                       fields      => {
                                        pagename => $pagename,
                                      },
                     );

  # Hack around bug in WWW::Mechanize.
  my $locales = $agent->value( "locales" );
  $locales =~ s/\n/\r\n/gs;
  my $categories = $agent->value( "categories" );
  if ( !$categories ) {
    print "WARNING: no existing categories found.  Skipping.\n";
    return;
  }

  my @cats = split /\n/, $categories;
  my %cathash = map { $_ => 1 } @cats;

  if ( $zone =~ /^[1-6A-D]$/ ) {
    $cathash{ "Zone $zone Stations" } = 1;
  } else {
    $zone =~ s/\.5$//;
    $cathash{ "Zone $zone Stations" } = 1;
    if ( $zone == 6 ) {
      $cathash{ "Zone A Stations" } = 1;
    } else {
      $cathash{ "Zone $zone Stations" } = 1;
      $cathash{ "Zone " . ( $zone + 1 ) . " Stations" } = 1;
    }
  }

  $categories = join "\r\n", keys %cathash;

  $agent->submit_form(
                       form_number => 1,
                       fields      => {
                                        locales => $locales,
                                        categories => $categories,
                                        username => $default_username,
                                        comment => $default_comment,
                                        edit_type => "Minor tidying",
                                      },
                       button => "Save",
                     );
  print "Processed $pagename\n";
  return;
}
