using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MongoDB.Driver;
using MongoDB.Bson;

namespace Mongo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
         
        private const string connectionString = "mongodb://localhost:27017";
        MongoClient mong = new MongoClient(connectionString);
        


        private void btnAfficher_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
           
            IMongoDatabase db = mong.GetDatabase("Kba");
            IMongoCollection<BsonDocument> cl = db.GetCollection<BsonDocument>("joueurs");

            var filter = Builders<BsonDocument>.Filter.Empty;
            var res = cl.Find(filter).ToList();
            foreach (var re in res)
            {
                listBox1.Items.Add(re);
            }
        }
             
        private void btnInser1_Click(object sender, EventArgs e)
        {

            IMongoDatabase db = mong.GetDatabase("Kba");
            IMongoCollection<BsonDocument> cl = db.GetCollection<BsonDocument>("joueurs");
            int id = int.Parse(textId.Text.ToString());
            string alias = textAliass.Text;
            int solde = int.Parse(textSolde.Text.ToString());
            string nom = textNom.Text;
            string force = textForce.Text;
            string arme = textArme.Text;
            var documnt = new BsonDocument

            {
                { "_id", id},
                { "alias",alias},
                { "solde", solde},

                { "mechant" , new BsonDocument {
                                                  {"nom",nom},
                                                  { "force", force},
                                                  { "arme", arme},
                                                 
                                               }
                }
            };
                                   
             cl.InsertOne(documnt);
        }

  

        private void butnchercher_Click_1(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            string varalias = textAlias.Text;
            IMongoDatabase db = mong.GetDatabase("Kba");

            IMongoCollection<BsonDocument> cl = db.GetCollection<BsonDocument>("joueurs");

            var filter = Builders<BsonDocument>.Filter.Eq("alias", varalias);
            var res1 = cl.Find(filter).ToList();

            foreach (var re in res1)
            {
                listBox1.Items.Add(re);
            }
        }

        private void rechercher2_Click(object sender, EventArgs e)
        {

            string mechant = textMechant.Text;
            IMongoDatabase db = mong.GetDatabase("Kba");
            IMongoCollection<BsonDocument> cl = db.GetCollection<BsonDocument>("joueurs");

            var filter = Builders<BsonDocument>.Filter.Eq("mechant.nom", mechant);
            var res1 = cl.Find(filter).ToList();

            foreach (var re in res1)
            {
                listBox1.Items.Add(re);
            }

        }
    }
}
