#!/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>
#
# 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: Literaturdatenbank Montangeschichte
SID: 199
Ticket: #18552
Origin: Cloud

"""


import os
import sys
import csv
import re

import marcx
import pymarc
from siskin.configuration import Config
from siskin.mappings import formats
from siskin.arguments import FincArgumentParser
from siskin.utils import check_isbn, marc_build_field_008


persons_non_grata = ["NN.", "Anonym", "(Hrsg.)", "TK 25", "Autorenkollektiv", "S.B.", "(Bearb.)"]


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

SID = "199"

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 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:
    inputfile = fip.inputfilename(SID)
    config = Config.instance()
    user = config.get(SID, "user")
    password = config.get(SID, "password")
    url = config.get(SID, "url")
    os.system('wget -O %s --user=%s --password=%s "%s" --no-check-certificate' % (inputfile, user, password, url))

inputfile = open(inputfile, "r", encoding="utf-8")
records = csv.reader(inputfile, delimiter=",")


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

for fields in records:

    # id = 0
    # author = 1
    # title = 2
    # series = 3
    # isbn = 4
    # topic = 5
    # description = 6

    # Ignore first line
    if fields[0] == "id":
        continue

    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 = fields[0]
    marcrecord.add("001", data="199-" + f001)

    # Access type (online or physical)
    f007 = formats[format]["p007"]
    marcrecord.add("007", data=f007)

    # Periodicity
    language = "und"
    series = fields[3]
    match1 = re.search("\s(\d\d\d\d), S.", series)
    match2 = re.search(":\s(\d\d\d\d)", series)
    match3 = re.search("\s(\d\d\d\d)", series)
    if match1:
        year = match1.group(1)
    elif match2:
        year = match2.group(1)
    elif match3:
        year = match3.group(1)
    else:
        year = ""
    if year:
        y = int(year)
        if y > 2025:
            year = ""
    periodicity = formats[format]["008"]
    f008 = marc_build_field_008(year, periodicity, language)
    marcrecord.add("008", data=f008)

    # ISBN
    isbn = fields[4]
    f020a = check_isbn(isbn)
    marcrecord.add("020", a=f020a)

    # First creator
    authors = fields[1]
    authors = authors.split("; ")
    f100a = authors[0]
    f100a = f100a.rstrip(":")
    if f100a not in persons_non_grata:
        marcrecord.add("100", a=f100a)

    # Title
    f245a = fields[2]
    marcrecord.add("245", a=f245a)

    # Imprint
    f260c = year
    marcrecord.add("260", c=f260c)

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

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

    # Description
    f520a = fields[6]
    marcrecord.add("520", a=f520a)

    # Subject headings
    subjects = fields[5]
    if subjects:
        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
    for f700a in authors[1:]:
        f700a = f700a.rstrip(":")
        if f700a not in persons_non_grata:
            marcrecord.add("700", a=f700a)

    # Parent work
    f773g = fields[3]
    f773g = f773g.rstrip(",")
    marcrecord.add("773", g=f773g)

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

    # Collection and sealing
    marcrecord.add("980", a=f001, b=SID, c="sid-199-col-litmont")

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

inputfile.close()
outputfile.close()
