module OSMModel

  class Point
    attr_accessor :osmid, :visible, :lat, :lon, :tags
    
    def initialize point, tags={} #expect long, lat point
      @osmid = 0
      @visible="true"
      @lat = point[1]
      @lon = point[0]
      @tags = tags
    end

    def to_xml
      if tags.empty? then
        "<node id='-#{@osmid}' visible='#{@visible}' lat='#{@lat}' lon='#{@lon}' />"
      else
        ret = ["<node id='-#{@osmid}' visible='#{@visible}' lat='#{@lat}' lon='#{@lon}'>"]
        @tags.each do |k, v|
          ret << "<tag k=\"#{k}\" v=\"#{v}\" />"
        end
        ret << "</node>"
        ret.join("\n")
      end
    end
  end

  class Segment
    attr_accessor :osmid, :from, :to

    def initialize from, to
      @osmid = 0
      @from = from
      @to = to
    end

    def to_xml
      "<segment id='-#{@osmid}' visible='true' from='-#{@from.osmid}' to='-#{@to.osmid}' />"
    end
  end

  class Way
    attr_accessor :osmid, :segs, :tags
    
    def initialize
      @osmid = 0
      @segs = []
      @tags = {}
    end

    def to_xml
      ret = ["<way id='-#{@osmid}' visible='#{@visible}'>"]
      @segs.each do |seg|
        ret << "<seg id='-#{seg.osmid}' />"
      end
      @tags.each do |k, v|
        ret << "<tag k=\"#{k}\" v=\"#{v}\" />"
      end
      ret << "</way>"
      ret.join("\n")
    end

  end

end