#!/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: GEOSCAN
SID: 129
Ticket: #9871
Origin: Cloud

"""


import os
import sys
import csv

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


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

SID = "129"

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 input 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")
csvrecords = csv.reader(inputfile, delimiter=",")


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

for csvrecord in csvrecords:

    try:
        csvrecord[0]
    except:
        continue

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

    # Format mapping
    format = csvrecord[7]
    doi = csvrecord[14]
    
    if format == "format":
        continue
    elif format == "Book":
        format = "Book"
    elif format == "Map":
        format = "Map"
    elif format == "Journal":
        format = "Journal"
    else:
        format = "Article"

    if doi:
        is_electronic = True
    else:
        is_electronic = False

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

    # Identifier
    f001 = csvrecord[0]
    f001 = f001.replace("129-finc-", "129-")
    marcrecord.add("001", data=f001)

    # Access type
    if is_electronic:
        f007 = formats[format]["e007"]
    else:
        f007 = formats[format]["p007"]
    marcrecord.add("007", data=f007)

    # Periodicity
    languages = csvrecord[8]
    languages = marc_get_languages(languages)
    year = csvrecord[3]
    periodicity = formats[format]["008"]
    f008 = marc_build_field_008(year, periodicity, languages)
    marcrecord.add("008", data=f008)

    # Coordinates
    f034a = csvrecord[13]
    marcrecord.add("034", a=f034a)

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

    # Creator
    authors = csvrecord[5]
    if ",  " in authors:
        authors = authors.split(",  ")
    else:
        authors = authors.split("; ")
    author = authors[0]
    if " " in author and not ", " in author:
        marcrecord.add("110", a=author, _4="aut")
    else:
        marcrecord.add("100", a=author, _4="aut")

    # Title
    f245 = csvrecord[2]
    if ":" in f245:
        f245a, f245b = f245.split(":", 1)
    else:
        f245a = f245
        f245b = ""
    f245a = f245a.strip()
    f245b = f245b.strip()
    marcrecord.add("245", a=f245a, b=f245b)

    # Edition
    f250a = csvrecord[19]
    marcrecord.add("250", a=f250a)

    # Imprint
    f260b = csvrecord[4]
    f260c = csvrecord[3]
    marcrecord.add("260", 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)

    # Series
    f490a = csvrecord[9]
    marcrecord.add("490", a=f490a)

    # Abstract
    f520a = csvrecord[15]
    marcrecord.add("520", a=f520a)

    # Subjects
    subjects = csvrecord[12]
    subjects = subjects.split("; ")
    for subject in subjects:
        subject = subject.strip()
        subject = subject.title()
        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 author in authors[1:]:
        if " " in author and not ", " in author:
            marcrecord.add("710", a=author, _4="aut")
        else:
            marcrecord.add("700", a=author, _4="aut")

    # Container
    f490a = csvrecord[6]
    marcrecord.add("773", g=f490a)

    # Link to resource
    urls = csvrecord[1]
    urls = urls.split(", ")
    for url in urls:
        if "doi.org" in url:
            f856u = url
            break
    else:
        f856u = urls[0]
    marcrecord.add("856", q="text/html", _3="Link zur Ressource", u=f856u)

    # Link to Project
    f8563 = csvrecord[10]
    f856u = csvrecord[11]
    if f8563 and f856u:
        marcrecord.add("856", q="text/html", _3=f8563, u=f856u)

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

    # Collection
    id = csvrecord[0]
    f980a = id.replace("129-finc-", "")
    marcrecord.add("980", a=f980a, b="129", c="sid-129-col-geoscan")

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

inputfile.close()
outputfile.close()
