using System;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;

public partial class login : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        if (Session["culture"] != null)
        {
            UICulture = Session["culture"].ToString();
            Culture = Session["culture"].ToString();

            Thread.CurrentThread.CurrentCulture =
              CultureInfo.CreateSpecificCulture(Session["culture"].ToString());
            Thread.CurrentThread.CurrentUICulture =
              new CultureInfo(Session["culture"].ToString());
        }
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //throw new ApplicationException("TEST");


        FakturaImageButton.ImageUrl = Resources.Common.Faktura;
        if (!IsPostBack) { SetFocus(NIPTextBox); }
    }

    protected void ZalogujButton_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid) return;
        string nip = NIPTextBox.Text.Replace(" ", string.Empty).Replace("-", string.Empty);
        string nrZlec = NrZlecTextBox.Text.Replace(" ", string.Empty);

        bool czyZalogowany = Zaloguj(nip, nrZlec);
        if (czyZalogowany)
        {
            FormsAuthentication.RedirectFromLoginPage(nip, false);
        }
        else
        {
            errorMsg.Text = GetLocalResourceObject("errorMsg").ToString();
        }
    }

    bool Zaloguj(string nip, string nrFaktury)
    {
        if (nrFaktury.Split('/').Length != 3) return false;

        string fakturaNr = nrFaktury.Split('/')[0];
        string fakturaRoz = nrFaktury.Split('/')[1];
        string fakturaRok = nrFaktury.Split('/')[2];

        string connString = ConfigurationManager.ConnectionStrings["BazaReklamConn"].ConnectionString;
        const string cmdText = "SELECT ID_Faktury FROM dbo.vDanePlatnosciEcard WHERE nip=@nip AND [Numer]=@numer AND [Numer_Roz]=@numerRoz AND [Numer_Rok]=@numerRok";
        bool znalezione = false;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            SqlCommand cmd = new SqlCommand(cmdText, conn);
            cmd.Parameters.Add("@nip", SqlDbType.NVarChar, 50).Value = nip;
            cmd.Parameters.Add("@numer", SqlDbType.NVarChar, 10).Value = fakturaNr;
            cmd.Parameters.Add("@numerRoz", SqlDbType.NVarChar, 10).Value = fakturaRoz;
            cmd.Parameters.Add("@numerRok", SqlDbType.NVarChar, 10).Value = fakturaRok;
            conn.Open();

            SqlDataReader reader = cmd.ExecuteReader();
            if (reader == null)
            {
                conn.Close();
                return false;
            }

            if (reader.Read())
            {
                Session["Nip"] = nip;
                Session["NrFaktury"] = nrFaktury;
                Session["IdFaktury"] = reader.GetInt32(0);
                znalezione = true;
            }
            reader.Close();
            reader.Dispose();
            conn.Close();
        }
        return znalezione;
    }
}