using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Collections.ObjectModel;
using Maser.Feanor.Model;
using System.Data.SqlClient;
using System.Drawing;



/*
Table Chambers in DB should has fields:
    int             PK
    int             MIDS
    varchar(255)    Description
    varchar(15)     Network
    bit             Active
*/


namespace Maser.Feanor.Biz
{

    public class Chambers
    {
        public Chamber GetByPK(Int32 PK)
        {
            string query = String.Format("Select * from Chambers where PK = {0}", PK);
            try
            {
                return GetByQuery(query);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public Chamber GetByMIDS(Int32 MIDS)
        {
            string query = String.Format("Select * from Chambers where MIDS = {0}", MIDS);
            try
            {
                return GetByQuery(query);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public List<Chamber> GetAllActive()
        {
            string query = "Select * from Chambers where Active = 1 ORDER by MIDS ASC";
            try
            {
                return GetListByQuery(query);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public List<Chamber> GetAll()
        {
            string query = "Select * from Chambers ORDER by MIDS ASC";
            try
            {
                return GetListByQuery(query);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public Boolean Delete(Chamber chamber)
        {
            try
            {
                chamber.Active = false;
                return Modify(chamber);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Exists(Chamber chamber)
        {
            try
            {
                return (GetByPK(chamber.PK) != null);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        
        public bool Modify(Chamber chamber)
        {
            string query = String.Format("UPDATE Chambers SET MIDS='{0}', Description='{1}', Interval='{2}', Network='{3}', ServerType='{4}', Active='{5}', RS485='{6}', HumidityChamber='{7}' WHERE PK={8};", chamber.MIDS, chamber.Description,chamber.Interval, chamber.Network, (int)chamber.ServerType, chamber.Active,chamber.RS485, chamber.Humidity, chamber.PK);

            try
            {
                SqlConnection myConnection = new SqlConnection(Properties.Settings.Default.FeanorConnStr);
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand(query, myConnection);
                myCommand.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        public Int32 Add(Chamber chamber)
        {
            string query = String.Format("INSERT INTO Chambers (MIDS, Description, Interval, Network, ServerType, Active, RS485, HumidityChamber) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}');", chamber.MIDS, chamber.Description, chamber.Interval, chamber.Network, (int)chamber.ServerType, chamber.Active, chamber.RS485, chamber.Humidity);
            query += "SELECT SCOPE_IDENTITY();";

            try
            {
                SqlConnection myConnection = new SqlConnection(Properties.Settings.Default.FeanorConnStr);
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand(query, myConnection);
                return Int32.Parse(myCommand.ExecuteScalar().ToString());
            }
            catch (Exception e)
            {
                throw e;
            }
        }



        private Chamber GetByQuery(string query)
        {
            Chamber result = null;
            SqlConnection myConnection = new SqlConnection(Properties.Settings.Default.FeanorConnStr);
            SqlDataReader myReader = null;
            try
            {
                myConnection.Open();

                SqlCommand myCommand = new SqlCommand(query, myConnection);
                myReader = myCommand.ExecuteReader();
                if (myReader.HasRows)
                {
                    myReader.Read();
                    result = new Chamber(myReader.GetInt32(0), myReader.GetInt32(1), myReader.GetString(2), myReader.GetInt32(3), myReader.GetString(4), (ServerType)myReader.GetInt32(5), myReader.GetBoolean(6), myReader.GetBoolean(7), myReader.GetBoolean(8));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                myReader.Close();
                myConnection.Close();
                /* 
                https://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx
                Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. 
                In a finalizer, only release unmanaged resources that your class owns directly. 
                If your class does not own any unmanaged resources, do not include a Finalize method in your class definition. 
                For more information, see Garbage Collection.
                */
            }
            return result;
        }

        private List<Chamber> GetListByQuery(string query)
        {
            List<Chamber> result = new List<Chamber>();
            SqlConnection myConnection = new SqlConnection(Properties.Settings.Default.FeanorConnStr);

            SqlDataReader myReader = null;
            try
            {
                myConnection.Open();

                SqlCommand myCommand = new SqlCommand(query, myConnection);
                myReader = myCommand.ExecuteReader();
                if (myReader.HasRows)
                {
                    while (myReader.Read())
                    {
                        result.Add(new Chamber(myReader.GetInt32(0), myReader.GetInt32(1), myReader.GetString(2), myReader.GetInt32(3), myReader.GetString(4), (ServerType)myReader.GetInt32(5), myReader.GetBoolean(6), myReader.GetBoolean(7), myReader.GetBoolean(8)));
                    }
                }
                myReader.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
               // myReader.Close();
                /* 
                https://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx
                Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. 
                In a finalizer, only release unmanaged resources that your class owns directly. 
                If your class does not own any unmanaged resources, do not include a Finalize method in your class definition. 
                For more information, see Garbage Collection.
                */
            }
            return result;
        }





    }


}
