#!/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: DABI
SID: 185
Ticket: #15924
Origin: local file

"""


import json
import marcx
import pymarc

from siskin.mappings import formats
from siskin.configuration import Config
from siskin.arguments import FincArgumentParser
from siskin.utils import (check_isbn, check_issn, marc_build_field_008, marc_build_field_773g)


def get_field(jsonrecord, field):
    """
    Returns a field or an empty string.
    """
    value = jsonrecord[field]
    if value is None or value == "NULL":
        value = ""
    return str(value)


dabi_to_ddc = {
                "00": "020",
                "1.8": "020",
                "1.8.1": "020",
                "1.8.2": "020",
                "1.8.3": "020",
                "6": "020",
                "6.1": "020",
                "6.2": "020",
                "6.3": "020",
                "1": "021",
                "1.1": "021",
                "1.2": "021",
                "1.2.1": "021",
                "1.2.2": "021",
                "1.2.2.1": "021",
                "1.2.2.2": "021",
                "1.2.2.3": "021",
                "1.2.3": "021",
                "1.2.4": "021",
                "1.7": "021",
                "1.7.1": "021",
                "1.7.2": "021",
                "1.7.3": "021",
                "1.7.4": "021",
                "1.7.5": "021",
                "1.7.6": "021",
                "1.7.7": "021",
                "1.7.8": "021",
                "3": "021",
                "3.1": "021",
                "3.2.": "021",
                "3.2.1": "021",
                "3.2.2": "021",
                "3.2.3": "021",
                "3.2.4": "021",
                "3.3": "021",
                "3.4": "021",
                "3.5": "021",
                "3.5.1": "021",
                "3.5.2": "021",
                "3.5.3": "021",
                "3.5.4": "021",
                "3.5.5": "021",
                "3.5.6": "021",
                "3.6": "021",
                "5": "022",
                "5.1": "022",
                "5.2": "022",
                "7": "023",
                "7.1": "023",
                "7.2": "023",
                "7.3": "023",
                "7.4": "023",
                "7.5": "023",
                "7.6": "023",
                "7.7": "023",
                "7.8": "023",
                "4": "025",
                "4.1": "025",
                "4.2": "025",
                "4.3": "025",
                "4.3.1": "025",
                "4.3.2": "025",
                "4.4": "025",
                "4.4.": "025",
                "4.4.1": "025",
                "4.4.2": "025",
                "4.4.4": "025",
                "4.4.5": "025",
                "4.4.6": "025",
                "4.4.7": "025",
                "4.4.10": "025",
                "4.4.12": "025",
                "4.4.13": "025",
                "4.4.20": "025",
                "4.4.22": "025",
                "4.4.30": "025",
                "4.4.34": "025",
                "4.4.50": "025",
                "4.5": "025",
                "4.5.1": "025",
                "4.5.2": "025",
                "4.6": "025",
                "4.7": "025",
                "4.8": "025",
                "4.9": "025",
                "4.9.1": "025",
                "4.9.2": "025",
                "4.10": "025",
                "4.10.1": "025",
                "4.10.2": "025",
                "4.10.3": "025",
                "4.11": "025",
                "2": "026",
                "2.1": "026",
                "2.1.1": "026",
                "2.1.2": "026",
                "2.1.3": "026",
                "2.10": "026",
                "2.2": "026",
                "2.3": "026",
                "2.3.1": "026",
                "2.3.2": "026",
                "2.3.3": "026",
                "2.4": "026",
                "2.4.1": "026",
                "2.4.2": "026",
                "2.4.3": "026",
                "2.4.4": "026",
                "2.5": "026",
                "2.5.1": "026",
                "2.5.2": "026",
                "2.6": "026",
                "2.6.1": "026",
                "2.6.2": "026",
                "2.7": "026",
                "2.7.1": "026",
                "2.7.2": "026",
                "2.7.3": "026",
                "2.7.4": "026",
                "2.7.5": "026",
                "2.7.6": "026",
                "2.7.7": "026",
                "2.7.8": "026",
                "2.7.9": "026",
                "2.8": "026",
                "2.9": "026",
                "1.4": "306.488",
                "1.6": "352.744",
                "1.6.1": "352.744",
                "1.6.2": "352.744",
                "1.6.3": "352.744",
                "1.6.4": "352.744",
                "1.8.2": "353.73 ",
                "1.3": "381.45002",
                "1.5": "809.9339"
}


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

SID = "185"

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)

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

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


##################################################################################
# 2. Acquire input data
##################################################################################

if not inputfile:
    config = Config.instance()
    inputfile = config.get("dabi", "input")

inputfile = open(inputfile, "r")
jsonrecords = inputfile.readlines()


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

for jsonrecord in jsonrecords:

    jsonrecord = json.loads(jsonrecord)
    marcrecord = marcx.Record(force_utf8=True)
    marcrecord.strict = False

    # Format for entire source
    format = "Article"

    # Leader
    leader = formats[format]["Leader"]
    marcrecord.leader = leader

    # Identifier
    f001 = get_field(jsonrecord, "artikel_id")
    if not f001:
        continue
    marcrecord.add("001", data="185-" + f001)

    # Access type (online or physical)
    url = get_field(jsonrecord, "url")
    if url:
        f007 = formats[format]["e007"]
    else:
        f007 = formats[format]["p007"]
    marcrecord.add("007", data=f007)

    # Periodicity
    year = get_field(jsonrecord, "jahr")
    periodicity = formats[format]["008"]
    language = "ger"
    f008 = marc_build_field_008(year, periodicity, language)
    marcrecord.add("008", data=f008)

    # ISSN
    identifier = get_field(jsonrecord, "issn")
    f022a = check_issn(identifier)
    marcrecord.add("022", a=f022a)

    # Language
    language = "ger"
    marcrecord.add("041", a=language)

    # DDC class
    dabi = get_field(jsonrecord, "notationen")
    dabi = dabi.split(";")[0]
    dabi = dabi.replace("a", ".")
    f082 = dabi_to_ddc.get(dabi, "")
    marcrecord.add("082", a=f082)

    # First creator
    authors = get_field(jsonrecord, "autoren")
    authors = authors.split("; ")
    f100a = authors[0]
    f100a = f100a.strip()
    marcrecord.add("100", a=f100a, _4="aut")

    # Title statement
    f245a = get_field(jsonrecord, "titel")
    f245b = get_field(jsonrecord, "untertitel")
    marcrecord.add("245", a=f245a, b=f245b)

    # Imprint
    f260a = get_field(jsonrecord, "ort")
    f260b = get_field(jsonrecord, "verlag")
    f260c = get_field(jsonrecord, "jahr")
    marcrecord.add("260", a=f260a, b=f260b, c=f260c)

    # RDA-content
    f336b = formats[format]["336b"]
    marcrecord.add("336", b=f336b)

    # RDA-carrier
    f338b = formats[format]["338b"]
    marcrecord.add("338", b=f338b)

    # Footnote
    f500a = get_field(jsonrecord, "bemerkungen")
    marcrecord.add("500", a=f500a)
    f500a = get_field(jsonrecord, "oa_status")
    marcrecord.add("500", a=f500a)

    # Abstract
    f520a = get_field(jsonrecord, "abstract")
    marcrecord.add("520", a=f520a)

    # Subject headings
    subjects = get_field(jsonrecord, "schlagworte")
    subjects = subjects.split(";")
    for subject in subjects:
        subject = subject.strip()
        marcrecord.add("650", a=subject)

    # GND-content and -carrier
    f655a = formats[format]["655a"]
    f6552 = formats[format]["6552"]
    marcrecord.add("655", a=f655a, _2=f6552)

    # Additional creators
    if authors:
        for f700a in authors[1:]:
            f700a = f700a.strip()
            marcrecord.add("700", a=f700a, _4="aut")

    # Editors
    editors = get_field(jsonrecord, "herausgeber")
    if "Schelle-Wolff" in editors:
        editors = editors.split(", ")
    else:
        editors = editors.split("; ")
    for f700a in editors:
        if f700a:
            f700a = f700a.strip()
            marcrecord.add("700", a=f700a, _4="edt")

    # Parent work (journal)
    f773t = get_field(jsonrecord, "zs_titel")
    volume = get_field(jsonrecord, "band")
    issue = get_field(jsonrecord, "heft")
    year = get_field(jsonrecord, "jahr")
    spage = get_field(jsonrecord, "anfangsseite")
    epage = get_field(jsonrecord, "endseite")
    f773g = marc_build_field_773g(volume, year, issue, spage, epage)
    marcrecord.add("773", t=f773t, g=f773g)

    # Link to fulltext
    url = get_field(jsonrecord, "url")
    if url:
        marcrecord.add("856", q="text/html", _3="Link zur Ressource", u=url)

    # Link to journal
    url = get_field(jsonrecord, "zs_link")
    if url:
        marcrecord.add("856", q="text/html", _3="Link zur Zeitschrift", u=url)

    # SWB-content
    f935c = formats[format]["935c"]
    marcrecord.add("935", c=f935c)

    # Collection and sealing
    collections = ["a", f001, "b", "185", "c", "sid-185-col-dabi"]
    marcrecord.add("980", subfields=collections)

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

inputfile.close()
outputfile.close()
