using System;
using System.IO;

namespace Txgy.EWS.Client.Common
{
    public class FreeSqlLocalSqLite
    {
        private static Lazy<IFreeSql> sqLiteLazy = CreateLazy();

        public static IFreeSql freeLocalSqLite => sqLiteLazy.Value;

        public static void Reset()
        {
            if (sqLiteLazy.IsValueCreated)
            {
                sqLiteLazy.Value.Dispose();
            }

            sqLiteLazy = CreateLazy();
        }

        private static Lazy<IFreeSql> CreateLazy()
        {
            return new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
             .UseConnectionString(FreeSql.DataType.Sqlite, "Data Source=" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, BusinessConfigManager.Current.Paths.LocalSqLiteDb))
             .Build());
        }
    }
}
