require 'pathname'

module Twine
  module Formatters
    class OpenExplore < Abstract
      include Twine::Placeholders

      def format_name
        'open-explore'
      end

      def extension
        '.hm'
      end

      def self.can_handle_directory?(path)
        true
      end

      def default_file_name
        return 'R2KitString+TxtFileName.hm'
      end

      def determine_language_given_path(path)
        raise 'not going to implement'
      end

      def write_file(path, lang)
        default_lang = @strings.language_codes[0]
        filePath = Pathname.new(path)
        stringFileName = filePath.basename.to_s[0..-4] 
        header = "R2KitString+" + stringFileName + ".h"
        headerPath = filePath.dirname + header
        impl = "R2KitString+" + stringFileName + ".m"
        implPath = filePath.dirname + impl
        File.open(headerPath, 'w:UTF-8') do |f|
          f.puts "/**\n * Objective-C header File\n * Generated by Twine #{Twine::VERSION}\n */"
          f.write (<<S).strip

#import "R2KitString.h"

/**
  Generated class from twine (apple-objc formatter)
 */
@interface R2KitString (#{stringFileName})
S
          f.write "\n"
          @strings.sections.each do |section|
            printed_section = false
            section.rows.each do |row|
              if row.matches_tags?(@options[:tags], @options[:untagged])
                if !printed_section
                  f.puts ''
                  if section.name && section.name.length > 0
                    section_name = section.name
                    f.puts "#pragma mark - #{section_name}"
                    f.write "\n"
                  end
                  printed_section = true
                end

                key = row.key

                value = row.translated_string_for_lang(lang, default_lang)
                if !value && @options[:include_untranslated]
                  value = row.translated_string_for_lang(@strings.language_codes[0])
                end

                if value # if values is nil, there was no appropriate translation, so let Android handle the defaulting
                  value = String.new(value) # use a copy to prevent modifying the original

                  value.gsub!('"', '\\\\"')

                  comment = row.comment

                  f.puts "/**"
                  if comment && comment.length > 0
                    f.puts " #{comment}"
                  end
                  f.puts " Translation in #{lang} :"
                  f.puts " #{value}"
                  f.puts "*/"
                  f.puts "@property (nonatomic, readonly) NSString *#{key};"
                  f.write "\n"
                end
              end
            end
          end
          f.write (<<S).rstrip
@end
S
          f.write "\n"
        end
        File.open(implPath, 'w:UTF-8') do |f|
          f.puts "/**\n * Objective-C impl File\n * Generated by Twine #{Twine::VERSION}\n */"
          f.write (<<S).strip
#import "R2KitString+#{stringFileName}.h"

static NSString *kStringsFileName = @"#{stringFileName}";

@implementation R2KitString (#{stringFileName})
S
          f.write "\n"
          f.write "\n"
          @strings.sections.each do |section|
            printed_section = false
            section.rows.each do |row|
              if row.matches_tags?(@options[:tags], @options[:untagged])
                if !printed_section
                  if section.name && section.name.length > 0
                    section_name = section.name
                    f.puts "#pragma mark - #{section_name}"
                    f.write "\n"
                  end
                  printed_section = true
                end

                key = row.key

                value = row.translated_string_for_lang(lang, default_lang)
                if !value && @options[:include_untranslated]
                  value = row.translated_string_for_lang(@strings.language_codes[0])
                end

                if value # if values is nil, there was no appropriate translation, so let Android handle the defaulting
                  value = String.new(value) # use a copy to prevent modifying the original

                  value.gsub!('"', '\\\\"')
                  f.write (<<S).rstrip
- (NSString *)#{key}
{
    return NSLocalizedStringFromTableInBundle(@"#{key}", kStringsFileName, [NSBundle bundleWithIdentifier:@"openium.TerritoriumKit"], @"");
}
S
                  f.write "\n"
                  f.write "\n"
                end
              end
            end
          end
          f.write (<<S).rstrip
@end
S
          f.write "\n"
        end
      end
    end
  end
end

Twine::Formatters.formatters << Twine::Formatters::OpenExplore.new
