# Copyright (C) 2005 Laurent Sansonetti <lrz@gnome.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

module MARC
    class Record
        attr_reader :values

        def initialize(string)
            @values = {}
            parse(string) 
        end

        def title
            if value = @values[200]
                value['a']
            elsif value = @values[245]
                if title = value['a']
                    full_title = if remainder = value['b']
                        title + ' ' + remainder
                    else
                        title
                    end
                    full_title.sub(/\s+\/$/, '')
                end
            end
        end

        def isbn
            if value = @values[20]
                value = value.first if value.kind_of?(Array)
                if isbn = value['a'] 
                    isbn[0..9]
                end
            end
        end

        def authors
            authors = []
            if value = @values[700]
                if value.kind_of?(Array)
                    value.each do |value|
                        if name = value['a']
                            authors << name
                        end
                    end
                else
                    if name = value['a']
                        authors << name
                    end
                end
            end
            authors.map { |x| x.sub(/\s*,$/, '') }
        end

        def edition 
            if value = @values[200]
                value['b']
            end
        end

        def publisher
            if value = @values[260]
                if publisher = value['b']
                    publisher.sub(/\s*,$/, '')
                end
            end
        end

        #######
        private
        #######

        def parse(string)
            regex = /^(\d\d\d)[\s\d\|]+(\$.+$)$/
            @values.clear
            string.split(/\n/).each do |line|
                next unless md = regex.match(line)
                tag, value = md[1].to_i, md[2]
                next if tag == 0
                value = hash_for_value(value)
                if @values.has_key?(tag)
                    existing_value = @values[tag]
                    if existing_value.kind_of?(Array)
                        existing_value << value
                    else
                        ary = []
                        ary << existing_value
                        ary << value
                        @values[tag] = ary
                    end
                else
                    @values[tag] = value
                end
            end
        end

        def hash_for_value(string)
            hash = {}
            string.split('$').each do |token|
                token.strip!
                next if token.length < 3
                code = token[0..0]
                value = token[2..-1]
                hash[code] = value 
            end
            return hash
        end
    end
end
