
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using NDatabase;
using MvcWriter.Models;
using System.IO;
using NDatabase.Api.Query;
using NDatabase.Api;



namespace NDatabaseIO
{
    /// <summary>
    /// Local Server side object database API. Used for testing when real sql server is not available
    /// </summary>
    public class NWriterDbIO
    {
        public string dbFileName = "";


        /// <summary>
        /// Initializes a new instance of the <see cref="NDatabaseIO"/> class.
        /// </summary>
        public NWriterDbIO()
        {
            dbFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\Writer.db");
        }
        /// <summary>
        /// Saves the object to database.
        /// </summary>
        /// <param name="Obj">The obj.</param>
        /// <returns>True or False if succeeded or failed</returns>
        public bool SaveRecord(object Obj)
        {
            NDatabase.Api.OID I = null;
            using (var odb = OdbFactory.Open(dbFileName))
            {
                I = odb.Store(Obj);
            }
            return I != null;
        }

        /// <summary>
        /// Reads all records that match the type of the object passed in (ObjSource)
        /// </summary>
        /// <param name="ObjSource">The obj source.</param>
        /// <returns>ArrayList of the objects found</returns>
        public ArrayList ReadRecords(object ObjSource)
        {
            // retrieve them by classes and by interface
            ArrayList resultRecs = new ArrayList();
            using (var odb = OdbFactory.Open(dbFileName))
            {
                var recs = odb.Query<object>().Execute<object>();
                foreach (var obj in recs)
                {
                    if (ObjSource.GetType() == obj.GetType())
                        resultRecs.Add(obj);
                }
            }
            return resultRecs;
        }

        /// <summary>
        /// Deletes the record.
        /// </summary>
        /// <param name="Obj">The obj.</param>
        public void DeleteRecord(object Obj)
        {
            using (var odb = OdbFactory.Open(dbFileName))
            {
                odb.Delete(Obj);
            }
        }
    }


    public class WriterData 
    {

        static string  dbFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\Writer.db");


        public static void IntializeModel()
        {
            WriterModel model = new WriterModel();
            model.writerID = 1001;
            model.Code = "FNFCODE1";
            model.Name = "Fidelity National Financial";
            model.IsActive = true;

            List<Agent> agents = new List<Agent>();

            Agent agent = new Agent();
            agent.AgentID = "101";
            agent.Name = "Chat";
            agent.IsActive = false;
            agents.Add(agent);




            agent = new Agent();
            agent.AgentID = "102";
            agent.Name = "Chatra";
            agent.IsActive = true;
            agents.Add(agent);

            model.Agents = agents;


            using (var odb = OdbFactory.Open(dbFileName))
            {
                // Store the object
                odb.Store(model);
            }

            model = new WriterModel();
            model.writerID = 1002;
            model.Code = "FACCode2";
            model.Name = "First American Corporation";
            model.IsActive = true;

            agents = new List<Agent>();

            agent = new Agent();
            agent.AgentID = "103";
            agent.Name = "patt";
            agent.IsActive = true;
            agents.Add(agent);

            agent = new Agent();
            agent.AgentID = "104";
            agent.Name = "pati";
            agent.IsActive = true;
            agents.Add(agent);


            model.Agents = agents;

            using (var odb = OdbFactory.Open(dbFileName))
            {
                // Store the object
                odb.Store(model);
            }

            model = new WriterModel();
            model.writerID = 1003;
            model.Code = "FACCode3";
            model.Name = "First American Corporation";
            model.IsActive = false;

            agents = new List<Agent>();

            agent = new Agent();
            agent.AgentID = "105";
            agent.Name = "patt";
            agent.IsActive = false;

            agents.Add(agent);

            agent = new Agent();
            agent.AgentID = "106";
            agent.Name = "pati";
            agent.IsActive = true;

            agents.Add(agent);

            model.Agents = agents;

            using (var odb = OdbFactory.Open(dbFileName))
            {
                // Store the object
                odb.Store(model);
            }


        }
        /// <summary>
        /// Saves the record.
        /// </summary>
        /// <param name="Record">The record.</param>
        /// <returns>True or False</returns>
        //public bool SaveWriter(WriterModel Writer)
        //{
        //    return base.SaveRecord(Writer);
        //}

        /// <summary>
        /// Deletes the record.
        /// </summary>
        /// <param name="Record">The record</param>
        public void DeleteWriter(int WriterId)
        {
            using (var odb = OdbFactory.Open(dbFileName))
            {
                IQuery query = odb.Query<WriterModel>();
                query.Descend("ID").Constrain(WriterId).Equal();

                IObjectSet<WriterModel> result = query.Execute<WriterModel>();
                WriterModel Writer = ((WriterModel)result.GetFirst());
                odb.Delete(Writer);
            }
        }

        /// <summary>
        /// Updates the record.
        /// </summary>
        /// <param name="record">The record.</param>
        public static bool UpdateWriter(int WriterId, WriterModel NewWriter)
        {
            NDatabase.Api.OID I = null;
            using (var odb = OdbFactory.Open(dbFileName))
            {
                // read existin Writer in
                IQuery query = odb.Query<WriterModel>();
                query.Descend("ID").Constrain(WriterId).Equal();
                IObjectSet<WriterModel> result = query.Execute<WriterModel>();
                WriterModel ExistingWriter = ((WriterModel)result.GetFirst());

                ExistingWriter.Code = NewWriter.Code;
                ExistingWriter.Name = NewWriter.Name;
                ExistingWriter.IsActive = ExistingWriter.IsActive;
                I = odb.Store(ExistingWriter);
            }
            return I != null;
        
        }


        public static List<WriterModel> ReadWriters()
        {
            //IntializeModel();//only one time run for Ndatabase insert
            // retrieve them by classes and by interface
            List<WriterModel> resultRecs = new List<WriterModel>();
            using (var odb = OdbFactory.Open(dbFileName))
            {
                var Writers = odb.QueryAndExecute<WriterModel>();
                foreach (var obj in Writers)
                {
                  
                        resultRecs.Add(obj);
                }
            }
            return resultRecs;
        }

     

    }

}