package com.conveyal.gtfs.model;

import com.conveyal.gtfs.GTFSFeed;

import java.io.IOException;
import java.net.URL;
import java.util.Iterator;

public class Agency extends Entity {

    private static final long serialVersionUID = -2825890165823575940L;
    public String agency_id;
    public String agency_name;
    public URL    agency_url;
    public String agency_timezone;
    public String agency_lang;
    public String agency_phone;
    public URL    agency_fare_url;
    public URL    agency_branding_url;
    public String feed_id;

    public static class Loader extends Entity.Loader<Agency> {

        public Loader(GTFSFeed feed) {
            super(feed, "agency");
        }

        @Override
        protected boolean isRequired() {
            return true;
        }

        @Override
        public void loadOneRow() throws IOException {
            Agency a = new Agency();
            a.sourceFileLine = row + 1; // offset line number by 1 to account for 0-based row index
            a.agency_id    = getStringField("agency_id", false); // can only be absent if there is a single agency -- requires a special validator.
            a.agency_name  = getStringField("agency_name", true);
            a.agency_url   = getUrlField("agency_url", true);
            a.agency_lang  = getStringField("agency_lang", false);
            a.agency_phone = getStringField("agency_phone", false);
            a.agency_timezone = getStringField("agency_timezone", true);
            a.agency_fare_url = getUrlField("agency_fare_url", false);
            a.agency_branding_url = getUrlField("agency_branding_url", false);
            a.feed = feed;
            a.feed_id = feed.feedId;

            // TODO clooge due to not being able to have null keys in mapdb
            if (a.agency_id == null) a.agency_id = "NONE";

            feed.agency.put(a.agency_id, a);
        }

    }

    public static class Writer extends Entity.Writer<Agency> {
        public Writer(GTFSFeed feed) {
            super(feed, "agency");
        }

        @Override
        public void writeHeaders() throws IOException {
            writer.writeRecord(new String[] {"agency_id", "agency_name", "agency_url", "agency_lang",
                    "agency_phone", "agency_timezone", "agency_fare_url", "agency_branding_url"});
        }

        @Override
        public void writeOneRow(Agency a) throws IOException {
            writeStringField(a.agency_id);
            writeStringField(a.agency_name);
            writeUrlField(a.agency_url);
            writeStringField(a.agency_lang);
            writeStringField(a.agency_phone);
            writeStringField(a.agency_timezone);
            writeUrlField(a.agency_fare_url);
            writeUrlField(a.agency_branding_url);
            endRecord();
        }

        @Override
        public Iterator<Agency> iterator() {
            return this.feed.agency.values().iterator();
        }
    }

}

@@ -406,16 +406,48 @@
 y_lang;%0A
+    public String agency_email;%0A
     publ
@@ -466,24 +466,24 @@
 ency_phone;%0A
-
     public U
@@ -1360,32 +1360,100 @@
 _lang%22, false);%0A
+            a.agency_email = getStringField(%22agency_email%22, false);%0A
             a.ag
@@ -2346,24 +2346,40 @@
 ency_phone%22,
+ %22agency_email%22,
  %22agency_tim
@@ -2696,16 +2696,16 @@
 _lang);%0A
-
         
@@ -2738,24 +2738,70 @@
 ncy_phone);%0A
+            writeStringField(a.agency_email);%0A
             

add missing agency_email field