# -*- coding: utf-8 *-*

# This file is part of wger Workout Manager.
#
# wger Workout Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wger Workout Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License

# Django
from django.core.management.base import BaseCommand

# wger
from wger.core.models import RepetitionUnit
from wger.exercises.models import (
    Equipment,
    ExerciseCategory,
    Muscle,
)


class Command(BaseCommand):
    """
    Helper command to extract translatable content from the database such as
    categories, muscles or equipment names and write it to files, so they can
    be extracted and translated on weblate. This is a bit hacky and ugly, but
    these strings *very* rarely change.
    """

    help = 'Write the translatable strings from the database to a file'

    def handle(self, **options):

        # Collect all translatable items
        data = [i for i in ExerciseCategory.objects.all()] \
            + [i for i in Equipment.objects.all()] \
            + [i for i in Muscle.objects.all()] \
            + [i for i in RepetitionUnit.objects.all()]

        # Django - write to .tpl file
        with open('wger/i18n.tpl', 'w') as f:
            out = '{% load i18n %}\n'
            for i in data:
                out += f'{{% translate "{i}" %}}\n'
            f.write(out)
            self.stdout.write(self.style.SUCCESS(f'Wrote content to i18n.tpl!'))

        # React - write to .tsx file (copy the file into the react repo)
        with open('wger/i18n.tsx', 'w') as f:
            out = '''
                import { useTranslation } from "react-i18next";

                export const DummyComponent = () => {
                const [t, i18n] = useTranslation();'''
            for i in data:
                out += f't("{i}");\n'

            out += '''
                return (<p></p>);
                };'''
            f.write(out)
            self.stdout.write(self.style.SUCCESS(f'Wrote content to i18n.tsx!'))

        # Flutter - write to .dart file (copy the file into the flutter repo)
        # TO BE IMPLEMENTED...

@@ -1285,16 +1285,353 @@
 ions):%0A%0A
+        # Replace whitespace and other problematic characters with underscores%0A        def cleanup_name(text: str) -%3E str:%0A            return text.lower().%5C%0A                replace(' ', '_').%5C%0A                replace('-', '_').%5C%0A                replace('/', '_').%5C%0A                replace('(', '_').%5C%0A                replace(')', '_')%0A%0A%0A
         
@@ -1663,16 +1663,16 @@
 e items%0A
-
         
@@ -1781,58 +1781,8 @@
 %5D %5C%0A
-            + %5Bi for i in Muscle.objects.all()%5D %5C%0A
     
@@ -1834,16 +1834,79 @@
 all()%5D%0A%0A
+            # + %5Bi for i in Muscle.objects.all()%5D %5C%0A%0A        #%0A
         
@@ -2201,24 +2201,34 @@
 8n.tpl!'))%0A%0A
+        #%0A
         # Re
@@ -2585,18 +2585,52 @@
 += f't(%22
-%7Bi
+exercises.%7Bcleanup_name(i.__str__())
 %7D%22);%5Cn'%0A
@@ -2812,24 +2812,34 @@
 8n.tsx!'))%0A%0A
+        #%0A
         # Fl
@@ -2859,84 +2859,969 @@
  to 
-.dart file (copy the file into the flutter repo)%0A        # TO BE IMPLEMENTED...
+app_en.arb, copy to the end of app_en.arb in the flutter repo%0A        with open('wger/app_en.arb', 'w') as f:%0A            out = ''%0A            for i in data:%0A                out += f'%22%7Bcleanup_name(i.__str__())%7D%22: %22%7Bi%7D%22,%5Cn'%0A            f.write(out)%0A            self.stdout.write(self.style.SUCCESS(f'Wrote content to app_en.arb!'))%0A%0A        # Copy to i18n.dart in the flutter repo%0A        with open('wger/i18n.dart', 'w') as f:%0A            out = '''String getTranslation(String value, BuildContext context) %7B%0A                  switch (value) %7B'''%0A            for i in data:%0A                out += f'''%0A                case '%7Bi%7D':%0A                    return AppLocalizations.of(context).%7Bcleanup_name(i.__str__())%7D;%0A                '''%0A%0A            out += f'''default:%0A                  return 'NOT TRANSLATED';%0A                  %7D%7D%0A                  %7D%7D'''%0A%0A            f.write(out)%0A            self.stdout.write(self.style.SUCCESS(f'Wrote content to i18n.dart!'))%0A
 %0A

Improve extract-i18n.py to extract to the flutter app