require "date"

module Util
    def self.parse_epoch_ms(v)
        return nil if v.nil? || v.to_s.strip.empty?
        begin
            Time.at(v.to_i / 1000).to_date
        rescue ArgumentError, RangeError, TypeError
            nil
        end
    end

    def self.parse_aus_date(text)
        s = text.to_s.strip
        return nil if s.empty?
        ["%d/%m/%Y", "%d/%m/%y"].each do |fmt|
            begin
                return Date.strptime(s, fmt)
            rescue ArgumentError, Date::Error
                next
            end
        end
        Date.parse(s)
    rescue ArgumentError, Date::Error
        nil
    end
    COUNCIL_MAP = {
        "BREAK_ODAY"         => "da_break_oday",
        "BRIGHTON"           => "da_brighton",
        "BURNIE"             => "da_burnie",
        "CENTRAL_COAST"      => "da_centralcoast",
        "CENTRAL_HIGHLANDS"  => "da_centralhighlands",
        "CIRCULAR_HEAD"      => "da_circularhead",
        "CLARENCE"           => "da_clarence",
        "DERWENT_VALLEY"     => "da_derwentvalley",
        "DEVONPORT"          => "da_devonportcity",
        "DORSET"             => "da_dorset",
        "FLINDERS"           => "da_flinders_council",
        "GEORGE_TOWN"        => "da_georgetown",
        "GLAMORGAN-SPRING_BAY" => "da_glamorgan",
        "GLENORCHY"          => "da_glenorchy",
        "HOBART"             	 => "da_hobartcity",
        "HUON_VALLEY"        => "da_huonvalley",
        "KENTISH"            => "da_kentish",
        "KINGBOROUGH"        => "da_kingborough",
        "KING_ISLAND"        => "da_kingisland",
        "LATROBE"            => "da_latrobe",
        "LAUNCESTON"         => "da_launceston",
        "MEANDER_VALLEY"     => "da_meandervalley",
        "NORTHERN_MIDLANDS"  => "da_northernmidlands",
        "SORELL"             => "da_sorell",
        "SOUTHERN_MIDLANDS"  => "da_southernmidlands",
        "TASMAN"             => "da_tasman",
        "WARATAH-WYNYARD"    => "da_waratah_wynyard",
        "WEST_COAST"         => "da_westcoast",
        "WEST_TAMAR"         => "da_westtamar"
        }

    PLANBUILD_CODE_MAP = {
        "BOD" => "BREAK_ODAY",
        "BRI" => "BRIGHTON",
        "BUR" => "BURNIE",
        "CCO" => "CENTRAL_COAST",
        "CHD" => "CIRCULAR_HEAD",
        "CEH" => "CENTRAL_HIGHLANDS",
        "CLA" => "CLARENCE",
        "DER" => "DERWENT_VALLEY",
        "DEV" => "DEVONPORT",
        "DOR" => "DORSET",
        "FLI" => "FLINDERS",
        "GEO" => "GEORGE_TOWN",
        "GLC" => "GLENORCHY",
        "GSP" => "GLAMORGAN-SPRING_BAY",
        "HOB" => "HOBART",
        "HUO" => "HUON_VALLEY",
        "KEN" => "KENTISH",
        "KIN" => "KINGBOROUGH",
        "KIS" => "KING_ISLAND",
        "LAT" => "LATROBE",
        "LAU" => "LAUNCESTON",
        "MEA" => "MEANDER_VALLEY",
        "NMI" => "NORTHERN_MIDLANDS",
        "SOR" => "SORELL",
        "SOM" => "SOUTHERN_MIDLANDS",
        "TAS" => "TASMAN",
        "WAR" => "WARATAH-WYNYARD",
        "WCO" => "WEST_COAST",
        "WTA" => "WEST_TAMAR"
        }

    # returns the table name
    def self.ref_to_table(ref)
        code = ref.split("-")[1].to_s.upcase
        council_key = PLANBUILD_CODE_MAP[code]
        raise "Unknown council code: #{code}" unless council_key

        # make sure the key exists in COUNCIL_MAP
        table = COUNCIL_MAP[council_key]
        raise "No table mapping for council: #{council_key}" unless table
		table
    end

    # returns the folder name for PDFs
    def self.ref_to_folder(ref)
        code = ref.split("-")[1].to_s.upcase
        council_name = PLANBUILD_CODE_MAP[code]
        raise "Unknown council code: #{code}" unless council_name
        council_name  # use full council name for folder
    end

end