using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Baza_Reklam
{
    public partial class LogaKlientowForm : Form
    {

		#region Fields (1) 

        private int customerId;

		#endregion Fields 

		#region Constructors (1) 

        public LogaKlientowForm(int customerId)
        {
            InitializeComponent();
            this.customerId = customerId;

            this.kLIENCI_INTERNET_LOGOTableAdapter.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 LogaKlientowForm_Load(object sender, EventArgs e)
        {
            this.kLIENCI_INTERNET_LOGOTableAdapter.FillBy(this.bAZA_REKLAMDataSet.KLIENCI_INTERNET_LOGO,customerId);
        }

        private void UsunLogoToolStripButton_Click(object sender, EventArgs e)
        {
            if (kLIENCI_INTERNET_LOGOBindingSource.Current != null)
            {



                if (Utils.logoByloWykorzystane(Convert.ToInt32(((DataRowView)kLIENCI_INTERNET_LOGOBindingSource.Current)["ID"])))
                {
                    kLIENCI_INTERNET_LOGOBindingSource.RemoveCurrent();
                    kLIENCI_INTERNET_LOGOTableAdapter.Update(bAZA_REKLAMDataSet.KLIENCI_INTERNET_LOGO);
                }
                else
                {
                    MessageBox.Show("Logo jest przypisane do reklamy.");
                }
            }
        }


		#endregion Methods 

    }
}