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 StructDataRequest : 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.buildingType"], "turret"),
                   Builders<BsonDocument>.Filter.Eq(e => e["eventDocument.buildingType"], "inhibitor")
               );

                //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 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();

                /*
                var filter = Builders<BsonDocument>.Filter.Eq("RequestGameID", DataManager.getInstance().mPlatformGameID);
                var subFilter = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(
                    "{'eventDocument.sequenceIndex' : 1,'eventDocument.teamID' : 1, 'eventDocument.gameTime' : 1, 'eventDocument.lane' : 1, 'eventDocument.turretTier' : 1, 'eventDocument.buildingType' : 1}");
                List<BsonDocument> documents = this.eventDataBase.GetCollection<BsonDocument>("building_destroyed")
                    .Find(filter)
                    .SortBy(x => x["sequenceIndex"])
                    .Project(subFilter)
                    .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) { } 
        }

    }
}
