import providers.io.osf.normalizer as OSFParser
from share.normalize.normalizer import Normalizer

from share.normalize.parsers import Parser
from share.normalize import Delegate, RunPython, Map, ctx, Maybe, ParseDate


class Person(Parser):
    given_name = ctx.embeds.users.data.attributes.given_name
    family_name = ctx.embeds.users.data.attributes.family_name
    additional_name = ctx.embeds.users.data.attributes.middle_names
    suffix = ctx.embeds.users.data.attributes.suffix
    url = ctx.embeds.users.data.links.html

    class Extra:
        nodes = ctx.embeds.users.data.relationships.nodes.links.related.href
        locale = ctx.embeds.users.data.attributes.locale
        date_registered = ctx.embeds.users.data.attributes.date_registered
        active = ctx.embeds.users.data.attributes.active
        timezone = ctx.embeds.users.data.attributes.timezone
        profile_image = ctx.embeds.users.data.links.profile_image


class Contributor(Parser):
    person = Delegate(Person, ctx)
    order_cited = ctx('index')
    cited_name = ctx.embeds.users.data.attributes.full_name


class Tag(Parser):
    name = ctx


class ThroughTags(Parser):
    tag = Delegate(Tag, ctx)


class Institution(Parser):
    name = ctx.attributes.name
    url = ctx.links.self

    class Extra:
        nodes = ctx.relationships.nodes.links.related.href
        users = ctx.relationships.users.links.related.href
        registrations = ctx.relationships.registrations.links.related.href
        description = ctx.attributes.description


class Publisher(Parser):
    name = ctx


class Association(Parser):
    pass


class Link(Parser):
    url = ctx
    type = RunPython('get_link_type', ctx)

    def get_link_type(self, link):
        if 'dx.doi.org' in link:
            return 'doi'
        else:
            return 'provider'


class ThroughLinks(Parser):
    link = Delegate(Link, ctx)


class Preprint(Parser):
    schema = 'Preprint'
    title = ctx.attributes.title
    description = ctx.attributes.abstract
    contributors = Map(Delegate(Contributor), ctx['contributors'])
    date_created = ParseDate(ctx.attributes.date_created)
    subject = Delegate(Tag, ctx.attributes.subjects)
    links = Map(
        Delegate(ThroughLinks),
        ctx.links.self,
        ctx.links.html,
        ctx.links.doi
    )
    tags = Map(Delegate(ThroughTags), ctx.attributes.tags)
    rights = Maybe(ctx, 'attributes.node_license')

    class Extra:
        files = ctx.relationships.files.links.related.href
        subjects = Delegate(Tag, ctx.attributes.subjects)
        date_modified = ctx.attributes.date_modified
        type_soc = ctx.type
        id_soc = ctx.id
        doi_plain = ctx.attributes.doi


class PreprintNormalizer(Normalizer):
    root_parser = OSFParser.Project

    def do_normalize(self, data):
        unwrapped = self.unwrap_data(data)

        return Preprint(unwrapped).parse()

@@ -1,52 +1,4 @@
-import providers.io.osf.normalizer as OSFParser%0A
 from
@@ -146,21 +146,19 @@
 p, ctx, 
-Maybe
+Try
 , ParseD
@@ -1866,32 +1866,8 @@
 r):%0A
-    schema = 'Preprint'%0A
     
@@ -1905,32 +1905,36 @@
   description = 
+Try(
 ctx.attributes.a
@@ -1940,16 +1940,17 @@
 abstract
+)
 %0A    con
@@ -2067,16 +2067,60 @@
 reated)%0A
+    # TODO: update subject once implemented%0A
     subj
@@ -2261,32 +2261,36 @@
 s.html,%0A        
+Try(
 ctx.links.doi%0A  
@@ -2286,16 +2286,17 @@
 inks.doi
+)
 %0A    )%0A 
@@ -2332,16 +2332,20 @@
 hTags), 
+Try(
 ctx.attr
@@ -2356,16 +2356,17 @@
 es.tags)
+)
 %0A    rig
@@ -2375,20 +2375,16 @@
 s = 
-Maybe
+Try
 (ctx
-, '
+.
 attr
@@ -2402,17 +2402,16 @@
 _license
-'
 )%0A%0A    c
@@ -2726,44 +2726,8 @@
 er):
-%0A    root_parser = OSFParser.Project
 %0A%0A  

Update the preprint normalizer