using System;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace NorthwindExample.ConsoleApp {
    internal static class Program {
        private static void Main() {
            try {
                var cfg = new Configuration();
                cfg.Configure();
                SchemaExport exporter = new SchemaExport(cfg);
                exporter.Execute(true, true, false, false);
                ISessionFactory factory = cfg.BuildSessionFactory();
                using(ISession session = factory.OpenSession()) {
                    using(session.BeginTransaction()) {
//                        LoadCustomer(session);
//                        GetCustomer(session);
//                        GetCustomers(session);
//                        SaveCustomer(session);
//                        AddOrderToCustomer(session, "JAMES");
                        session.Transaction.Commit();
                    }
                }
            } catch(Exception e) {
                Console.WriteLine(e);
            }
            Console.WriteLine("Press <ENTER> to continue...");
            Console.ReadLine();
        }

        private static void LoadCustomer(ISession session) {
            Customer c = session.Load<Customer>("ALFKI");
            Console.WriteLine("{0}: {1}", c.CustomerID, c.CompanyName);
            Console.WriteLine("City: {0}", c.Location.City);
            Console.WriteLine("# of Order: {0}", c.Orders.Count);
        }

        private static void GetCustomer(ISession session) {
            Customer c = session.Get<Customer>("ALFKI2");
            if(c != null) {
                DisplayCustomer(c);
            } else {
                Console.WriteLine("Unknown customer");
            }
        }

        private static void DisplayCustomer(Customer c) {
            Console.WriteLine("{0}: {1}", c.CustomerID, c.CompanyName);
        }

        private static void GetCustomers(ISession session) {
            IQuery query = session.CreateQuery("select c, size(c.Orders) from Customer c where size(c.Orders) = 0 and c.CompanyName like 'Paris%'");
            foreach(Customer c in query.Enumerable()) {
                DisplayCustomer(c);
            }
        }

        private static void SaveCustomer(ISession session) {
            session.Delete("from Customer where CustomerID = 'JAMES'");
            Customer c = GetAndDisplayCustomer(session, "JAMES");

            c = new Customer("JAMES", "JamesKovacs.com Consulting Inc.", null);
            session.Save(c);
            Customer c2 = GetAndDisplayCustomer(session, "JAMES");

            c2.CompanyName = "JK.com";
            Customer c3 = GetAndDisplayCustomer(session, "JAMES");
        }

        private static void AddOrderToCustomer(ISession session, string customerId) {
            Customer c = session.Get<Customer>(customerId);
            c.AddOrder(DateTime.Today);
        }

        private static Customer GetAndDisplayCustomer(ISession session, string key) {
            Customer c = session.Get<Customer>(key);
            if(c != null) {
                DisplayCustomer(c);
            } else {
                Console.WriteLine("Customer does not exist");
            }
            return c;
        }
    }
}