package PO4Moz::POfile;

use strict;
use PO4Moz::Util;
use POSIX qw(strftime);

sub parse {
  my $file = shift;
  my $contents = shift;
  my $callback = shift;
  
  # Remove trailing whitespace
  $contents =~ s/(\s|\n)*$//s;

  my %entries = ("index" => []);
  my $curentry = {};
  while ($contents ne "") {
    # Remove leading whitespace
    $contents =~ s/^(\s|\n)*//s;

    $curentry->{"file"} = "";
    $curentry->{"comments"} = "";

    # Find comments
    while ($contents =~ s/^#[.:]?(.*?)(\n|$)(?:\s|\n)*//s) {
      $curentry->{"comments"} .= "$1\n";
    }

    if ($contents ne "") {
      # Find msgid
      if ($contents =~ s/^msgid(?:\s|n)+((?:(["'])(?:\\\2|.)*?\2(?:\s|\n)*)+)//s) {
        # fix editors' quote b0rkage with this: "
        my $text = PO4Moz::Util::parse_quoted_string($1);
        if ($text =~ /^(.*?):(.*?):\n(.*)$/s) {
          $curentry->{"file"} = $1;
          $curentry->{"key"} = $2;
          $curentry->{"text"} = $3;
        }
        elsif ($text eq "") {
          $curentry->{"text"} = "";
        }
        else {
          print STDERR "Not from Mozilla in file $file: $text\n";
          exit 1;
        }
      }
      else {
        $contents =~ /^(.*?)(\n|$)/s;
        print STDERR "Parse error in file $file at $1\n";
        exit 1;
      }

      # Find msgstr
      if ($contents =~ s/^msgstr(?:\s|n)+((?:(["'])(?:\\\2|.)*?\2(?:\s|\n)*)+)//s) {
        # fix editors' quote b0rkage with this: "
        my $text = PO4Moz::Util::parse_quoted_string($1);
        $curentry->{"translation"} = $text;
      }
      else {
        $contents =~ /^(.*?)(\n|$)/s;
        print STDERR "Parse error in file $file at $1\n";
        exit 1;
      }

      if ($curentry->{"text"} ne "" && $curentry->{comments} !~ /^(,\s*\S*)*, fuzzy/m) {
        my $key = $curentry->{"file"}.":".$curentry->{"key"};
        $entries{$key} = $curentry;
        push @{$entries{"index"}}, $key;
        
        if ($callback) {
          &$callback($key, $curentry);
        }
      }
      $curentry = {};
    }
  }

  return %entries;
}

sub generate {
  my $file = shift;
  my %entries = @_;
  my $out = "";

  my $potcreat = strftime("%Y-%m-%d %H:%M%z", localtime);
  $out .= "# SOME DESCRIPTIVE TITLE.\n";
  $out .= "# This file is put in the public domain.\n";
  $out .= "# FIRST AUTHOR <EMAIL\@ADDRESS>, YEAR.\n";
  $out .= "#\n";
  $out .= "#, fuzzy\n";
  $out .= "msgid \"\"\n";
  $out .= "msgstr \"\"\n";
  $out .= "\"Project-Id-Version: PACKAGE VERSION\\n\"\n";
  $out .= "\"Report-Msgid-Bugs-To: \\n\"\n";
  $out .= "\"POT-Creation-Date: $potcreat\\n\"\n";
  $out .= "\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n";
  $out .= "\"Last-Translator: FULL NAME <EMAIL\@ADDRESS>\\n\"\n";
  $out .= "\"Language-Team: LANGUAGE <LL\@li.org>\\n\"\n";
  $out .= "\"MIME-Version: 1.0\\n\"\n";
  $out .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
  $out .= "\"Content-Transfer-Encoding: 8bit\\n\"\n\n";
  
  foreach my $entrykey (@{$entries{"index"}}) {
    my $entry = $entries{$entrykey};
    if ($file eq "" || $file eq $entry->{"file"}) {
      if ($entry->{"comments"}) {
        foreach my $comment (split "\n", $entry->{"comments"}) {
          $out .= "#.$comment\n";
        }
      }
      $out .= "msgid ".PO4Moz::Util::po_quote_string("$entrykey:\n".$entry->{"text"})."\n";
      $entry->{"translation"} = "" if (!defined $entry->{"translation"});
      $out .= "msgstr ".PO4Moz::Util::po_quote_string($entry->{"translation"})."\n\n";
    }
  }
  
  return $out;
}

1;
