using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace AgencySystem
{
    class OurStoresLayout : DatabaseLayout
    {
        public class OurStores
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string StreetName { get; set; }
            public int StreetNumber { get; set; }
            public string City { get; set; }
        }

        /// <summary>
        /// Function to get all our stores from database
        /// </summary>
        /// <returns></returns>
        public static OurStores[] GetOurStoresFromDB()
        {
            OurStores[] allStores = null;
            try
            {
                ConnectionToDatabaseOpen();
                MySqlCommand GetOurStores = myConnection.CreateCommand();
                GetOurStores.Connection = myConnection;
                GetOurStores.CommandText = "SELECT * FROM OurStores";

                using (MySqlDataReader reader = GetOurStores.ExecuteReader())
                {
                    var list = new List<OurStores>();
                    while (reader.Read())
                    {
                        list.Add(new OurStores
                        {
                            Id = Convert.ToInt16(reader["Id"]),
                            Name = reader["Name"].ToString(),
                            StreetName = reader["StreetName"].ToString(),
                            StreetNumber = Convert.ToInt16(reader["StreetNumber"]),
                            City = reader["City"].ToString()
                        });
                    }
                    allStores = list.ToArray();
                }
            }
            catch (Exception e)
            {
                Default.ErrorReporting(e);
            }
            finally
            {
                ConnectionToDatabaseClose();
            }
            return allStores;
        }
    }
}
