from __future__ import absolute_import
from __future__ import unicode_literals

from collections import OrderedDict

from corehq.apps.translations.models import TransifexProject
from corehq.apps.translations.integrations.transifex.client import TransifexApiClient


class ProjectMigrator(object):
    def __init__(self, domain, project_slug, source_app_id, target_app_id, resource_ids_mapping):
        """
        Migrate a transifex project from one app to another by
        1. updating slugs of resources to use new module/form ids
        2. updating context of translations in "Menus_and_forms" sheet to use new module/form ids
        :param resource_ids_mapping: tuple of type, old_id, new_id
        """
        self.domain = domain
        self.project_slug = project_slug
        self.project = TransifexProject.objects.get(slug=project_slug)
        self.client = TransifexApiClient(self.project.organization.get_api_token, self.project.organization,
                                         project_slug)
        self.source_app_id = source_app_id
        self.target_app_id = target_app_id
        self.resource_ids_mapping = resource_ids_mapping
        self.id_mapping = {old_id: new_id for _, old_id, new_id in self.resource_ids_mapping}

    def _update_slugs(self):
        # ToDo: update slugs on transifex
        pass

    def _update_context(self, translations):
        # ToDo: update context on Menus and forms sheet
        pass

    def _upload_new_translations(self, translations):
        # ToDo: upload updated translations
        pass

    def _update_menus_and_forms_sheet(self):
        translations = OrderedDict()
        # ToDo: pull translations from transifex
        self._update_context(translations)
        return self._upload_new_translations(translations)

    def validate(self):
        ProjectMigrationValidator(self).valid()

    def migrate(self):
        self._update_slugs()
        self._update_menus_and_forms_sheet()


class ProjectMigrationValidator(object):
    def __init__(self, migrator):
        self.migrator = migrator

    def _ensure_same_source_lang(self):
        # ToDo: ensure same source lang for source app, target app and on transifex project
        pass

    def valid(self):
        self._ensure_same_source_lang()

@@ -73,16 +73,46 @@
 terals%0A%0A
+from memoized import memoized%0A
 from col
@@ -287,16 +287,121 @@
 iClient%0A
+from corehq.apps.translations.integrations.transifex.const import (%0A    TRANSIFEX_SLUG_PREFIX_MAPPING,%0A)%0A
 %0A%0Aclass 
@@ -1392,86 +1392,620 @@
     
-def _update_slugs(self):%0A        # ToDo: update slugs on transifex%0A        pas
+@memoized%0A    def _get_slug_prefix(self, resource_type):%0A        return TRANSIFEX_SLUG_PREFIX_MAPPING.get(resource_type)%0A%0A    def _update_slugs(self):%0A        responses = %7B%7D%0A        for resource_type, old_id, new_id in self.resource_ids_mapping:%0A            slug_prefix = self._get_slug_prefix(resource_type)%0A            if not slug_prefix:%0A                continue%0A            resource_slug = %22%25s_%25s%22 %25 (slug_prefix, old_id)%0A            new_resource_slug = %22%25s_%25s%22 %25 (slug_prefix, new_id)%0A            responses%5Bold_id%5D = self.client.update_resource_slug(resource_slug, new_resource_slug)%0A        return response
 s%0A%0A 

update resource slugs