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

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

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

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

            this.Database = new LiteDatabase(path);

            this.PageTemplate = this.Database.GetCollection<PageTemplateEntity>();
            this.PageGroup = this.Database.GetCollection<PageGroupEntity>();
            this.Page = this.Database.GetCollection<PageEntity>();
            this.ControlField = this.Database.GetCollection<ControlFieldEntity>();
            this.ControlObject = this.Database.GetCollection<ControlObjectEntity>();
            this.SceneThumbnailCache = this.Database.GetStorage<Guid>("_SceneThumbnailFiles", "_SceneThumbnailChunks");
            this.ScrollElementPackage = this.Database.GetCollection<ScrollElementPackageEntity>();
            this.PageCommand = this.Database.GetCollection<PageCommandEntity>();
        }

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

        /// <summary>
        /// 页模板
        /// </summary>
        public ILiteCollection<PageTemplateEntity> PageTemplate { get; private set; }

        /// <summary>
        /// 页分组
        /// </summary>
        public ILiteCollection<PageGroupEntity> PageGroup { get; private set; }

        /// <summary>
        /// 页
        /// </summary>
        public ILiteCollection<PageEntity> Page { get; private set; }

        /// <summary>
        /// 控制字段
        /// </summary>
        public ILiteCollection<ControlFieldEntity> ControlField { get; private set; }

        /// <summary>
        /// 控制对象
        /// </summary>
        public ILiteCollection<ControlObjectEntity> ControlObject { get; private set; }

        /// <summary>
        /// 场景缩略图缓存
        /// </summary>
        public ILiteStorage<Guid> SceneThumbnailCache { get; private set; }

        /// <summary>
        /// 底飞数据包
        /// </summary>
        public ILiteCollection<ScrollElementPackageEntity> ScrollElementPackage { get; private set; }

        /// <summary>
        /// 页命令
        /// </summary>
        public ILiteCollection<PageCommandEntity> PageCommand { get; private set; }

        /// <summary>
        /// 销毁
        /// </summary>
        public void Dispose()
        {
            this.PageTemplate = null;
            this.PageGroup = null;
            this.Page = null;
            this.ControlField = null;
            this.ControlObject = null;
            this.SceneThumbnailCache = null;
            this.ScrollElementPackage = null;
            this.PageCommand = null;

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