<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using UtyDepend;
using UtyDepend.Config;
using UtyMap.Unity.Infrastructure.Primitives;
using UtyRx;

namespace UtyMap.Unity.Data
{
    /// <summary> Defines behavior of class responsible of mapdata processing. </summary>
    public interface IMapDataStore : IObserver<Tile>, IObserver<MapQuery>,
        IObservable<MapData>, IObservable<Tile>, IObservable<Element>
    {
        /// <summary> Registers in-memory data storage with given key. </summary>
        /// <param name="storageKey"> Storage key.</param>
        void Register(string storageKey);

        /// <summary> Registers persistent data storage with given key. </summary>
        /// <param name="storageKey"> Storage key.</param>
        /// <param name="indexPath"> Persistent index path. </param>
        void Register(string storageKey, string indexPath);

        /// <summary> Adds mapdata to the specific data storage. </summary>
        /// <param name="storageKey"> Storage key. </param>
        /// <param name="dataPath"> Path to mapdata. </param>
        /// <param name="stylesheet"> Stylesheet which to use during import. </param>
        /// <param name="levelOfDetails"> Which level of details to use. </param>
        /// <returns> Returns progress status. </returns>
        IObservable<int> AddTo(string storageKey, string dataPath, Stylesheet stylesheet, Range<int> levelOfDetails);

        /// <summary> Adds mapdata to the specific data storage. </summary>
        /// <param name="storageKey"> Storage key. </param>
        /// <param name="dataPath"> Path to mapdata. </param>
        /// <param name="stylesheet"> Stylesheet which to use during import. </param>
        /// <param name="quadKey"> QuadKey to add. </param>
        /// <returns> Returns progress status. </returns>
        IObservable<int> AddTo(string storageKey, string dataPath, Stylesheet stylesheet, QuadKey quadKey);
    }

    /// <summary> Default implementation of map data store. </summary>
    internal class MapDataStore : IMapDataStore, IDisposable, IConfigurable
    {
        private readonly IMapDataProvider _mapDataProvider;
        private readonly IMapDataLibrary _mapDataLibrary;

        private readonly List<string> _storageKeys = new List<string>();
        private readonly List<IObserver<MapData>> _dataObservers = new List<IObserver<MapData>>();
        private readonly List<IObserver<Tile>> _tileObservers = new List<IObserver<Tile>>();
        private readonly List<IObserver<Element>> _elementObservers = new List<IObserver<Element>>();

        [Dependency]
        public MapDataStore(IMapDataProvider mapDataProvider, IMapDataLibrary mapDataLibrary)
        {
            _mapDataLibrary = mapDataLibrary;

            _mapDataProvider = mapDataProvider;
            _mapDataProvider
                .ObserveOn(Scheduler.ThreadPool)
                .Subscribe(value =>
                {
                    // We have map data in store.
                    if (String.IsNullOrEmpty(value.Item2))
                        _mapDataLibrary.Get(value.Item1, _dataObservers);
                    else
                        // NOTE store data in the first registered store
                        AddTo(_storageKeys.First(), value.Item2, value.Item1.Stylesheet, value.Item1.QuadKey)
                            .Subscribe(progress => { }, () => _mapDataLibrary.Get(value.Item1, _dataObservers));
                });
        }

        #region Interface implementations

        /// <inheritdoc />
        public void Register(string storageKey)
        {
            _storageKeys.Add(storageKey);
            _mapDataLibrary.Register(storageKey);
        }

        /// <inheritdoc /><commit_msg>library: subscribe observers to tile loaded event<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using UtyDepend;
using UtyDepend.Config;
using UtyMap.Unity.Infrastructure.Primitives;
using UtyRx;

namespace UtyMap.Unity.Data
{
    /// <summary> Defines behavior of class responsible of mapdata processing. </summary>
    public interface IMapDataStore : IObserver<Tile>, IObserver<MapQuery>,
        IObservable<MapData>, IObservable<Tile>, IObservable<Element>
    {
        /// <summary> Registers in-memory data storage with given key. </summary>
        /// <param name="storageKey"> Storage key.</param>
        void Register(string storageKey);

        /// <summary> Registers persistent data storage with given key. </summary>
        /// <param name="storageKey"> Storage key.</param>
        /// <param name="indexPath"> Persistent index path. </param>
        void Register(string storageKey, string indexPath);

        /// <summary> Adds mapdata to the specific data storage. </summary>
        /// <param name="storageKey"> Storage key. </param>
        /// <param name="dataPath"> Path to mapdata. </param>
        /// <param name="stylesheet"> Stylesheet which to use during import. </param>
        /// <param name="levelOfDetails"> Which level of details to use. </param>
        /// <returns> Returns progress status. </returns>
        IObservable<int> AddTo(string storageKey, string dataPath, Stylesheet stylesheet, Range<int> levelOfDetails);

        /// <summary> Adds mapdata to the specific data storage. </summary>
        /// <param name="storageKey"> Storage key. </param>
        /// <param name="dataPath"> Path to mapdata. </param>
        /// <param name="stylesheet"> Stylesheet which to use during import. </param>
        /// <param name="quadKey"> QuadKey to add. </param>
        /// <returns> Returns progress status. </returns>
        IObservable<int> AddTo(string storageKey, string dataPath, Stylesheet stylesheet, QuadKey quadKey);
    }

    /// <summary> Default implementation of map data store. </summary>
    internal class MapDataStore : IMapDataStore, IDisposable, IConfigurable
    {
        private readonly IMapDataProvider _mapDataProvider;
        private readonly IMapDataLibrary _mapDataLibrary;

        private readonly List<string> _storageKeys = new List<string>();
        private readonly List<IObserver<MapData>> _dataObservers = new List<IObserver<MapData>>();
        private readonly List<IObserver<Tile>> _tileObservers = new List<IObserver<Tile>>();
        private readonly List<IObserver<Element>> _elementObservers = new List<IObserver<Element>>();

        [Dependency]
        public MapDataStore(IMapDataProvider mapDataProvider, IMapDataLibrary mapDataLibrary)
        {
            _mapDataLibrary = mapDataLibrary;

            _mapDataProvider = mapDataProvider;
            _mapDataProvider
                .ObserveOn(Scheduler.ThreadPool)
                .Subscribe(value =>
                {
                    // We have map data in store.
                    if (String.IsNullOrEmpty(value.Item2))
                        _mapDataLibrary
                            .Get(value.Item1, _dataObservers)
                            .Subscribe(_ => _tileObservers.ForEach(t => t.OnNext(value.Item1)));
                    else
                        // NOTE store data in the first registered store
                        AddTo(_storageKeys.First(), value.Item2, value.Item1.Stylesheet, value.Item1.QuadKey)
                            .Subscribe(progress => { }, () => _mapDataLibrary.Get(value.Item1, _dataObservers));
                });
        }

        #region Interface implementations

        /// <inheritdoc />
        public void Register(string storageKey)
        {
            _storageKeys.Add(storageKey);
            _mapDataLibrary.Register(storageKey);
        }

        /// <inheritdoc />
{'fraction_non_alphanumeric': 0.10955165692007797, 'fraction_numerical': 0.0016894087069525667, 'mean_word_length': 2.4403218596334377, 'pattern_counts': {'":': 0, '<': 127, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7582006', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1955, 'n_words': 552}