module SVGeez::Output::IOSShapeLayer
	class Renderer
		attr_accessor :options, :svgs

		def initialize options, svgs = {}
			self.options = options
			self.svgs = svgs
		end

		def run
			if options.dry_run then
				fo_h = File.open(File::NULL, "w")
				fo_m = $stdout
			else
				fo_h = File.open(options.output_file + ".h", "w")
				fo_m = File.open(options.output_file + ".m", "w")
			end

			xxsvgs = {}

			self.svgs.keys.each do |key|
				svg = self.svgs[key]

				old_stdout = $stdout

				foo = StringIO.new
				$stdout = foo

				dump_svg svg

				contents = foo.string

				$stdout = old_stdout

				xxsvgs[key] = contents
			end

			fo_h.puts "// Autogenerated by SVGeez. Do not modify!"
			fo_m.puts "// Autogenerated by SVGeez. Do not modify!"

			fo_h.puts "@import UIKit;"
			fo_h.puts

			fo_m.puts "#import \"SVGeez.h\""
			fo_m.puts

			xxsvgs.keys.each do |key|
				fo_h.puts "extern NSString * const SVGeez_#{key};"
				fo_m.puts "NSString * const SVGeez_#{key} = @\"#{key}\";"
			end

			fo_h.puts
			fo_m.puts

			fo_h.puts "@interface SVGeez : NSObject"
			fo_h.puts "+ (CAShapeLayer *)layerForKey:(NSString *)key;"
			fo_h.puts "@end"

			fo_m.puts "@implementation SVGeez"
			fo_m.puts "+ (CAShapeLayer *)layerForKey:(NSString *)key {"
			xxsvgs.keys.each do |key|
				fo_m.puts "\tif ([key isEqualToString:SVGeez_#{key}]) {"
				fo_m.puts "\t\treturn [self _layerForKey_#{key}];"
				fo_m.puts "\t}"
			end
			fo_m.puts "\treturn nil;"
			fo_m.puts "}"
			fo_m.puts

			xxsvgs.each do |key, svg|
				fo_m.puts "+ (CAShapeLayer *)_layerForKey_#{key} {"
				fo_m.puts svg
				fo_m.puts "\tCAShapeLayer *layer = [CAShapeLayer layer];"
				fo_m.puts "\tlayer.frame = frame;"
				fo_m.puts "\tlayer.path = svg.CGPath;"
				fo_m.puts "\treturn layer;"
				fo_m.puts "}"
				fo_m.puts
			end

			fo_m.puts "@end"

			fo_h.close
			fo_m.close
		end

		def dump_svg element
			writer = Writer.new
			writer.indent = 1
			dump_svg_with_children element, writer
		end

		def dump_svg_with_children element, writer
			if "none" == element.state[:display]
				return nil
			end

			if element.source.name == "use" then
				actual_element = element.linked_element
			else
				actual_element = element
			end

			output = nil
			case actual_element.source.name
			when "svg"
				output = SVG.new(actual_element)
			when "circle"
				output = Circle.new(actual_element)
			when "ellipse"
				output = Ellipse.new(actual_element)
			when "g"
				output = Container.new(actual_element)
			when "path"
				output = Path.new(actual_element)
			when "polygon"
				output = Polygon.new(actual_element)
			when "polyline"
				output = Polyline.new(actual_element)
			when "rect"
				output = Rect.new(actual_element)
			end

			if not output then
				return output
			end

			output.writer = writer

			output.render

			# recurse through children
			actual_element.children.each_index do |idx|
				child = actual_element.children[idx]

				writer.idx = idx

				writer.write "{"
				writer.indent += 1
				
				child_code = dump_svg_with_children child, writer
				output.add_child(child_code) if child_code

				writer.indent -= 1
				writer.write "}"
			end

			output
		end
	end

	class Writer
		attr_accessor :indent, :idx

		def initialize
			@idx = 0
			@indent = 0
		end

		def write thing
			puts "#{indent_str}#{thing}"
		end

		def indent_str
			"\t" * @indent
		end
	end
end
%w(base circle container ellipse path polygon polyline rect svg).each do |filename|
  require "svgeez/output/ios_shape_layer/#{filename}"
end
