﻿using System.IO;
using Codex.Logging;
using Codex.Search;
using Lucene.Net.Store;
using Directory = Lucene.Net.Store.Directory;

namespace Codex.Lucene.Search
{
    public class LuceneConfiguration : CodexBaseConfiguration
    {
        public string Directory { get; set; }

        private Directory Root { get; set; }

        public Logger Logger = new ConsoleLogger();

        private string GetIndexRoot(SearchType searchType)
        {
            return Path.Combine(Directory, searchType.IndexName);
        }

        public Directory OpenIndexDirectory(SearchType searchType)
        {
            if (Root != null)
            {
                return new ScopedDirectory(Root, searchType.IndexName);
            }

            return FSDirectory.Open(GetIndexRoot(searchType));
        }

        public LuceneConfiguration(string directory)
        {
            Directory = directory;
        }

        private LuceneConfiguration()
        {
        }

        public static LuceneConfiguration CreateFromRoot(Directory root)
        {
            return new LuceneConfiguration() { Root = root };
        }
    }
}
