from __future__ import division, unicode_literals
from mpcontribs.rest.rester import MPContribsRester
from mpcontribs.io.core.utils import get_short_object_id
from mpcontribs.io.archieml.mpfile import MPFile
from pandas import DataFrame

class MnO2PhaseSelectionRester(MPContribsRester):
    """MnO2_phase_selection-specific convenience functions to interact with MPContribs REST interface"""

    def get_contributions(self, phase=None):
        data = []
        phase_query_key = {'$exists': 1} if phase is None else phase
        columns = ['mp-id', 'contribution', 'formula']
        if phase is None:
            columns.append('phase')
        columns += ['dH (formation)', 'dH (hydration)', 'GS?', 'CIF']

        for doc in self.query_contributions(
            criteria={
                'project': {'$in': ['LBNL', 'MIT']},
                'content.Phase': phase_query_key
            }, projection={'_id': 1, 'mp_cat_id': 1, 'content': 1}
        ):
            mpfile = MPFile.from_contribution(doc)
            mp_id = mpfile.ids[0]
            contrib = mpfile.hdata[mp_id]
            row = [mp_id, get_short_object_id(doc['_id']), contrib['Formula']]
            if phase is None:
                row.append(contrib['Phase'])
            row += [contrib['dHf'], contrib['dHh'], contrib['GS'], 'TODO']
            # TODO URLs for mp_id and cid
            data.append((mp_id, row))

        return DataFrame.from_items(data, orient='index', columns=columns)

    def get_provenance(self):
        for doc in self.query_contributions(
            criteria={
                'project': {'$in': ['LBNL', 'MIT']}, 'content.Authors': {'$exists': 1},
                'content.Title': {'$exists': 1}, 'content.Reference': {'$exists': 1}
            },
            projection={
                '_id': 1, 'mp_cat_id': 1, 'content.Authors': 1,
                'content.Reference': 1, 'content.Title': 1
            }
        ):
            mpfile = MPFile.from_contribution(doc)
            mp_id = mpfile.ids[0]
            return mpfile.hdata[mp_id]

    def get_phases(self):
        phases = set()

        for doc in self.query_contributions(
            criteria={
                'project': {'$in': ['LBNL', 'MIT']},
                'content.Phase': {'$exists': 1}
            }, projection={'_id': 0, 'content.Phase': 1}
        ):
            phases.add(doc['content']['Phase'])

        return list(phases)


@@ -1620,33 +1620,33 @@
 IT'%5D%7D, 'content.
-A
+a
 uthors': %7B'$exis
@@ -1675,25 +1675,25 @@
    'content.
-T
+t
 itle': %7B'$ex
@@ -1704,33 +1704,33 @@
 ': 1%7D, 'content.
-R
+r
 eference': %7B'$ex
@@ -1834,9 +1834,9 @@
 ent.
-A
+a
 utho
@@ -1868,17 +1868,17 @@
 content.
-R
+r
 eference
@@ -1892,17 +1892,17 @@
 content.
-T
+t
 itle': 1

lower-case in provenance