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 ObjectDataRequest : ARequestData
    {
        protected override void requestDataMongoDB()
        {
            try
            {

                //var subFilter = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>("{'eventDocument.gameTime' : {$gt : " + calculatedTime + " }}");

                var subFilterMonsterSort = //Builders<BsonDocument>.Filter.ElemMatch("eventDocument",
                    Builders<BsonDocument>.Filter.Or(
                        Builders<BsonDocument>.Filter.Eq(e => e["eventDocument.monsterType"], "dragon"),
                        Builders<BsonDocument>.Filter.Eq(e => e["eventDocument.monsterType"], "riftHerald"),
                        Builders<BsonDocument>.Filter.Eq(e => e["eventDocument.monsterType"], "baron"),
                        Builders<BsonDocument>.Filter.Eq(e => e["eventDocument.monsterType"], "VoidGrub"),
                        Builders<BsonDocument>.Filter.Eq(e => e["eventDocument.monsterType"], "RuinousAtakhan"),
                        Builders<BsonDocument>.Filter.Eq(e => e["eventDocument.monsterType"], "VoraciousAtakhan"),
                        Builders<BsonDocument>.Filter.Eq(e => e["eventDocument.monsterType"], "ThornboundAtakhan")
                    );

                //var subFilterGameID = Builders<BsonDocument>.Filter.Eq("RequestGameID", DataManager.getInstance().mPlatformGameID);

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

                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.First()["eventDocument"]["sequenceIndex"].ToInt32());

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

    }
}
