#!/usr/bin/env dart
// Migraine Log - a simple multi-platform headache diary
// Copyright (C) 2021    Eskild Hustvedt
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
import 'dart:convert';
import 'dart:io';

bool disableIOS = false;

void writeCodeFile(String code, String name, Map iosXCStrings, [ bool skipIOS = true, bool skipAndroid = false ])
{
  // Android
  if (!skipAndroid)
  {
    Directory('./android/app/src/main/res/values-b+'+code+'/').createSync();
    File('./android/app/src/main/res/values-b+'+code+'/strings.xml').writeAsStringSync('<?xml version="1.0" encoding="utf-8"?>\n<resources>\n  <string name="app_name">'+name+'</string>\n</resources>');
  }
  // iOS
  if (!skipIOS && !disableIOS)
  {
    if(code == "zh")
    {
      code = "zh-Hans";
    }
    Directory('./ios/Runner/'+code+'.lproj').createSync();
    File('./ios/Runner/'+code+'.lproj/InfoPlist.strings').writeAsStringSync('CFBundleDisplayName = "'+name+'"'+";\n");
    // Dummy files
    File('./ios/Runner/'+code+'.lproj/LaunchScreen.strings').writeAsStringSync('');
    File('./ios/Runner/'+code+'.lproj/Main.strings').writeAsStringSync('');
    iosXCStrings[code] = {
      "stringUnit": {
        "state":"translated",
        "value": name,
      }
    };
  }
}

void main ()
{
  if (! Directory("./ios/Runner/").existsSync())
  {
    disableIOS = true;
  }

  var iosXCStrings = {};
  var i18nFileList = Directory("./i18n/").listSync();
  i18nFileList.sort( (a,b) => a.toString().compareTo(b.toString()));
  for(var file in i18nFileList)
  {
    var path = file.path;
    if(path.endsWith(".arb") && !path.contains('intl_messages') && !path.endsWith('/en.arb'))
    {
      var code = RegExp(r'\/([^/]+)\.arb').firstMatch(path)!.group(1);
      if(code == null)
      {
        throw("Failed to extract code from filename "+path);
      }
      var content = jsonDecode((file as File).readAsStringSync());
      if(code == 'nn')
      {
        writeCodeFile("nn",content['Migraine Log'], iosXCStrings, true);
        writeCodeFile("no",content['Migraine Log'], iosXCStrings, true);
        writeCodeFile("nb",content['Migraine Log'], iosXCStrings);
      }
      else if(code == 'fr')
      {
        writeCodeFile("fr",content['Migraine Log'], iosXCStrings);
        writeCodeFile("fr-BE",content['Migraine Log'], iosXCStrings, false, true);
      }
      else if(code == 'pt')
      {
        writeCodeFile("pt",content['Migraine Log'], iosXCStrings);
        writeCodeFile("pt-PT",content['Migraine Log'], iosXCStrings, false, true);
      }
      else
      {
        writeCodeFile(code,content['Migraine Log'], iosXCStrings);
      }
    }
  }
  if (!disableIOS)
  {
    var encoder = new JsonEncoder.withIndent("  ");
    File('./ios/Runner/InfoPlist.xcstrings').writeAsStringSync(encoder.convert({
      "sourceLanguage" : "en",
      "strings" : {
        "CFBundleDisplayName" : {
          "extractionState" : "migrated",
          "localizations" : iosXCStrings,
        }
      },
      "version" : "1.0"
    }));
  }
}
