#!/usr/bin/env python3
# coding: utf-8
#
# Copyright 2020 by Leipzig University Library, http://ub.uni-leipzig.de
#                   The Finc Authors, http://finc.info
#                   Robert Schenk, <robert.schenk@uni-leipzig.de>
#                   Martin Czygan, <martin.czygan@uni-leipzig.de>
#
# This file is part of some open source application.
#
# Some open source application is free software: you can redistribute
# it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version.
#
# Some open source application is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
#
# @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>

"""

Source: Nationallizenzen
SID: 17
Ticket: #15788, #15863, #15865, #18558
Origin: HTTP

"""


import os
import sys
from io import BytesIO

import marcx
import pymarc
from siskin.configuration import Config
from siskin.utils import marc_clean_record, xmlstream, convert_to_finc_id
from siskin.arguments import FincArgumentParser


adlr_ddc = ["175", "303.38", "342.0853", "384", "778.5", "791"]

adlr_ddc_start = ["002", "070", "302.2", "303.375", "303.376", "303.4833", "303.4834", "323.445",
                  "324.73", "343.099", "384.3", "384.54", "384.55", "384.8", "659", "741.5", "770",
                  "781.54", "791.4", "794.8"]


##################################################################################
# 1. Parse arguments and prepare outputfile
##################################################################################

SID = "17"

fip = FincArgumentParser()

# Get arguments
inputfile = fip.args.inputfile
outputformat = fip.args.outputformat

# Generates string for outputfilename, example: 196-output-20200701.fincmarc.mrc
outputfilename = fip.outputfilename(SID)

# Generates path for inputfile
path = fip.sid_path(SID)

# Removes n old inputfiles and outputfiles as specified in input-hist-size and output-hist-size
fip.remove_old_outputfiles(SID)
fip.remove_old_inputfiles(SID)

# Set output format for MARC record
if outputformat == "xml":
    outputfile = pymarc.XMLWriter(open(outputfilename, "wb"))
else:
    outputfile = open(outputfilename, "wb")


##################################################################################
# 2. Get input data
##################################################################################

if not inputfile:    
    config = Config.instance()
    try:
        inputfile = config.get(SID, "input")
    except:
        inputfile = ""

    if not inputfile:
        try:
            server = config.get(SID, "server")
        except:
            sys.exit("No server given in siskin.ini")
        inputfile = fip.inputfilename(SID)
        query = "collection_details:GBV_NL_EBOOK OR collection_details:ZDB-95-BKS"
        os.system("""solrdump -verbose -server %s -q "%s" -fl fullrecord | jq -r '.fullrecord' | replace '#29;' $(printf "\x1D") '#30;' $(printf "\x1E") '#31;' $(printf "\x1F") > %s/17_input.mrc""" % (server, query, path))
        os.system("sed ':a;N;$!ba;s/\\x1d\\x0a/\\x1d/g' %s/17_input.mrc > %s/17_input_cleaned.mrc" % (path, path))
        os.system("yaz-marcdump -i marc -o marcxml %s/17_input_cleaned.mrc > %s" % (path, inputfile))
        os.system("rm -f %s/17_input.mrc" % path)
        os.system("rm -f %s/17_input_cleaned.mrc" % path)


##################################################################################
# 3. Process data
##################################################################################

for oldrecord in xmlstream(inputfile, "record"):

    oldrecord = BytesIO(oldrecord)
    oldrecord = pymarc.marcxml.parse_xml_to_array(oldrecord)
    oldrecord = oldrecord[0]

    record = marcx.Record.from_record(oldrecord)
    record.force_utf8 = True
    record.strict = False

    # Identifier
    f001 = record["001"].data
    record.remove_fields("001")
    record.add("001", data="17-" + f001)

    # Collection and sealing
    try:
        f082a = record["082"]["a"]
    except:
        f082a = ""

    for ddc in adlr_ddc_start:
        if f082a.startswith(ddc):
            f082a_startswith_adlr_ddc = True
            break
    else:
        f082a_startswith_adlr_ddc = False

    f912a = record.get_fields("912")

    if len(f912a) == 2:
        f912a_1 = f912a[0].get_subfields("a")[0]
        f912a_2 = f912a[1].get_subfields("a")[0]
    else:
        f912a_1 = f912a[0].get_subfields("a")[0]
        f912a_2 = ""

    if f082a in adlr_ddc or f082a_startswith_adlr_ddc:
        collections = ["a", f001, "b", "17", "c", f912a_1, "c", f912a_2, "c", "sid-17-col-adlr"]
    else:
        collections = ["a", f001, "b", "17", "c", f912a_1, "c", f912a_2]

    record.remove_fields("980")
    record.add("980", subfields=collections)

    # Convert all identifier in 001, 770, 772 ... to Finc schema
    record = convert_to_finc_id(SID, record, encode=False, finc_prefix=False)

    # Remove empty subfields
    marc_clean_record(record)

    # Write record to file
    if outputformat == "xml":
        outputfile.write(record)
    else:
        outputfile.write(record.as_marc())

outputfile.close()
