using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDB.Driver.Linq;

namespace JWH.MONGODB
{
    public class FDCMongoDB
    {

        public static string ConnectionString { get; set; } = "mongodb://192.168.8.181:27001";

        public static string DatabaseName { get; set; } = "fdc";

        public static List<BsonDocument> GetListCollection()
        {
            MongoClient client = new MongoClient(ConnectionString);
            IMongoDatabase database = client.GetDatabase(DatabaseName);
            List<BsonDocument> item = database.ListCollectionsAsync().Result.ToListAsync<BsonDocument>().Result;

            return item;
        }

        public static List<TraceCollection> GetCollectionData(string collectionName, DateTime dtStart, DateTime dtEnd, TraceCollection condition = null)
        {
            MongoClient client = new MongoClient(ConnectionString);
            IMongoDatabase database = client.GetDatabase(DatabaseName);
            IMongoCollection<TraceCollection> collection = database.GetCollection<TraceCollection>(collectionName);

            // # ALL Data
            //var lst = collection.Find(_ => true).ToList();
            // # Use BsonDocument
            //var filter = new BsonDocument("LotID", "13089570006B");
            // # Use Builders.Filter
            //var filterBuilder = Builders<TraceCollection>.Filter;
            //var filter = filterBuilder.Eq(x => x.LotID, "13109360026B");

            FilterDefinitionBuilder<TraceCollection> filterBuilder = Builders<TraceCollection>.Filter;
            FilterDefinition<TraceCollection> filter = filterBuilder.Gte(x => x.EVENTTIME, dtStart) & filterBuilder.Lt(x => x.EVENTTIME, dtEnd);
            if (condition != null)
            {
                string[] SkipPropertys = new string[] { "_id", "EVENTTIME" };
                foreach (PropertyInfo property in condition.GetType().GetProperties())
                {
                    object value = property.GetValue(condition);
                    if (value != null)
                    {
                        if (property.Name.Equals("LotID")) filter &= filterBuilder.Eq(x => x.LotID, value);
                        continue;
                    }
                }
            }
            List< TraceCollection> document = collection.Find(filter).ToList();
            
            return document;
        }

        public class TraceCollection
        {

            public ObjectId _id { get; set; }

            public List<object> LotList { get; set; }

            [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
            public DateTime EVENTTIME { get; set; }

            public object Value { get; set; }

            public string VIDName { get; set; }

            public object UnitNO { get; set; }

            public string Recipe { get; set; }

            public string ProductID { get; set; }

            public string MaterialID { get; set; }

            public string LotID { get; set; }

            public string ProcessID { get; set; }

            public string VORNR { get; set; }

            public string UnitState { get; set; }

            public string Sign { get; set; }

        }


    }
}
