using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using AutoMapper.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SQLite;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTestSQLiteHelper
    {
        [TestMethod]
        public void TestMethodSave()
        {
            Exception ex = null;
            try
            {
                var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
                var cfg = new AutoMapper.Configuration.MapperConfigurationExpression();
                cfg.AddProfiles("UnitTestProject1");
                AutoMapper.Mapper.Initialize(cfg);

                Model.DBModel dBModel = new Model.DBModel("test.sqlite3");
                dBModel.Init();

                var books_db = new List<Model.Book>
            {
                new Model.Book()
                {
                    ID = dBModel.Books.FreeID,
                    BookName = "ASP.NET MVC 5 高级编程(第5版)",
                    PageCount = 460,
                    PrintTime = new DateTime(2019,8,8)
                },
                new Model.Book()
                {
                    ID = dBModel.Books.FreeID,
                    BookName = "c# 入门经典(第7版）",
                    PageCount = 432,
                    PrintTime = new DateTime(2012,5,20)
                },
            };
                var users_lc = new List<Model.UserLC>
            {
                new Model.UserLC(){
                    ID =  dBModel.Users.FreeID,
                    Name = "潘栩锋",
                    BookIDs = new List<long>{
                        books_db[0].ID,books_db[1].ID
                    }
                }
            };
                var users_db = AutoMapper.Mapper.Map<List<Model.User>>(users_lc);

                List<string> sqls = new List<string>();
                foreach (var book_db in books_db)
                    sqls.Add(SQLiteHelper.GetInsertCommandText(book_db));

                foreach (var user_db in users_db)
                    sqls.Add(SQLiteHelper.GetInsertCommandText(user_db));

                dBModel.sqliteHelper.QueryTranAsync(sqls);
            }
            catch (Exception e)
            {
                ex = e;
            }
            Assert.IsNull(ex);
        }

        [TestMethod]
        public void TestMethodLoad()
        {
            Exception ex = null;
            try
            {
                var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
                var cfg = new AutoMapper.Configuration.MapperConfigurationExpression();
                cfg.AddDataReaderMapping();
                cfg.AddProfiles("UnitTestProject1");
                AutoMapper.Mapper.Initialize(cfg);

                Model.DBModel dBModel = new Model.DBModel("test.sqlite3");
                dBModel.Init();

                string sql =
                    $"SELECT * FROM {dBModel.Users.TableName}" +
                    $" ORDER BY ID";
                var table = dBModel.sqliteHelper.ExecuteReader(sql);
                Stopwatch stopwatch = new Stopwatch();

                stopwatch.Start();
                var users_db = SQLiteHelper.ToObjs<Model.User>(table);
                stopwatch.Stop();
                Console.WriteLine($"SQLiteHelper.ToObjs<Model.User>(table) Elapsed={stopwatch.ElapsedMilliseconds}ms");

                stopwatch.Restart();
                var users_db2 = AutoMapper.Mapper.Map<IDataReader, List<Model.User>>(table.CreateDataReader());
                stopwatch.Stop();
                Console.WriteLine($"AutoMapper.Mapper.Map<IDataReader,List<Model.User>>(table.CreateDataReader()) Elapsed={stopwatch.ElapsedMilliseconds}ms");

                var users_lc = AutoMapper.Mapper.Map<List<Model.UserLC>>(users_db);


                sql =
                    $"SELECT * FROM {dBModel.Books.TableName}" +
                    $" ORDER BY ID";
                table = dBModel.sqliteHelper.ExecuteReader(sql);

                stopwatch.Restart();
                var books_db = SQLiteHelper.ToObjs<Model.Book>(table);
                stopwatch.Stop();
                Console.WriteLine($"SQLiteHelper.ToObjs<Model.Book>(table) Elapsed={stopwatch.ElapsedMilliseconds}ms");

                stopwatch.Restart();
                var books_db2 = AutoMapper.Mapper.Map<IDataReader, List<Model.Book2>>(table.CreateDataReader());
                stopwatch.Stop();
                Console.WriteLine($"AutoMapper.Mapper.Map<IDataReader, List<Model.Book2>>(table.CreateDataReader()) Elapsed={stopwatch.ElapsedMilliseconds}ms");

                string json = Newtonsoft.Json.JsonConvert.SerializeObject(books_db, Newtonsoft.Json.Formatting.Indented);
                Console.WriteLine(json);

            }
            catch (Exception e)
            {

                ex = e;
                Assert.Fail(e.Message);
            }
            Assert.IsNull(ex);
        }

        
    }
}
