module MyRepresentatives
  module NSW
    class CSVUpper
      include MyRepresentatives::Fileable
      include MyRepresentatives::Guessable

      attr_accessor :csv_url, :csv_filename, :people

      def initialize
        @csv_url      = "https://www.parliament.nsw.gov.au/_layouts/15/NSWParliament/memberlistservice.aspx?members=LC&format=Excel"
        @csv_filename = "tmp/csv_nsw_upper.csv"
        @people       = []

        csv_from_url
        people_from_csv
      end

      private

        def csv_from_url
          create_tmp
          begin
            open(@csv_filename, "wb") do |file|
              open(@csv_url) do |uri|
                 file.write(uri.read)
              end
            end
          rescue SocketError => err
            @logger.debug("Unable to connect to #{@csv_url}")
            raise err
          end
        end

        def people_from_csv
          CSV.foreach(@csv_filename, headers: true) do |row|

            # Setup the electorate
            electorate_name = find_electorate
            electorate      = Electorate.new(electorate_name)
            electorate.state_nsw_upper!

            # Setup the Representative (Person)
            person = Person.new(electorate)

            person.first_name                 = find_first_name(row)
            person.last_name                  = find_last_name(row)
            person.title                      = find_title(row)
            person.honorifics                 = find_honorifics(row)
            person.email                      = find_email(row)
            person.phone                      = find_phone(row)
            person.party_name                 = find_party(row)
            person.salutation                 = find_salutation(row)
            person.formal_name                = format_formal_name(person)
            person.physical_address           = format_physical_address(row)
            person.postal_address             = format_postal_address(row)
            person.gender                     = guess_gender(person.title)
            person.preferred_name             = nil
            person.image_url                  = nil
            person.homepage_url               = nil

            @people << person
          end
        end

        def find_first_name(arr)
          arr["INITIALS"]
        end

        def find_last_name(arr)
          arr["SURNAME"]
        end

        def find_title(arr)
          arr["TITLE"]
        end

        def find_honorifics(arr)
          arr["POST-NOMINALS"]
        end

        def find_electorate
          "All NSW"
        end

        def find_email(arr)
          arr["CONTACT ADDRESS EMAIL"]
        end

        def find_party(arr)
          arr["PARTY"]
        end

        def find_phone(arr)
          arr["CONTACT ADDRESS PHONE"]
        end

        def find_salutation(arr)
          arr["SALUTATION"]
        end

        def format_formal_name(person)
          "#{person.title} #{person.first_name} #{person.last_name} #{person.honorifics}"
        end

        def format_physical_address(arr)
          "#{arr["CONTACT ADDRESS LINE1"]}\n#{arr["CONTACT ADDRESS LINE2"]}\n#{arr["CONTACT ADDRESS LINE3"]}\n#{arr["CONTACT ADDRESS SUBURB"]} #{arr["CONTACT ADDRESS STATE"]} #{arr["CONTACT ADDRESS POSTCODE"]}"
        end

        def format_postal_address(arr)
          "#{arr["CONTACT ADDRESS LINE1"]}\n#{arr["CONTACT ADDRESS LINE2"]}\n#{arr["CONTACT ADDRESS LINE3"]}\n#{arr["CONTACT ADDRESS SUBURB"]} #{arr["CONTACT ADDRESS STATE"]} #{arr["CONTACT ADDRESS POSTCODE"]}"
        end

    end # CSVUpper
  end # NSW
end #MyRepresentatives
