using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Baza_Reklam.Classes;

namespace Baza_Reklam
{
    public partial class LogoWReklamieForm : Form
    {

		#region Fields (1) 

        private int customerId;
        private int reklamaId;

        Baza_Reklam.REKLAMADataSetTableAdapters.REKLAMATableAdapter reklamaAdapter = new Baza_Reklam.REKLAMADataSetTableAdapters.REKLAMATableAdapter();

		#endregion Fields 

		#region Constructors (1) 

        public LogoWReklamieForm(int reklamaId)
        {
            InitializeComponent();

            this.reklamaId = reklamaId;
            this.customerId = Utils.customerId(reklamaId);
            
            this.kLIENCI_INTERNET_LOGOTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            this.reklamaAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
        }

		#endregion Constructors 

		#region Methods (5) 


		// Private Methods (5) 

        private bool CheckImageSize()
        {
            Image image = Image.FromFile(openFileDialog1.FileName);
            if (image.Size.Height > 75 || image.Size.Width > 150)
            {
                MessageBox.Show("Logo ma nieodpowiednie wymiary (150 x 75)");
                return false;
            }

             return true;
        }

        private void DodajLogoToolStripButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                if (!CheckImageSize())
                {
                    return;
                }

                kLIENCI_INTERNET_LOGOTableAdapter.Insert(customerId,openFileDialog1.SafeFileName,"",ImageToBytes(openFileDialog1.FileName));

                this.kLIENCI_INTERNET_LOGOTableAdapter.FillBy(this.bAZA_REKLAMDataSet.KLIENCI_INTERNET_LOGO, customerId);
            }
        }

        private byte[] ImageToBytes(string  fileName)
        {
            FileInfo fInfo = new FileInfo(fileName);

            long bytesNumber = fInfo.Length;

            FileStream stream = new FileStream(fileName, FileMode.Open,FileAccess.Read);
            BinaryReader reader = new BinaryReader(stream);

            byte[] byteArray = reader.ReadBytes((int)bytesNumber);

            reader.Close();
            stream.Close();

            return byteArray;
        }

        private void ImageFromDB(int idLoga)
        {

            Byte[] byteBLOBData = new Byte[0];

            string sqlText = "SELECT logo FROM KLIENCI_INTERNET_LOGO WHERE id = " + idLoga;

            SqlConnection connection = new SqlConnection(ConnString.getConnString().Value);
            SqlCommand command = new SqlCommand(sqlText, connection);

            connection.Open();
            SqlDataReader dr = command.ExecuteReader();

            if (dr.Read())
            {
                byteBLOBData = (Byte[])dr.GetValue(0);
            }

            connection.Close();
            MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
            pictureBox1.Image = Image.FromStream(stmBLOBData);
            
            stmBLOBData.Close();    
         
        }

        private void LogaKlientowForm_Load(object sender, EventArgs e)
        {
            this.kLIENCI_INTERNET_LOGOTableAdapter.FillBy(this.bAZA_REKLAMDataSet.KLIENCI_INTERNET_LOGO,customerId);

            //int id = Utils.logoId(reklamaId);


            REKLAMADataSet.REKLAMARow row = reklamaAdapter.GetDataByReklamaId(reklamaId)[0];


            if (row.logo != false)
            {
                ImageFromDB(row.logoID);
            }
            
        }


		#endregion Methods 

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (kLIENCI_INTERNET_LOGOBindingSource.Current != null)
            {
                DataRowView logoRow = (DataRowView)kLIENCI_INTERNET_LOGOBindingSource.Current;

                REKLAMADataSet.REKLAMARow row = reklamaAdapter.GetDataByReklamaId(reklamaId)[0];

                row.logo = true;
                row.logoID = Convert.ToInt32(logoRow["ID"]);

                reklamaAdapter.Update(row);

                ImageFromDB(row.logoID);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

         
            REKLAMADataSet.REKLAMARow row = reklamaAdapter.GetDataByReklamaId(reklamaId)[0];

            row.logo = false;
           // row.logoID = DBNull.Value;

            reklamaAdapter.Update(row);

            pictureBox1.Image = null;
        }


    }
}