using LiteDB;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VIZ.Package.Storage
{
    /// <summary>
    /// 本地数据上下文
    /// </summary>
    public class LocalDbContext
    {
        /// <summary>
        /// 日志
        /// </summary>
        private static ILog log = LogManager.GetLogger(typeof(LocalDbContext));

        /// <summary>
        /// 数据库
        /// </summary>
        private ILiteDatabase Database;

        /// <summary>
        /// LiteDB数据库
        /// </summary>
        /// <param name="path">数据库路径</param>
        public LocalDbContext(string path)
        {
            this.Path = path;

            this.Database = new LiteDatabase(path);

            this.VizConfig = this.Database.GetCollection<VizConfigEntity>();
            this.MediaConfig = this.Database.GetCollection<MediaConfigEntity>();
            this.HotKeyConfig = this.Database.GetCollection<HotKeyConfigEntity>();
            this.ConnGroup = this.Database.GetCollection<ConnGroupEntity>();
            this.Conn = this.Database.GetCollection<ConnEntity>();
            this.PluginMappingConfig = this.Database.GetCollection<PluginMappingConfigEntity>();
            this.ClockField = this.Database.GetCollection<ClockFieldEntity>();

            this.SaveLayoutField = this.Database.GetCollection<SaveLayoutFieldEntity>();
        }

        /// <summary>
        /// 数据库文件路径
        /// </summary>
        public string Path { get; private set; }

        /// <summary>
        /// Viz配置
        /// </summary>
        public ILiteCollection<VizConfigEntity> VizConfig { get; private set; }

        /// <summary>
        /// 媒资库配置
        /// </summary>
        public ILiteCollection<MediaConfigEntity> MediaConfig { get; private set; }

        /// <summary>
        /// 热键配置
        /// </summary>
        public ILiteCollection<HotKeyConfigEntity> HotKeyConfig { get; private set; }

        /// <summary>
        /// 连接分组
        /// </summary>
        public ILiteCollection<ConnGroupEntity> ConnGroup { get; private set; }

        /// <summary>
        /// 连接
        /// </summary>
        public ILiteCollection<ConnEntity> Conn { get; private set; }


        /// <summary>
        /// 时钟保存字段
        /// </summary>
        public ILiteCollection<ClockFieldEntity> ClockField { get; private set; }


        /// <summary>
        /// 布局保存字段
        /// </summary>
        public ILiteCollection<SaveLayoutFieldEntity> SaveLayoutField { get; private set; }
        /// <summary>
        /// 插件映射
        /// </summary>
        public ILiteCollection<PluginMappingConfigEntity> PluginMappingConfig { get; private set; }

        /// <summary>
        /// 销毁
        /// </summary>
        public void Dispose()
        {
            this.VizConfig = null;
            this.MediaConfig = null;
            this.HotKeyConfig = null;
            this.ConnGroup = null;
            this.Conn = null;
            this.PluginMappingConfig = null;

            this.Database?.Dispose();
        }
    }
}