using aeqw89.DataKeys;
using aeqw89.PersistentData;
using Beam.Dynamic;
using Beam.Models;

namespace Beam.Data {
    using BeamFile = Models.File;
    
    public class BeamDataContext : BaseDataDictionary {
        #region  Tables
        public Table<Template> Templates {
            get => GetOrCreateTable<Template>(nameof(Templates));
            set => Set(nameof(Templates), value);
        }
        
        public Table<DataBindings> Bindings {
            get => GetOrCreateTable<DataBindings>(nameof(Bindings));
            set => Set(nameof(Bindings), value);
        }
        public Table<WebResource> Resources {
            get => GetOrCreateTable<WebResource>(nameof(Resources));
            set => Set(nameof(Resources), value);
        }
        public Table<ResourceDictionary> ResourceDictionaries {
            get => GetOrCreateTable<ResourceDictionary>(nameof(ResourceDictionaries));
            set => Set(nameof(ResourceDictionaries), value);
        }
        public Table<ImmutableState> InitialStates {
            get => GetOrCreateTable<ImmutableState>(nameof(InitialStates));
            set => Set(nameof(InitialStates), value);
        }
        public Table<BeamFile> Files {
            get => GetOrCreateTable<BeamFile>(nameof(Files));
            set => Set(nameof(Files), value);
        }
        #endregion
        #region Junctions
        public Junction<WebResource, ResourceDictionary> WebResourceToResourceDictionaryJunction {
            get => GetOrCreateJunction<WebResource, ResourceDictionary>(nameof(WebResourceToResourceDictionaryJunction));
            set => Set(nameof(WebResourceToResourceDictionaryJunction), value);
        }
        #endregion
        #region Computed
        public Dictionary<DataKey<WebResource>, ResourceDictionary[]> ResourceDictionariesByNovel =>
            Resources.Keys.ToDictionary(x => x,
                x => WebResourceToResourceDictionaryJunction[x].Select(y => ResourceDictionaries[y]).ToArray());

        #endregion
    }
}
