# -*- cperl -*-
use strict;
use warnings;
use Test::More tests => 2;
use Test::Differences;
unified_diff;
use Text::Diff::Config;
$Text::Diff::Config::Output_Unicode = 1;

use Encode;
use Biber;
use Biber::Utils;
use Biber::Output::biblatexml;
use Log::Log4perl;
use Unicode::Normalize;
chdir("t/tdata");
no warnings 'utf8';
use utf8;

# Set up Biber object
my $biber = Biber->new( configfile => 'tool-testsort.conf');
my $LEVEL = 'ERROR';
my $l4pconf = qq|
    log4perl.category.main                             = $LEVEL, Screen
    log4perl.category.screen                           = $LEVEL, Screen
    log4perl.appender.Screen                           = Log::Log4perl::Appender::Screen
    log4perl.appender.Screen.utf8                      = 1
    log4perl.appender.Screen.Threshold                 = $LEVEL
    log4perl.appender.Screen.stderr                    = 0
    log4perl.appender.Screen.layout                    = Log::Log4perl::Layout::SimpleLayout
|;
Log::Log4perl->init(\$l4pconf);

my $outvar;

$biber->set_output_obj(Biber::Output::biblatexml->new());
# Get reference to output object
my $out = $biber->get_output_obj;

# Options - we could set these in the control file but it's nice to see what we're
# relying on here for tests

# Biber options
Biber::Config->setoption('tool', 1);
Biber::Config->setoption('output_resolve_xdata', 1);
Biber::Config->setoption('output_resolve_crossrefs', 1);
Biber::Config->setoption('output_format', 'biblatexml');
Biber::Config->setoption('sortlocale', 'en_GB.UTF-8');
Biber::Config->setoption('dsn', 'tool.bib');

# Set the output target
$out->set_output_target($out->set_output_target_file(\$outvar, 1));

# THERE IS A CONFIG FILE BEING READ!

# Now generate the information
$ARGV[0] = 'tool.bib'; # fake this as we are not running through top-level biber program
$biber->tool_mode_setup;
$biber->prepare_tool;
$out->output;

my $main = $biber->datalists->get_lists_by_attrs(section                    => 99999,
                                       name                       => 'tool/global//global/global',
                                       type                       => 'entry',
                                       sortingtemplatename             => 'tool',
                                       sortingnamekeytemplatename      => 'global',
                                       labelprefix                => '',
                                       uniquenametemplatename     => 'global',
                                       labelalphanametemplatename => 'global')->[0];

my $bltxml1 = q|<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="tool.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
<!-- Auto-generated by Biber::Output::biblatexml -->

<bltx:entries xmlns:bltx="http://biblatex-biber.sourceforge.net/biblatexml">
  <bltx:entry id="i3Š" entrytype="unpublished">
    <bltx:options>useprefix=false</bltx:options>
    <bltx:names type="author">
      <bltx:name>
        <bltx:namepart type="family" initial="A">AAA</bltx:namepart>
      </bltx:name>
      <bltx:name>
        <bltx:namepart type="family" initial="B">BBB</bltx:namepart>
      </bltx:name>
      <bltx:name>
        <bltx:namepart type="family" initial="C">CCC</bltx:namepart>
      </bltx:name>
      <bltx:name>
        <bltx:namepart type="family" initial="D">DDD</bltx:namepart>
      </bltx:name>
      <bltx:name>
        <bltx:namepart type="family" initial="E">EEE</bltx:namepart>
      </bltx:name>
    </bltx:names>
    <bltx:institution>
      <bltx:list>
        <bltx:item>REPlaCEDte</bltx:item>
        <bltx:item>early</bltx:item>
      </bltx:list>
    </bltx:institution>
    <bltx:lista>
      <bltx:list>
        <bltx:item>list test</bltx:item>
      </bltx:list>
    </bltx:lista>
    <bltx:listb>
      <bltx:list>
        <bltx:item>late</bltx:item>
        <bltx:item>early</bltx:item>
      </bltx:list>
    </bltx:listb>
    <bltx:location>
      <bltx:list>
        <bltx:item>one</bltx:item>
        <bltx:item>two</bltx:item>
      </bltx:list>
    </bltx:location>
    <bltx:abstract>Some abstract %50 of which is useless</bltx:abstract>
    <bltx:note>i3Š</bltx:note>
    <bltx:title>Š title</bltx:title>
    <bltx:userb>test</bltx:userb>
    <bltx:date>2003</bltx:date>
  </bltx:entry>
  <bltx:entry id="xd1" entrytype="book">
    <bltx:names type="author">
      <bltx:name>
        <bltx:namepart type="family" initial="E">Ellington</bltx:namepart>
        <bltx:namepart type="given">
          <bltx:namepart initial="E">Edward</bltx:namepart>
          <bltx:namepart initial="P">Paul</bltx:namepart>
        </bltx:namepart>
      </bltx:name>
    </bltx:names>
    <bltx:location>
      <bltx:list>
        <bltx:item>New York</bltx:item>
        <bltx:item>London</bltx:item>
      </bltx:list>
    </bltx:location>
    <bltx:publisher>
      <bltx:list>
        <bltx:item>Macmillan</bltx:item>
      </bltx:list>
    </bltx:publisher>
    <bltx:note>A Note</bltx:note>
    <bltx:date>2001</bltx:date>
  </bltx:entry>
  <bltx:entry id="macmillan" entrytype="xdata">
    <bltx:ids>
      <bltx:key>macmillanalias</bltx:key>
    </bltx:ids>
    <bltx:location>
      <bltx:list>
        <bltx:item>New York</bltx:item>
        <bltx:item>London</bltx:item>
      </bltx:list>
    </bltx:location>
    <bltx:publisher>
      <bltx:list>
        <bltx:item>Macmillan</bltx:item>
      </bltx:list>
    </bltx:publisher>
    <bltx:note>A Note</bltx:note>
    <bltx:date>2001</bltx:date>
  </bltx:entry>
  <bltx:entry id="macmillan:pub" entrytype="xdata">
    <bltx:ids>
      <bltx:key>macmillan:pubALIAS</bltx:key>
    </bltx:ids>
    <bltx:publisher>
      <bltx:list>
        <bltx:item>Macmillan</bltx:item>
      </bltx:list>
    </bltx:publisher>
  </bltx:entry>
  <bltx:entry id="macmillan:loc" entrytype="xdata">
    <bltx:location>
      <bltx:list>
        <bltx:item>New York</bltx:item>
        <bltx:item>London</bltx:item>
      </bltx:list>
    </bltx:location>
    <bltx:note>A Note</bltx:note>
  </bltx:entry>
  <bltx:entry id="b1" entrytype="book">
    <bltx:location>
      <bltx:list>
        <bltx:item annotation="ann1">London</bltx:item>
        <bltx:item annotation="ann2">Edinburgh</bltx:item>
      </bltx:list>
    </bltx:location>
    <bltx:mainsubtitle>Mainsubtitle</bltx:mainsubtitle>
    <bltx:maintitle>Maintitle</bltx:maintitle>
    <bltx:maintitleaddon>Maintitleaddon</bltx:maintitleaddon>
    <bltx:title annotation="ann1, ann2">Booktitle</bltx:title>
    <bltx:date>1999</bltx:date>
  </bltx:entry>
  <bltx:entry id="mv1" entrytype="mvbook">
    <bltx:ids>
      <bltx:key>mvalias</bltx:key>
    </bltx:ids>
    <bltx:subtitle>Mainsubtitle</bltx:subtitle>
    <bltx:title>Maintitle</bltx:title>
    <bltx:titleaddon>Maintitleaddon</bltx:titleaddon>
  </bltx:entry>
  <bltx:entry id="dt1" entrytype="book">
    <bltx:date>
      <bltx:start>2004-04-25T14:34:00</bltx:start>
      <bltx:end>2004-04-05T14:37:06</bltx:end>
    </bltx:date>
    <bltx:date type="event">
      <bltx:start>2004-04-25T14:34:00+05:00</bltx:start>
      <bltx:end>2004-04-05T15:34:00+05:00</bltx:end>
    </bltx:date>
    <bltx:date type="orig">
      <bltx:start>2004-04-25T14:34:00Z</bltx:start>
      <bltx:end>2004-04-05T14:34:05Z</bltx:end>
    </bltx:date>
    <bltx:date type="url">
      <bltx:start>2004-04-25T14:34:00</bltx:start>
      <bltx:end>2004-04-05T15:00:00</bltx:end>
    </bltx:date>
  </bltx:entry>
  <bltx:entry id="m1" entrytype="article">
    <bltx:date>2017</bltx:date>
  </bltx:entry>
</bltx:entries>
|;

eq_or_diff($outvar, encode_utf8($bltxml1), 'bltxml tool mode - 1');
is_deeply($main->get_keys, ['b1', 'macmillan', 'dt1', 'm1', 'macmillan:pub', 'macmillan:loc', 'mv1', NFD('i3Š'), 'xd1'], 'tool mode sorting');
