package eu.xeppaka.scalalearn

import java.util.Currency

import org.bson.codecs.configuration.CodecRegistries.{fromCodecs, fromProviders, fromRegistries}
import org.bson.codecs.{Codec, DecoderContext, EncoderContext}
import org.bson.{BsonReader, BsonWriter}
import org.mongodb.scala.MongoClient
import org.mongodb.scala.bson.codecs.DEFAULT_CODEC_REGISTRY
import org.mongodb.scala.bson.codecs.Macros._

import scala.concurrent.Await
import scala.concurrent.duration._

object Main9 {
  case class Id(id: String)
  case class Value(data: Int, text: String)
  case class Data(values: Map[String, Value])

  def main(args: Array[String]): Unit = {
    val codecRegistry = fromRegistries(fromProviders(classOf[Id], classOf[Value], classOf[Data]), DEFAULT_CODEC_REGISTRY)
    val client = MongoClient()
    val db = client.getDatabase("db-0002").withCodecRegistry(codecRegistry)
    val coll = db.getCollection[Data]("coll-0001")

    val d = Data(Map("111" -> Value(1, "111"), "222" -> Value(2, "222")))
    //Await.result(coll.insertOne(d).toFuture(), 2.seconds)

//    Await.result(fieldColl.insertOne(Class23()).toFuture(), 1.second)
//    Await.result(accounts.insertOne(Event("001", 1, Class23())).toFuture(), 1.second)
//    Await.result(accounts.insertOne(Event("001", 1, AccountV1("000001", 30, Value(10)))).toFuture(), 1.second)
//    Await.result(accounts.insertOne(Event("001", 2, AccountV2("000002", 20, "EUR"))).toFuture(), 1.second)

    val res = Await.result(coll.find().toFuture(), 1.second)
    res.foreach(println)

    client.close()
  }
}
