File size: 4,182 Bytes
e3ea2f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121e5f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3ea2f2
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Mdict.Mdx.Readers;
using Mdx.Storage;

namespace mdict.Services
{
    public class DictionaryItem
    {
        public string FilePath { get; set; }
        public string Name { get; set; }
        public MdxReader Reader { get; set; }
    }

    public class LibraryManager : IDisposable
    {
        private static LibraryManager _instance;
        public static LibraryManager Instance => _instance ??= new LibraryManager();

        public ObservableCollection<DictionaryItem> LoadedDictionaries { get; } = new ObservableCollection<DictionaryItem>();

        public void AddDictionary(string path)
        {
            if (LoadedDictionaries.Any(d => d.FilePath.Equals(path, StringComparison.OrdinalIgnoreCase)))
                return;

            try
            {
                var reader = new MdxReader(path);
                var info = reader.ContentDb.Meta.DbInfo;
                var name = !string.IsNullOrEmpty(info.Title) ? info.Title : System.IO.Path.GetFileNameWithoutExtension(path);

                LoadedDictionaries.Add(new DictionaryItem
                {
                    FilePath = path,
                    Name = name,
                    Reader = reader
                });
            }
            catch (Exception ex)
            {
                // TODO: Log error
                Console.WriteLine($"Failed to load dictionary {path}: {ex.Message}");
                throw;
            }
        }

        public void RemoveDictionary(DictionaryItem item)
        {
            if (LoadedDictionaries.Contains(item))
            {
                item.Reader?.Dispose();
                LoadedDictionaries.Remove(item);
            }
        }

        public List<(DictionaryItem Dictionary, KeyIndex Index)> Search(string query)
        {
            var results = new List<(DictionaryItem, KeyIndex)>();

            foreach (var dict in LoadedDictionaries)
            {
                try
                {
                    var index = dict.Reader.Lookup(query);
                    if (index != null)
                    {
                        results.Add((dict, index));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error searching in {dict.Name}: {ex.Message}");
                }
            }

            return results;
        }
        
        public List<string> GetSuggestions(string prefix, int maxCount = 20)
        {
            // Basic implementation: just verify exact match or start match for now.
            // A real implementation would iterate keys. 
            // MdxReader doesn't expose key iteration easily yet without scanning blocks.
            // For now, we rely on exact lookup or maybe simple linear scan if supported.
            
            // TODO: Implement prefix search in MdxReader (ZdbReader).
            return new List<string>();
        }

        public string? GetHtml(string key)
        {
            foreach (var dict in LoadedDictionaries)
            {
                try
                {
                    var index = dict.Reader.Lookup(key);
                    if (index != null)
                    {
                        return dict.Reader.GetHtml(index);
                    }
                }
                catch { }
            }
            return null;
        }

        public byte[]? GetResource(string key)
        {
            foreach (var dict in LoadedDictionaries)
            {
                try
                {
                    var data = dict.Reader.GetResource(key);
                    if (data != null)
                    {
                        return data;
                    }
                }
                catch { }
            }
            return null;
        }

        public void Dispose()
        {
            foreach (var dict in LoadedDictionaries)
            {
                dict.Reader?.Dispose();
            }
            LoadedDictionaries.Clear();
        }
    }
}