#!/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: Kieler Beiträge zur Filmmusikforschung
SID: 101
Ticket: #7967, #9371, #10831, #12048, #12967, #14649, #15882, #18416
Origin: local file

"""


import os
import sys
import re
import base64

import marcx
import xlrd
from siskin.mappings import formats
from siskin.configuration import Config
from siskin.utils import check_issn, marc_build_field_008
from siskin.arguments import FincArgumentParser


def clean_number(value):
    """
    Removes trailing '.0' in fields containing numbers.
    """
    value = str(value)
    return re.sub("\.0$", "", value)


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

SID = "101"

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:
    config = Config.instance()
    inputfile = config.get(SID, "input")

workbook = xlrd.open_workbook(inputfile)
sheet = workbook.sheet_by_name("Tabelle1")


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

for row in range(1, sheet.nrows):

    csvrecord = sheet.row_values(row)

    marcrecord = marcx.Record(force_utf8=True)
    marcrecord.strict = False

    # Set format for entire source
    format = "Article"

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

    # Identifier
    try:
        url = csvrecord[13]
    except:
        continue
    url = bytes(url, "utf-8")
    url = base64.b64encode(url)
    f001 = url.decode("utf-8").rstrip("=")
    marcrecord.add("001", data="finc-101-" + f001)

    # Access type
    marcrecord.add("007", data="cr")

   # Periodicity
    year = csvrecord[8]
    year = clean_number(year)
    periodicity = formats[format]["008"]
    language = "ger"
    f008 = marc_build_field_008(year, periodicity, language)
    marcrecord.add("008", data=f008)

    # ISSN
    issn = csvrecord[5]
    issn = str(issn)
    f022a = check_issn(issn)
    marcrecord.add("022", a=f022a)

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

    # First creator
    authors = csvrecord[0]
    if authors:
        authors = authors.split("; ")
        f100a = authors[0]
        marcrecord.add("100", a=f100a, _4="aut")

    # Main title
    title = csvrecord[1]
    if ": " in title:
        titles = title.split(": ")
        f245a = titles[0]
        f245b = ""
        for title in titles[1:]:
            f245b = f245b + title + " : "
    else:
        f245a = title
        f245b = ""
    f245a = f245a.strip()
    f245b = f245b.rstrip(" :")
    f245b = f245b.strip()
    marcrecord.add("245", a=f245a, b=f245b)

    # Imprint
    year = csvrecord[8]
    f260c = clean_number(year)
    subfields = ("a", "Kiel", "b", "Kieler Gesellschaft für Filmmusikforschung", "c", f260c)
    marcrecord.add("260", subfields=subfields)

    # Page count
    pages = str(csvrecord[12])
    extent = pages.split("-")
    if len(extent) == 2:
        start = int(extent[0])
        end = int(extent[1])
        f300a = str(end - start + 1) + " Seiten"
    else:
        f300a = ""
    marcrecord.add("300", a=f300a)

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

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

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

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

    # Parent work
    f773t = csvrecord[2]
    year = str(csvrecord[8]).rstrip(".0")
    pages = str(csvrecord[12])
    issue = str(csvrecord[6]).rstrip(".0")
    f773g = "(" + year + "), Heft: " + issue + ", S. " + pages
    marcrecord.add("773", t=f773t, g=f773g)

    # Link to fulltext
    f856u = csvrecord[13]
    marcrecord.add("856", q="text/pdf", _3="Link zur Ressource", u=f856u)

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

    # Collection and sealing
    collections = ["a", f001, "b", SID, "c", "sid-101-col-kielfilm"]
    marcrecord.add("980", subfields=collections)
    outputfile.write(marcrecord.as_marc())

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

outputfile.close()
