using System;
using System.Data.SqlClient;
using System.Globalization;

namespace Baza_Reklam
{
    /// <summary>
    /// Klasa pobiera numry wydan na ten tydzien.
    /// </summary>
    internal class Global
    {
        private static Global global;

        //aktualny nr AMT
        private int nrAMT;
        //nr GS z tego tygodnia
        private int nrGS1;
        private int nrGS2;

        private Global()
        {
            pobierzAktualneNrWydan();
        }

        public int NrAMT
        {
            get { return nrAMT; }
        }

        public int NrGS1
        {
            get { return nrGS1; }
        }

        public int NrGS2
        {
            get { return nrGS2; }
        }

        public static Global getGlobal()
        {
            if (global == null)
            {
                global = new Global();
            }
            return global;
        }

        private void pobierzAktualneNrWydan()
        {
            DateTime dzisiaj = DateTime.Today;
            CultureInfo myCI = new CultureInfo("pl-PL");
            Calendar myCal = myCI.Calendar;

            int nrTygodnia = myCal.GetWeekOfYear(dzisiaj, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
            int pom = nrTygodnia;
            while (nrTygodnia == pom)
            {
                dzisiaj = dzisiaj.Subtract(new TimeSpan(1, 0, 0, 0));
                pom = myCal.GetWeekOfYear(dzisiaj, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
            }

            DateTime poczatek = dzisiaj.AddDays(1);
            DateTime koniec = dzisiaj.AddDays(7);

            SqlCommand cm = new SqlCommand();
            cm.CommandText =
                "select nrw from dbo.NR where @poczatek <= DATA_W AND DATA_W <= @koniec AND tyt='AMT' order by nrw";
            cm.Parameters.AddWithValue("@poczatek", poczatek);
            cm.Parameters.AddWithValue("@koniec", koniec);

            cm.Connection = new SqlConnection(ConnString.getConnString().Value);

            cm.Connection.Open();

            nrAMT = Convert.ToInt32(cm.ExecuteScalar());

            cm.Connection.Close();

            cm.CommandText =
                "select nrw from dbo.NR where @poczatek <= DATA_W AND DATA_W <= @koniec AND tyt='GS' order by nrw";

            cm.Connection.Open();

            nrGS1 = Convert.ToInt32(cm.ExecuteScalar());

            cm.Connection.Close();

            nrGS2 = nrGS1 + 1;
        }
    }
}