using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;

namespace LolDataRequestLib
{
    class StructGoldDataRequest : ARequestData

    {
        protected override void requestDataMongoDB()
        {
            try
            {
                var subFilterMonsterSort =
                    Builders<BsonDocument>.Filter.Or(
                   Builders<BsonDocument>.Filter.Eq(e => e["eventDocument.source"], "turretPlate"),
                   Builders<BsonDocument>.Filter.Eq(e => e["eventDocument.source"], "turret")
               );                

                var filter = Builders<BsonDocument>.Filter.And(subFilterMonsterSort); 


                var projection = Builders<BsonDocument>.Projection
                   .Exclude("_id")
                   .Include("eventDocument");

                List<BsonDocument> documents = eventDataBase.GetCollection<BsonDocument>(this.mCollectionName)
                 .Find(filter)
                 .SortByDescending(x => x["sequenceIndex"])
                 .Project(projection)
                 .ToList();
                 

                if (documents.Count == 0)
                {
                    mUpdatedBsonValue = null;
                    return;
                }

                BsonDocument rtnValue = new BsonDocument();

                foreach (BsonDocument item in documents)
                {
                    rtnValue.Add(item["eventDocument"]["sequenceIndex"].ToString(), item["eventDocument"].ToBsonDocument());
                }
                rtnValue.Add("sequenceIndex", documents.Last()["eventDocument"]["sequenceIndex"].ToInt32());

                mUpdatedBsonValue = rtnValue;
            }
            catch (Exception ex) { }
        }

    }
}
