# Path: dplace_app/models.py
# class Society(models.Model):
#     ext_id = models.CharField('External ID', db_index=True, unique=True, max_length=20)
#     xd_id = models.CharField(
#         'Cross ID', db_index=True, default=None, null=True, max_length=10
#     )
#     name = models.CharField('Name', db_index=True, max_length=200)
#     latitude = models.FloatField('Latitude', null=True)
#     longitude = models.FloatField('Longitude', null=True)
#     focal_year = models.CharField('Focal Year', null=True, blank=True, max_length=100)
#     alternate_names = models.TextField(default="")
#     original_name = models.CharField('ORIG_name', max_length=200, default=None, null=True)
#     original_latitude = models.FloatField('ORIG_latitude', null=True)
#     original_longitude = models.FloatField('ORIG_longitude', null=True)
#     
#     region = models.ForeignKey('GeographicRegion', null=True)
#     source = models.ForeignKey('Source', null=True, related_name="societies")
#     language = models.ForeignKey('Language', null=True, related_name="societies")
#     
#     hraf_link = models.CharField('HRAF', null=True, default=None, max_length=200)
#     chirila_link = models.CharField('CHIRILA', default=None, null=True, max_length=200)
#     relationships = models.ManyToManyField(
#         'self', through='SocietyRelation', symmetrical=False)
# 
#     @property
#     def related(self):
#         return list(self.relationships.all())
# 
#     @property
#     def location(self):
#         return dict(coordinates=[self.longitude, self.latitude])
#         
#     @property
#     def original_location(self):
#         return dict(coordinates=[self.original_longitude, self.original_latitude])
# 
#     def get_environmental_data(self):
#         """Returns environmental data for the given society"""
#         valueDict = defaultdict(list)
#         for value in self.value_set\
#                 .select_related('variable').order_by('variable__name').all():
#             if value.variable.type == 'environmental':
#                 categories = value.variable.index_categories.all()
#                 valueDict[str(categories[0])].append({
#                     'name': value.variable.name,
#                     'value': '{0}'.format(value),
#                     'units': value.variable.units,
#                     'comment': value.comment
#                 })
#         return valueDict
# 
#     def get_cultural_trait_data(self):
#         """Returns the data for the given society"""
#         valueDict = defaultdict(list)
#         for value in self.value_set\
#                 .select_related('code').\
#                 select_related('variable').order_by('variable__label').all():
#             if value.variable.type == 'cultural':
#                 categories = value.variable.index_categories.all()
#                 for c in categories:
#                     valueDict[str(c)].append({
#                         'id': value.id,
#                         'label': value.variable.label,
#                         'name': value.variable.name,
#                         'code': value.coded_value,
#                         'description': value.get_description(),
#                         'year': value.focal_year,
#                         'comment': value.comment,
#                         'sources': value.references.all(),
#                     })
#         return valueDict
# 
#     def get_data_references(self):
#         """Returns the references for the cultural trait data"""
#         refs = []
#         qset = self.value_set
#         for value in qset.all():
#             for r in value.references.all():
#                 if r not in refs:
#                     refs.append(r)
#         return sorted(refs, key=lambda r: r.author)
# 
#     def __unicode__(self):
#         return "%s - %s" % (self.ext_id, self.name)
#     
#     def get_absolute_url(self):
#         return reverse("view_society", args=[self.ext_id])
#     
#     class Meta(object):
#         verbose_name_plural = "Societies"
#         ordering = ('name', )
# 
# class LanguageTree(models.Model):
#     name = models.CharField(max_length=50, db_index=True)
#     newick_string = models.TextField(default='')
#     source = models.ForeignKey('Source', null=True)
#     taxa = models.ManyToManyField('LanguageTreeLabels')
# 
#     class Meta(object):
#         ordering = ('name', )
# 
# class LanguageTreeLabels(models.Model):
#     languageTree = models.ForeignKey('LanguageTree')
#     label = models.CharField(max_length=255, db_index=True)
#     language = models.ForeignKey('Language', null=True)
#     societies = models.ManyToManyField('Society', through="LanguageTreeLabelsSequence")
#     
#     class Meta:
#         ordering = ("-languagetreelabelssequence__fixed_order",)
# 
# class LanguageTreeLabelsSequence(models.Model):
#     society = models.ForeignKey('Society')
#     labels = models.ForeignKey('LanguageTreeLabels')
#     fixed_order = models.PositiveSmallIntegerField(db_index=True)
#     
#     class Meta:
#         ordering = ("-fixed_order",)


import re
from clldutils.misc import nfilter
from nexus import NexusReader
from ete3 import Tree
from dplace_app.models import (
    Society, LanguageTree, LanguageTreeLabels, LanguageTreeLabelsSequence,
)



def load_phylogenies(repos):
    sources, sequences = {}, []
    for obj in repos.phylogenies:
        _load(obj, sources, sequences)
    LanguageTreeLabelsSequence.objects.bulk_create(sequences)
    return len(repos.phylogenies)


def _load(obj, sources, sequences):
    # now add languages to the tree
    reader = NexusReader(obj.trees.as_posix())


# Path: dplace_app/models.py
# class Society(models.Model):
#     ext_id = models.CharField('External ID', db_index=True, unique=True, max_length=20)
#     xd_id = models.CharField(
#         'Cross ID', db_index=True, default=None, null=True, max_length=10
#     )
#     name = models.CharField('Name', db_index=True, max_length=200)
#     latitude = models.FloatField('Latitude', null=True)
#     longitude = models.FloatField('Longitude', null=True)
#     focal_year = models.CharField('Focal Year', null=True, blank=True, max_length=100)
#     alternate_names = models.TextField(default="")
#     original_name = models.CharField('ORIG_name', max_length=200, default=None, null=True)
#     original_latitude = models.FloatField('ORIG_latitude', null=True)
#     original_longitude = models.FloatField('ORIG_longitude', null=True)
#     
#     region = models.ForeignKey('GeographicRegion', null=True)
#     source = models.ForeignKey('Source', null=True, related_name="societies")
#     language = models.ForeignKey('Language', null=True, related_name="societies")
#     
#     hraf_link = models.CharField('HRAF', null=True, default=None, max_length=200)
#     chirila_link = models.CharField('CHIRILA', default=None, null=True, max_length=200)
#     relationships = models.ManyToManyField(
#         'self', through='SocietyRelation', symmetrical=False)
# 
#     @property
#     def related(self):
#         return list(self.relationships.all())
# 
#     @property
#     def location(self):
#         return dict(coordinates=[self.longitude, self.latitude])
#         
#     @property
#     def original_location(self):
#         return dict(coordinates=[self.original_longitude, self.original_latitude])
# 
#     def get_environmental_data(self):
#         """Returns environmental data for the given society"""
#         valueDict = defaultdict(list)
#         for value in self.value_set\
#                 .select_related('variable').order_by('variable__name').all():
#             if value.variable.type == 'environmental':
#                 categories = value.variable.index_categories.all()
#                 valueDict[str(categories[0])].append({
#                     'name': value.variable.name,
#                     'value': '{0}'.format(value),
#                     'units': value.variable.units,
#                     'comment': value.comment
#                 })
#         return valueDict
# 
#     def get_cultural_trait_data(self):
#         """Returns the data for the given society"""
#         valueDict = defaultdict(list)
#         for value in self.value_set\
#                 .select_related('code').\
#                 select_related('variable').order_by('variable__label').all():
#             if value.variable.type == 'cultural':
#                 categories = value.variable.index_categories.all()
#                 for c in categories:
#                     valueDict[str(c)].append({
#                         'id': value.id,
#                         'label': value.variable.label,
#                         'name': value.variable.name,
#                         'code': value.coded_value,
#                         'description': value.get_description(),
#                         'year': value.focal_year,
#                         'comment': value.comment,
#                         'sources': value.references.all(),
#                     })
#         return valueDict
# 
#     def get_data_references(self):
#         """Returns the references for the cultural trait data"""
#         refs = []
#         qset = self.value_set
#         for value in qset.all():
#             for r in value.references.all():
#                 if r not in refs:
#                     refs.append(r)
#         return sorted(refs, key=lambda r: r.author)
# 
#     def __unicode__(self):
#         return "%s - %s" % (self.ext_id, self.name)
#     
#     def get_absolute_url(self):
#         return reverse("view_society", args=[self.ext_id])
#     
#     class Meta(object):
#         verbose_name_plural = "Societies"
#         ordering = ('name', )
# 
# class LanguageTree(models.Model):
#     name = models.CharField(max_length=50, db_index=True)
#     newick_string = models.TextField(default='')
#     source = models.ForeignKey('Source', null=True)
#     taxa = models.ManyToManyField('LanguageTreeLabels')
# 
#     class Meta(object):
#         ordering = ('name', )
# 
# class LanguageTreeLabels(models.Model):
#     languageTree = models.ForeignKey('LanguageTree')
#     label = models.CharField(max_length=255, db_index=True)
#     language = models.ForeignKey('Language', null=True)
#     societies = models.ManyToManyField('Society', through="LanguageTreeLabelsSequence")
#     
#     class Meta:
#         ordering = ("-languagetreelabelssequence__fixed_order",)
# 
# class LanguageTreeLabelsSequence(models.Model):
#     society = models.ForeignKey('Society')
#     labels = models.ForeignKey('LanguageTreeLabels')
#     fixed_order = models.PositiveSmallIntegerField(db_index=True)
#     
#     class Meta:
#         ordering = ("-fixed_order",)


# Path: dplace_app/loader/phylogenies.py
import re
from clldutils.misc import nfilter
from nexus import NexusReader
from ete3 import Tree
from dplace_app.models import (
    Society, LanguageTree, LanguageTreeLabels, LanguageTreeLabelsSequence,
)




def load_phylogenies(repos):
    sources, sequences = {}, []
    for obj in repos.phylogenies:
        _load(obj, sources, sequences)
    LanguageTreeLabelsSequence.objects.bulk_create(sequences)
    return len(repos.phylogenies)


def _load(obj, sources, sequences):
    # now add languages to the tree
    reader = NexusReader(obj.trees.as_posix())


    tree = LanguageTree.objects.create(name=obj.id)