using Microsoft.VisualBasic;
using static Microsoft.VisualBasic.CompilerServices.LikeOperator;
using System;
using System.Collections.Generic;
using System.IO;
using System.Resources;
using System.Collections;
using System.Linq;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace SM64_ROM_Manager_LangRes_Collector
{
    class Program
    {
        static void Main(string[] args)
        {
            var resFiles = new List<string>();
            var ofd = new CommonOpenFileDialog { IsFolderPicker = true };
            string rootPath, outputPath;
            var fileNameBlackList = new string[]
            {
                "Resources.resx",
                "My*Icons.resx",
                "*.de.resx",
                "ReflectionSymbols.resx",
                "UpdatingAdministrationLangRes"
            };
            var fileNameWhiteList = new string[]
            {
                "*.resx"
            };
            var blackListPropNames = new string[]
            {
                ">>*",
                "*.AccessibleName",
                "*.AccessibleDescription"
            };

            // Get root path
            ofd.Title = "Root (repository) directory";
            if (ofd.ShowDialog() == CommonFileDialogResult.Ok)
                rootPath = ofd.FileName;
            else
                return;

            // Get output path
            ofd.Title = "Output directory";
            if (ofd.ShowDialog() == CommonFileDialogResult.Ok)
                outputPath = ofd.FileName;
            else
                return;

            // Collect files
            if (Directory.Exists(rootPath))
            {
                foreach (var filePath in Directory.GetFiles(rootPath, "*.resx", SearchOption.AllDirectories))
                {
                    var fileName = Path.GetFileName(filePath);
                    var copyFile = true;

                    foreach (var blackFileName in fileNameBlackList)
                    {
                        if (copyFile && LikeString(fileName, blackFileName, CompareMethod.Binary))
                            copyFile = false;
                    }

                    foreach (var whiteFileName in fileNameWhiteList)
                    {
                        if (copyFile && !LikeString(fileName, whiteFileName, CompareMethod.Binary))
                            copyFile = false;
                    }

                    if (copyFile)
                        resFiles.Add(filePath);
                }
            }

            // Create output path if not exists
            Directory.CreateDirectory(outputPath);

            // Copy and prepair files
            foreach (var resFileInput in resFiles)
            {
                var resReader = new ResXResourceReader(resFileInput);
                var propsToCopy = new Dictionary<string, string>();

                // Collect input properties
                foreach (DictionaryEntry kvp in resReader)
                {
                    var propName = (string)kvp.Key;
                    var useProp = true;

                    foreach (var blackPropName in blackListPropNames)
                    {
                        if (useProp && LikeString(propName, blackPropName, CompareMethod.Binary))
                            useProp = false;
                    }

                    if (useProp && kvp.Value is string)
                    {
                        var val = (string)kvp.Value;

                        if (!string.IsNullOrEmpty(val))
                            propsToCopy.Add(propName, (string)kvp.Value);
                    }
                }

                resReader.Close();

                // Write output ResX file
                if (propsToCopy.Any())
                {
                    var resFileOutput = Path.Combine(outputPath, Path.GetFileName(resFileInput));
                    var resWriter = new ResXResourceWriter(resFileOutput);

                    foreach (var kvp in propsToCopy)
                        resWriter.AddResource(kvp.Key, kvp.Value);

                    resWriter.Close();
                }
            }
        }
    }
}
