using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;

public partial class _Default : 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)
    {
        if (string.IsNullOrEmpty(Session["IdFaktury"].ToString())) Response.Redirect("login.aspx");

        if (!IsPostBack)
        {
            string connString = ConfigurationManager.ConnectionStrings["BazaReklamConn"].ConnectionString;
            string cmdText = "SELECT SystemKsiegowyId, FirstName, LastName, Address, PostalCode, City, nip, Nabywca_nip, Brutto, Data_Wystawienia, Termin_zaplaty, Sposob_zaplaty, Imie, Nazwisko, ulica, kod, miasto, telefon, waluta_brutto, waluta_miano FROM dbo.vDanePlatnosciEcard WHERE ID_Faktury=@idFaktury";
            //string nip = (string)Session["nip"];
            //string nrZlec = (string)Session["nrFaktury"];
            int idFaktury = Int32.Parse(Session["IdFaktury"].ToString());
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(cmdText, conn);
                //cmd.Parameters.Add("@nip", SqlDbType.NVarChar, 50).Value = nip;
                cmd.Parameters.Add("@idFaktury", SqlDbType.Int).Value = idFaktury;

                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                reader.Read();

                // formaPlatnosci i waluta                
                string walBrutto = reader["waluta_brutto"].ToString(); //wartoscNaString(reader, 22, "double");
                string walMiano = reader["waluta_miano"].ToString(); // wartoscNaString(reader, 23, "string");
                string systemKsiegowy = reader["SystemKsiegowyId"].ToString(); // reader.GetByte(24).ToString();
                if (walMiano != "" && walBrutto != "")
                {
                    WyborWaluty.Enabled = true;
                    ArrayList values = new ArrayList();
                    values.Add("PLN");
                    values.Add(walMiano);
                    WyborWaluty.DataSource = values;
                    WyborWaluty.DataBind();
                }

                FormaPlatnosci = "karta";

                string firstName = reader["FirstName"].ToString();
                string lastName = reader["LastName"].ToString();
                string adres = reader["Address"].ToString();
                string kod = reader["PostalCode"].ToString();
                string miasto = reader["City"].ToString();
                string tNIP = reader["Nabywca_nip"].ToString();
                string tNrFaktury = Session["NrFaktury"].ToString();
                string tDataWystawienia = DateTime.Parse(reader["Data_Wystawienia"].ToString()).ToString("yyyy-MM-dd");
                string tTerminPlatnosci = DateTime.Parse(reader["Termin_zaplaty"].ToString()).ToString("yyyy-MM-dd");
                string tBrutto = reader["Brutto"].ToString();
                // Amount
                string AmountPLN = Convert.ToInt32(Convert.ToDecimal(reader["Brutto"])*100).ToString();
                string AmountWaluta = Convert.ToInt32(Convert.ToDecimal(reader["waluta_brutto"]) * 100).ToString();
                // tIDFaktury
                string tIDFaktury = Session["IdFaktury"].ToString();
                string ImieHandlowca = reader["Imie"].ToString(); //wartoscNaString(reader, 15, "string");
                string NazwiskoHandlowca = reader["Nazwisko"].ToString(); //wartoscNaString(reader, 16, "string");
                string AdresBiuraUlica = reader["Ulica"].ToString(); //wartoscNaString(reader, 17, "string");
                string AdresBiuraKod = reader["Kod"].ToString(); //wartoscNaString(reader, 18, "string");
                string AdresBiuraMiasto = reader["Miasto"].ToString(); //wartoscNaString(reader, 19, "string");
                string AdresBiuraTelefony = reader["Telefon"].ToString(); //wartoscNaString(reader, 20, "string");
                string WalutaBrutto = reader["waluta_brutto"].ToString(); //wartoscNaString(reader, 22, "double");
                string WalutaMiano = reader["Waluta_Miano"].ToString(); //wartoscNaString(reader, 23, "string");

                // ------------- Ustawienie zmiennych -----------------------
                kwotaPLN = tBrutto;
                kwotaWaluta = WalutaBrutto;
                kwotaGroszePLN = AmountPLN;
                kwotaGroszeWaluta = AmountWaluta;
                // ------------ Dane do wyświetlenia ------------------------

                // --------- Nazwa
                string nazwa;
                if (firstName == "")
                {
                    nazwa = lastName;
                }
                else
                {
                    nazwa = firstName + " " + lastName;
                }

                Nazwa.Text = nazwa;
                // --------- Adres
                Adres.Text = adres + ", " + kod + " " + miasto;
                // --------- NIP
                NIP.Text = tNIP;
                // -------- NrFaktury
                NrFaktury.Text = tNrFaktury;
                // --------- DataWystawienia
                DataWystawienia.Text = tDataWystawienia;
                // --------- TerminPlatnosci
                TerminPlatnosci.Text = tTerminPlatnosci;

                //------------- Informacje do kontaktu z handlowcem
                Handlowiec.Text = ImieHandlowca + " " + NazwiskoHandlowca;
                BiuroAdres.Text = AdresBiuraUlica + ", " + AdresBiuraKod + " " + AdresBiuraMiasto;
                BiuroTelefon.Text = AdresBiuraTelefony;

                // ---------------------- Dane przekazywane do sesji ------------

                // -------------- ORDERDESCRIPTION
                string ORDERDESCRIPTION = (string) Session["nrFaktury"];
                Session["ORDERDESCRIPTION"] = ORDERDESCRIPTION;
                // ---------------- ORDERNUMBER 
                string ORDERNUMBER = "1";
                Session["ORDERNUMBER"] = ORDERNUMBER;
                // ----------------- SESSIONID
                string SESSIONID = Session.SessionID;
                Session["SESSIONID"] = SESSIONID;
                // ------------- LANGUAGE PL - polski, EN - angielski, DE - niemiecki, FR - francuski, RU - rosyjski
                string LANGUAGE = "PL";

                Session["SystemKsiegowy"] = systemKsiegowy;

                if ((string) Session["CURRENCY"] == "978")
                {
                    Session["CURRENCY"] = "978";
                    Session["AMOUNT"] = kwotaGroszeWaluta;
                    Brutto.Text = kwotaWaluta + " €";
                    WyborWaluty.SelectedValue = "EUR";
                }
                else if ((string) Session["CURRENCY"] == "985")
                {
                    Session["CURRENCY"] = "985";
                    Session["AMOUNT"] = kwotaGroszePLN;
                    Brutto.Text = kwotaPLN + " zł";
                    WyborWaluty.SelectedValue = "PLN";
                }
                    //else if ((string)Session["CURRENCY"] == "826")
                    //{
                    //    Session["CURRENCY"] = "826";
                    //    Session["AMOUNT"] = kwotaGroszeWaluta;
                    //    Brutto.Text = kwotaWaluta + " £";
                    //    WyborWaluty.SelectedValue = "GBP";
                    //}
                else
                {
                    Session["CURRENCY"] = "985";
                    Session["AMOUNT"] = kwotaGroszePLN;
                    Brutto.Text = kwotaPLN + " zł";
                    WyborWaluty.SelectedValue = "PLN";
                }
                if (Thread.CurrentThread.CurrentCulture.Name == "pl-PL")
                {
                    Session["culture"] = "pl-PL";
                    LANGUAGE = "PL";
                }
                else if (Thread.CurrentThread.CurrentCulture.Name == "en-US")
                {
                    Session["culture"] = "en-US";
                    LANGUAGE = "EN";
                }
                else if (Thread.CurrentThread.CurrentCulture.Name == "fr-FR")
                {
                    Session["culture"] = "fr-FR";
                    LANGUAGE = "FR";
                }
                else if (Thread.CurrentThread.CurrentCulture.Name == "it-IT")
                {
                    Session["culture"] = "it-IT";
                    LANGUAGE = "IT";
                }
                else if (Thread.CurrentThread.CurrentCulture.Name == "de-DE")
                {
                    Session["culture"] = "de-DE";
                    LANGUAGE = "DE";
                }
                else
                {
                    Session["culture"] = "en-US";
                    LANGUAGE = "EN";
                }
                Session["LANGUAGE"] = LANGUAGE;
                // ----------------- CHARSET 
                string CHARSET = "ISO-8859-2";
                Session["CHARSET"] = CHARSET;
                // ---------------- COUNTRY 616 - Polska
                string COUNTRY = "616";
                Session["COUNTRY"] = COUNTRY;
                // ---------------- PAYMENTTYPE
                string PAYMENTTYPE = "CARDS";
                Session["PAYMENTTYPE"] = PAYMENTTYPE;
                // --------------- MERCHANTID
                Session["MERCHANTID"] = Int32.Parse(systemKsiegowy) == 1 ? "170906000" : "171485000";
                // ---------------- AUTODEPOSIT
                Session["AUTODEPOSIT"] = "1";
                // ---------------- IDFAKTURY
                Session["IdFaktury"] = tIDFaktury;
                reader.Close();
                conn.Close();
            }
        }
        else
        {
            string connString = ConfigurationManager.ConnectionStrings["BazaReklamConn"].ConnectionString;
            string cmdText = "SELECT SystemKsiegowyId, FirstName, LastName, Address, PostalCode, City, nip, Nabywca_nip, Brutto, Data_Wystawienia, Termin_zaplaty, Sposob_zaplaty, Imie, Nazwisko, ulica, kod, miasto, telefon, waluta_brutto, waluta_miano FROM dbo.vDanePlatnosciEcard WHERE ID_Faktury=@idFaktury";
            //string nip = (string)Session["nip"];
            //string nrZlec = (string)Session["nrFaktury"];
            int idFaktury = Int32.Parse(Session["IdFaktury"].ToString());
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(cmdText, conn);
                //cmd.Parameters.Add("@nip", SqlDbType.NVarChar, 50).Value = nip;
                //cmd.Parameters.Add("@nrFaktury", SqlDbType.NVarChar, 20).Value = nrZlec;
                cmd.Parameters.Add("@idFaktury", SqlDbType.Int).Value = idFaktury;

                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                reader.Read();

                // Wartości         
                string tBrutto = reader["Brutto"].ToString();
                string WalutaBrutto = reader["Waluta_Brutto"].ToString();
                string AmountPLN = (Convert.ToInt32(reader["Brutto"]) * 100).ToString(); // reader[""].ToString(); //przeliczNaGrosze(reader, 10);
                string AmountWaluta = (Convert.ToInt32(reader["waluta_brutto"]) * 100).ToString(); //przeliczNaGrosze(reader, 22);

                // ------------- Ustawienie zmiennych -----------------------
                kwotaPLN = tBrutto;
                kwotaWaluta = WalutaBrutto;
                kwotaGroszePLN = AmountPLN;
                kwotaGroszeWaluta = AmountWaluta;                

                reader.Close();
                conn.Close();                
            }
        }
        
    }

    protected void WyborWaluty_SelectedIndexChanged(object sender, EventArgs e)
    {
        string waluta = WyborWaluty.SelectedItem.Value;
        string kwota = "";        
        if (waluta == "PLN")
        {
            kwota = kwotaPLN + " zł";
            Session["CURRENCY"] = "985";
            Session["AMOUNT"] = kwotaGroszePLN;            
        }
        if (waluta == "EUR")
        {
            kwota = kwotaWaluta + " €";
            Session["CURRENCY"] = "978";
            Session["AMOUNT"] = kwotaGroszeWaluta;            
        }
        //if (waluta == "GBP")
        //{
        //    kwota = kwotaWaluta + " £";
        //    Session["CURRENCY"] = "826";
        //    Session["AMOUNT"] = kwotaGroszeWaluta;            
        //}
        Session["kwotaBrutto"] = kwota;
        Brutto.Text = kwota;         
    }

    protected void PlaceButton_Click(object sender, EventArgs e)
    {
        // ------------------ NAME
        string NAME = ImieTextBox.Text;
        Session["NAME"] = NAME;
        // ------------------ SURNAME
        string SURNAME = NazwiskoTextBox.Text;
        Session["SURNAME"] = SURNAME;
        // -------------- Przekierowanie
        Response.Redirect("Merchant.aspx");
    }

    protected void ZamknijButton_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Server.Transfer("login.aspx");
    }

    private string forma;
    public string FormaPlatnosci
    {
        get
        {
            return forma;
        }
        set
        {
            forma = value;
            if (forma == "karta")
            {
                forma = "karta";
            }
            else
            {
                forma = "";
            }            
        }
    }

    private bool czyWyswietlicWyborWaluty;
    public bool WyswietlicWyborWaluty
    {
        get
        {
            return czyWyswietlicWyborWaluty;
        }
        set
        {
            czyWyswietlicWyborWaluty = value;            
        }
    }    
    
    private string kwotaPLN;
    public string kwotaBruttoPLN
    {
        get
        {
            return kwotaPLN;
        }
        set
        {
            kwotaPLN = value;
            if (kwotaPLN == "") { kwotaPLN = "0"; }
        }
    }

    private string kwotaWaluta;
    public string kwotaBruttoWaluta
    {
        get
        {
            return kwotaWaluta;
        }
        set
        {
            kwotaWaluta = value;
            if (kwotaWaluta == "") { kwotaWaluta = "0"; }
        }
    }

    private string amPLN;
    public string kwotaGroszePLN
    {
        get
        {
            return amPLN;
        }
        set
        {
            amPLN = value;
            if (amPLN == "") { kwotaGroszePLN = "0"; }
        }
    }

    private string amWaluta;
    public string kwotaGroszeWaluta
    {
        get
        {
            return amWaluta;
        }
        set
        {
            amWaluta = value;
            if (amWaluta == "") { kwotaGroszeWaluta = "0"; }
        }
    }

    
}
