using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Baza_Reklam.Classes.Helpers
{
    public class ReklamaHelper
    {
        public static bool IsReklamaNew(REKLAMADataSet.ReklamyZestawienieRow reklama)
        {
            if (!reklama.IsidZamowieniaNull()) return true;

            return reklama.DATA_ZAMÓWIENIA.Year >= 2009;
        }

        public static bool IsReklamaNew(REKLAMADataSet.REKLAMARow reklama)
        {
            if (!reklama.IsidZamowieniaNull()) return true;

            return reklama.DATA_ZAMÓWIENIA.Year >= 2009;
        }

        public static bool IsReklamaOldExpo(REKLAMADataSet.REKLAMARow reklama)
        {
            return (reklama.IsidZamowieniaNull() && reklama.TYTUŁ.ToUpper() == "EXPO");
        }

        public static Dictionary<int, DateTime> GetDatyEmisji(int reklamaId)
        {
            Dictionary<int, DateTime> datyEmisji = new Dictionary<int, DateTime>();

            const string query = "sp_GetDatyEmisjiByReklama";

            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader reader = null;
            try
            {
                conn = new SqlConnection(ConnString.getConnString().Value);
                conn.Open();
                cmd = new SqlCommand(query, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@reklama", reklamaId);
                reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                        datyEmisji.Add(reader.GetInt32(0), reader.GetDateTime(1));
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }

            }
            return datyEmisji;

        }

        public static void AddUpdateIssueChange(double issueId, int currentIssueNumer, int newIssueNumber)
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;

            try
            {
                conn = new SqlConnection(ConnString.getConnString().Value);
                conn.Open();
                cmd = new SqlCommand("sp_AddUpdateIssueChange", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@issueId", issueId);
                cmd.Parameters.AddWithValue("@currentIssueNumer", currentIssueNumer);
                cmd.Parameters.AddWithValue("@newIssueNumber", newIssueNumber);
                cmd.ExecuteNonQuery();
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();

                if (conn != null && conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }

        }
    }
}