using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Baza_Reklam.Classes.Model;

namespace Baza_Reklam.Classes.Helpers
{
    public struct AdSize
    {
        public decimal Width;
        public decimal Height;
    }

    public class AdHelper
    {
        //TODO: get the initial ad size, margin size and max width height from db
        public static AdSize GetAdSize(int horizontal, int vertical, Module module)
        {
            AdSize adSize = new AdSize();

            if (module == null)
            {
                adSize.Width = 0;
                adSize.Height = 0;
                return adSize;
            }

            if (!module.MultiModule)
            {
                adSize.Width = Convert.ToInt32(module.Width);
                adSize.Height = Convert.ToInt32(module.Height);
                return adSize;
            }

            decimal width = ((horizontal - 1) * module.MarginHorizontal) + (horizontal * module.Width.Value);
            decimal height = Convert.ToInt32(((vertical - 1) * module.MarginVertical) + (vertical * module.Height));
            
            adSize.Width = width;
            adSize.Height = height;
            return adSize;
        }

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

        public static bool IsOgloszenie(REKLAMADataSet.REKLAMARow reklama)
        {
            if (reklama == null) return false;

            return GetProjectType(reklama.TYTUŁ, reklama.MOD_TYP) == 2;
        }

        //TODO: find better method to find out if this is ogloszenie or reklama
        public static int GetProjectType(string title, string moduleName)
        {
            if (!title.StartsWith("AS-")) return 1;

            switch (moduleName.ToLower())
            {
                case "standard":
                case "billboard":
                case "space":
                    return 2;
                default:
                    return 1;
            }
        }
    }
}