using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Globalization;

namespace Baza_Reklam
{
    /// <summary>
    /// Klasa pobiera numry wydan na ten tydzien.
    /// </summary>
    class Global
    {       

        private static Global global;

        private Global() {
            pobierzAktualneNrWydan();
        }

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

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

        public int NrAMT
        {
            get { return nrAMT; }
        }
           
        public int NrGS1
        {
            get { return nrGS1; }
        }
        
        public int NrGS2
        {
            get { return nrGS2; }
        }

        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();

            this.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();

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

            cm.Connection.Close();

            this.nrGS2 = this.nrGS1 + 1;

        }
    }
}
