/// <summary>
 /// This is a function in <see cref="CardLock"/>. It will check if a Card (stored as <see cref="Record"/>) is locked.
 /// </summary>
 /// <param name="record"><see cref="Record"/></param>
 /// <returns>True if the Card is locked.</returns>
 public static bool iscardlocked(Record record)
 {
     foreach(Record r in lockedcards)
     {
         if(r.kartenNummer == record.kartenNummer)
         {
             return true;
         }
     }
     record.kommen = DateTime.Now.ToString();
     lockedcards.Add(record);
     return false;
 }
        /// <summary>
        /// A function in <see cref="ExternalDB"/> to check if a specific <see cref="Record"/> is already in the database.
        /// </summary>
        /// <param name="connection"><see cref="SqlConnection"/></param>
        /// <param name="record"><see cref="Record"/> to transmit</param>
        /// <returns>Returns true if there is no Entry.</returns>
        public static bool CheckEntry(SqlConnection connection, Record record)
        {
            Config config = ConfigReader.getConfig();
            string query = "SELECT * FROM " +config.ext_zeitbuchungen+" WHERE ID = " +record.kartenID+ " StudentID = " + record.studentID + " AND Gehen = \"" + record.gehen + "\" and Kommen = \"" + record.kommen + "\" and ReaderIDGehen = " + record.readerIDGehen.ToString() + " and ReaderIDKommen = " + record.readerIDKommen.ToString() + " and KartenNummer = \"" + record.kartenNummer + "\"";

            if (OpenConnection(connection) == true)
            {
                SqlCommand cmd = new SqlCommand(query, connection);
                SqlDataReader dataReader = cmd.ExecuteReader();
                return !dataReader.HasRows;

            }

            CloseConnection(CreateConnString());

            return false;
        }
 public static void test()
 {
     //Testfunction, do whatever you want
     Record record = new Record("1A4E6B08", 1, DateTime.Now.ToString());
     record.completeRecord(1, DateTime.Now.ToString());
     record.studentID = "123";
     MS_ExternalDB.Transmit(MS_ExternalDB.CreateConnString(), record);
 }
 /// <summary>
 /// A function in <see cref="ExternalDB"/> to check if a specific <see cref="Record"/> is already in the table, if not it will be added to the table.
 /// </summary>
 /// <param name="connection"><see cref="SqlConnection"/></param>
 /// <param name="record"><see cref="Record"/> to transmit</param>
 public static void Transmit(SqlConnection connection, Record record)
 {
     // CHANGE TO CHECKENTRY AGAIN!!!
     if (CheckEntry(connection,record))
     {
         Insert(connection, record);
     }
 }
        /// <summary>
        /// A function in <see cref="ExternalDB"/> to add a <see cref="Record"/> to <see cref="SqlConnection"/>.
        /// </summary>
        /// <param name="connection"><see cref="SqlConnection"/></param>
        /// <param name="record"><see cref="Record"/> to transmit</param>
        public static void Insert(SqlConnection connection, Record record)
        {
            Config config = ConfigReader.getConfig();
            string query = "INSERT INTO " + config.ext_zeitbuchungen + " (KartenID,KartenNummer, StudentID, ReaderIDKommen, Kommen, ReaderIDGehen, Gehen, Erledigt, Gueltig) VALUES(" + "'" + record.kartenID + "'," + "'" + record.kartenNummer + "'," + "'" + record.studentID + "'," + "'" + record.readerIDKommen.ToString() + "'," + "'" + record.kommen + "'," + "'" + record.readerIDGehen.ToString() + "'," + "'" + record.gehen + "'," + "'" + record.erledigt.ToString() + "'," + "'" + record.gueltig.ToString() + "'" + ")";

            try
            {
                //string query = "INSERT INTO " + config.ext_zeitbuchungen + " (ID) VALUES(" +"'" +record.kartenID + "'"+")";
                if (OpenConnection(connection) == true)
                {
                    //create command and assign the query and connection from the constructor
                    SqlCommand cmd = new SqlCommand(query, connection);
                    //Execute command
                    cmd.ExecuteNonQuery();
                    //close connection
                    CloseConnection(connection);
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }
        }
 /// <summary>
 /// Function in <see cref="LocalDB"/> to transmit a single <see cref="Record"/>.
 /// </summary>
 /// <param name="client">Target <see cref="MyCouchClient"/></param>
 /// <param name="record"><see cref="Record"/> to transmit</param>
 public static async void Transmit(MyCouchClient client, Record record)
 {
     MyCouch.Requests.PostEntityRequest<Record> insert = new MyCouch.Requests.PostEntityRequest<Record>(record);
     MyCouch.Responses.EntityResponse<Record> reponse = await client.Entities.PostAsync(insert);
 }
        /// <summary>
        /// Function in <see cref="LocalDB"/> to check a <see cref="Record"/> "KartenNummer" in the <see cref="MyCouchClient"/> Database. "Valid" will be true when the KartenNummer is valid.
        /// </summary>
        /// <param name="record"><see cref="Record"/> to check.</param>
        /// <param name="valid">True if <see cref="Record"/> is valid</param>
        /// <returns>Returns the modified <see cref="Record"/>. When Card is OK it will add the <see cref="Record.studentID"/>, <see cref="Record.kartenID"/> and <see cref="Record"/></returns>
        public static Record checkCardnumber(Record record, out bool valid)
        {
            MyCouchClient client = LocalDB.ClientBuilder(ConfigReader.getConfig(), true);
            //Check the number
            #region QueryViewRequest
            MyCouch.Requests.QueryViewRequest query = new MyCouch.Requests.QueryViewRequest("view","KartenNummer");
            query.Configure(cfg => cfg.Key(record.kartenNummer));
            var response = client.Views.QueryAsync<Card>(query);

            #endregion
            try
            {
                if (response.Result.TotalRows > 0)
                {
                    //Get content-add StudentID to Record
                    record.studentID = response.Result.Rows[0].Value.studentID;
                    record.kartenID = response.Result.Rows[0].Value.id;
                    valid = true;
                    return record;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Invalid Cardnumber!");
                Console.WriteLine(record.kartenNummer);
                Notification.playsound(4);
            }
            valid = false;
            return record;

            // return null;
        }
        /// <summary>
        /// Function in <see cref="LocalDB"/> to check a <see cref="Record"/> in the LocalDB.
        /// </summary>
        /// <param name="KartenNummer"><see cref="Record.kartenNummer"/> to check.</param>
        /// <param name="record">Returns the edited <see cref="Record"/></param>
        /// <returns>true when there is exactly one unfinished Record. False when there are more then one or no unfinished Records.</returns>
        public static bool checkRecords(string KartenNummer, out Record record)
        {


            #region QueryViewRequest

            MyCouch.Requests.QueryViewRequest query = new MyCouch.Requests.QueryViewRequest("view","erledigt");
            query.Configure(cfg => cfg.Key(KartenNummer));
            var response = LocalDB.ClientBuilder(ConfigReader.getConfig()).Views.QueryAsync<Record[]>(query);
            #endregion

            if (response.Result.TotalRows == 1)
            {
                MyCouch.Responses.EntityResponse<Record> recordresponse = copyEntry(response.Result.Rows[0].Id);
                record = recordresponse.Content;
                deleteRecord(recordresponse.Id, recordresponse.Rev);
                
                return true;
            }
            else if (response.Result.TotalRows > 1) //should not happen right now! Same as 0 TotalRows
            {
                Console.WriteLine(response.Result.TotalRows + " Records not finished!");
                record = null;
                
                return false;
            }
            else
            {
                record = null;
                
                return false;
            }
        }
 /// <summary>
 /// A function in <see cref="ExternalDB"/> to check if a specific <see cref="Record"/> is already in the table, if not it will be added to the table.
 /// </summary>
 /// <param name="connection"><see cref="MySqlConnection"/></param>
 /// <param name="record"><see cref="Record"/> to transmit</param>
 public static void Transmit(MySqlConnection connection, Record record)
 {
     if (CheckEntry(connection, record) == true)
     {
         Insert(connection, record);
     }
 }
        /// <summary>
        /// A function in <see cref="ExternalDB"/> to add a <see cref="Record"/> to <see cref="MySqlConnection"/>.
        /// </summary>
        /// <param name="connection"><see cref="MySqlConnection"/></param>
        /// <param name="record"><see cref="Record"/> to transmit</param>
        public static void Insert(MySqlConnection connection, Record record)
        {
            Config config = ConfigReader.getConfig();
            string query = "INSERT INTO "+config.ext_zeitbuchungen+" (ID, KartenNummer, StudentID, ReaderIDKommen, Kommen, ReaderIDGehen, Gehen, Erledigt, Gueltig) VALUES(\"" + record.kartenNummer.ToString() + "\"," +record.kartenID+ "\"," + record.studentID + "\"," + record.readerIDKommen.ToString() + "\"," + record.kommen + "\"," + record.readerIDGehen.ToString() + "\"," + record.gehen + "\"," + record.erledigt + "\"," + record.gueltig + ")";

            if (OpenConnection(connection) == true)
            {
                //create command and assign the query and connection from the constructor
                MySqlCommand cmd = new MySqlCommand(query, connection);
                //Execute command
                cmd.ExecuteNonQuery();
                //close connection
                CloseConnection(connection);
            }
        }