import time
import email.utils

from datapackage import DataPackage
from jsontableschema_sql import Storage

from babbage_fiscal.callbacks import *
from .model_registry import ModelRegistry
from .config import get_engine
from .fdp_utils import fdp_to_model
from .db_utils import database_name, table_name_for_package


def factorize(factors, rec, key):
    if key in factors:
        return rec[key]*factors[key]
    return rec[key]


def _translator_iterator(it, field_order, factors, callback):
    count = 0
    for rec in it:
        count += 1
        if count % 1000 == 1 and callback is not None:
            callback(count=count)
        rec = (count,) + tuple(factorize(factors, rec, k) for k in field_order)
        yield rec


def noop(*args, **kw):
    pass


class FDPLoader(object):
    """
    Utility class for loading FDPs to the DB
    """

    def __init__(self, engine=None):
        if engine is None:
            self.engine = get_engine()
        else:
            self.engine = engine

    @staticmethod
    def load_fdp_to_db(package, engine=None, callback=None):
        """
        Load an FDP to the database, create a babbage model and save it as well
        :param package: URL for the datapackage.json
        :param engine: DB engine
        :param callback: callback to use to send progress updates
        """

        # Load and validate the datapackage
        if engine is None:
            engine = get_engine()
        if callback is None:
            callback = noop
        callback(status=STATUS_LOADING_DATAPACKAGE)
        dpo = DataPackage(package, schema='fiscal')
        callback(status=STATUS_VALIDATING_DATAPACKAGE)
        dpo.validate()
        callback(status=STATUS_LOADING_RESOURCE)
        resource = dpo.resources[0]
        schema = resource.descriptor['schema']

        # Use the cube manager to get the table name
        registry = ModelRegistry()
        datapackage_name = dpo.descriptor['name']
        datapackage_owner = dpo.descriptor['owner']
        datapackage_author = dpo.descriptor['author']

        # Get the full name from the author field, and rewrite it without the email
        fullname, email_addr = email.utils.parseaddr(datapackage_author)
        email_addr = email_addr.split('@')[0] + '@not.shown'
        dpo.descriptor['author'] = '{0} <{1}>'.format(fullname, email_addr)
        dpo.descriptor.setdefault('private', True)

        # Measure factors
        measures = dpo.descriptor.get('model',{}).get('measures',{})
        factors = {}
        for _, measure in measures.items():
            factor = measure.get('factor',1)
            if factor != 1:
                factors[measure.get('source')] = factor

        model_name = "{0}:{1}".format(datapackage_owner, datapackage_name)
        table_name = table_name_for_package(datapackage_owner, datapackage_name)

        all_fields = set()
        field_translation = {}
        field_order = []
        # Process schema - slugify field names
        for field in schema['fields']:
            name = database_name(field['name'], all_fields)
            all_fields.add(name)
            translated_field = {
                'name': name,
                'type': field['type']
            }
            field_translation[field['name']] = translated_field
            field_order.append(field['name'])

        storage_schema = {
            'fields': [
                {
                    'type': f['type'],
                    'name': field_translation[f['name']]['name'],
                    'format': f.get('format', 'default')
                }
                for f in schema['fields']
                ],
            # Babbage likes just one primary key
            'primaryKey': '_id'
        }

        # Add Primary key to schema
        storage_schema['fields'].insert(0, {
            'name': '_id',
            'type': 'integer'
        })

        # Load 1st resource data into DB
        storage = Storage(engine)
        if storage.check(table_name):
            callback(status=STATUS_DELETING_TABLE)
            storage.delete(table_name)
        callback(status=STATUS_CREATING_TABLE)
        storage.create(table_name, storage_schema)
        callback(status=STATUS_LOADING_DATA_READY)
        storage.write(table_name, _translator_iterator(resource.iter(), field_order, factors, callback))

        # Create Babbage Model
        callback(status=STATUS_CREATING_BABBAGE_MODEL)
        model = fdp_to_model(dpo, table_name, resource, field_translation)
        callback(status=STATUS_SAVING_METADATA)
        registry.save_model(model_name, package, dpo.descriptor,
                            model, datapackage_name, fullname)
        return model_name, dpo.descriptor, model

@@ -3894,16 +3894,910 @@
     %7D)%0A%0A
+        # Create Babbage Model%0A        callback(status=STATUS_CREATING_BABBAGE_MODEL)%0A        model = fdp_to_model(dpo, table_name, resource, field_translation)%0A%0A        # Create indexes%0A        indexes = %5B%5D%0A        primary_keys = resource.descriptor%5B'schema'%5D.get('primaryKey',%5B%5D)%0A        for dim in model%5B'dimensions'%5D.values():%0A            if dim%5B'label'%5D in primary_keys:%0A                key_field = dim%5B'attributes'%5D%5Bdim%5B'key_attribute'%5D%5D%5B'label'%5D%0A                key_field = field_translation%5Bkey_field%5D%5B'name'%5D%0A                indexes.append((key_field,))%0A%0A                label_field = dim%5B'attributes'%5D.get(dim.get('label_attribute'), %7B%7D).get('label')%0A                if label_field is not None:%0A                    label_field = field_translation%5Blabel_field%5D%5B'name'%5D%0A                    if label_field != key_field:%0A                        indexes.append((key_field, label_field))%0A%0A%0A
         
@@ -4825,24 +4825,24 @@
 ata into DB%0A
-
         stor
@@ -5087,18 +5087,28 @@
 e_schema
+, indexes
 )%0A
+%0A
         
@@ -5260,169 +5260,8 @@
 ))%0A%0A
-        # Create Babbage Model%0A        callback(status=STATUS_CREATING_BABBAGE_MODEL)%0A        model = fdp_to_model(dpo, table_name, resource, field_translation)%0A
     

Add indexes to loaded tables