namespace View_by_Distance.Shared.Models.Stateless.Methods;

internal abstract class FaceFileSystem
{ // ...

    internal static void SearchForMissingFile(string fullFileName, FileInfo fileInfo, string dataFullFileName)
    {
        if (fileInfo is null || string.IsNullOrEmpty(fileInfo.FullName))
            fileInfo = new FileInfo(fullFileName);
        if (Directory.Exists(fileInfo.DirectoryName))
            throw new Exception($"File [{fileInfo.Name}] <{fullFileName}> doesn't exist!");
        else
        {
            string? parentDirectoryName = Path.GetDirectoryName(fileInfo.DirectoryName);
            if (!Directory.Exists(parentDirectoryName))
                throw new Exception($"Parent directory <{parentDirectoryName}> doesn't exist!");
            else
            {
                string[] files = Directory.GetFiles(parentDirectoryName, Path.GetFileName(fullFileName), SearchOption.AllDirectories);
                if (!files.Any())
                    throw new Exception($"File [{fileInfo.Name}] <{fullFileName}> doesn't exist (deep search)!");
                else
                {
                    if (string.IsNullOrEmpty(dataFullFileName) && !dataFullFileName.EndsWith(".json"))
                        throw new Exception($"Maybe file <{Path.GetFileNameWithoutExtension(fullFileName)}> was moved to <{files[0]}>!");
                    else
                    {
                        string json = File.ReadAllText(dataFullFileName);
                        if (!json.Contains("MesaFab"))
                            throw new Exception($"Maybe file <{Path.GetFileNameWithoutExtension(fullFileName)}> was moved to <{files[0]}> (unknown)!");
                        else
                        {
                            json = json.Replace("MesaFab", "Mesa Fab");
                            fileInfo = new FileInfo(dataFullFileName);
                            File.WriteAllText(fileInfo.FullName, json);
                            File.SetLastWriteTime(fileInfo.FullName, fileInfo.LastWriteTime);
                            _ = new FileInfo(fullFileName);
                        }
                    }
                }
            }
        }
    }

    internal static Models.FaceFileSystem[] GetFaceFileSystemCollection(string requestPath, (string RootResultsDirectoryAbsoluteUri, string C_ResizeContentDirectory, string D_FacesContentDirectory, string E_DistanceCollectionDirectory) tuple, string selectedFileFullName)
    {
        List<Models.FaceFileSystem> results = new();
        FileInfo fileInfo;
        int? locationIndex;
        string jsonFileName;
        string? directoryName;
        string fileNameWithoutExtension;
        Models.FaceFileSystem faceFileSystem;
        string eDistanceCollectionFileFullName;
        Models.Face[] face = Face.GetFaces(selectedFileFullName);
        string extension = Path.GetExtension(selectedFileFullName);
        for (int i = 0; i < face.Length; i++)
        {
            if (face[i] is null)
                continue;
            locationIndex = 0;
            directoryName = Path.GetDirectoryName("face[i].RelativePath");
            if (directoryName is null)
                continue;
            fileNameWithoutExtension = Path.GetFileNameWithoutExtension("face[i].RelativePath");
            jsonFileName = string.Concat(locationIndex.Value, " - ", fileNameWithoutExtension, extension);
            eDistanceCollectionFileFullName = Path.Combine($"{tuple.E_DistanceCollectionDirectory}{directoryName}", fileNameWithoutExtension, jsonFileName);
            if (i == 0 && extension is ".json" && eDistanceCollectionFileFullName != selectedFileFullName)
                throw new Exception();
            fileInfo = new(eDistanceCollectionFileFullName);
            if (!fileInfo.Exists)
                throw new Exception($"File <{eDistanceCollectionFileFullName}> doesn't exist!");
            faceFileSystem = new Models.FaceFileSystem(requestPath, tuple.RootResultsDirectoryAbsoluteUri.Length, tuple.C_ResizeContentDirectory, tuple.D_FacesContentDirectory, fileInfo.FullName, face[i]);
            results.Add(faceFileSystem);
        }
        return results.ToArray();
    }

}