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

namespace Baza_Reklam
{
    public partial class MailForm : Form
    {
        public MailForm(int custID,string temat, string zalacznik)
        {
            InitializeComponent();

            kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;

            sZal1TextBox.Text = zalacznik;
            openFileDialog1.FileName = zalacznik;

            tematTextBox.Text = temat;

            this.kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU,custID);
           
        }

        public MailForm(int custID, string temat, string zalacznik, string tresc)
        {
            InitializeComponent();

           // kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;

            sZal1TextBox.Text = zalacznik;
            openFileDialog1.FileName = zalacznik;
            trescTextBox.Text = tresc;
            tematTextBox.Text = temat;
           // doComboBox.Text = Utils.mailKlienta(custID);
            string mailKlienta = Utils.mailKlienta(custID);
            doEmailTextBox.Text = mailKlienta;

          //  this.kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU, custID);

        }



        private void MailForm_Load(object sender, EventArgs e)
        {
            odTextBox.Text = User.getUser().Imie + " " + User.getUser().Nazwisko;
            odEmailTextBox.Text = User.getUser().Email;
            doWiadomosciTextBox.Text = User.getUser().Email;
            doComboBox.Text = "";

        }

        private void WyślijButton_Click(object sender, EventArgs e)
        {
            //walidacja maili  

            Regex r = new Regex("^[a-zA-Z0-9_\\.\\-]+@[a-zA-Z0-9\\-]+\\.[a-zA-Z0-9\\-\\.]+$");

            if (!r.IsMatch(odEmailTextBox.Text))
            {
                MessageBox.Show("Niepoprawny adres mailowy nadawcy");
                return;
            }

            if (!r.IsMatch(doEmailTextBox.Text))
            {
                MessageBox.Show("Nieporawny adres mailowy odbiorcy");
                return;
            }

            if (!r.IsMatch(doWiadomosciTextBox.Text))
            {
                MessageBox.Show("Nieporawny adres mailowy 'do wiadomości'");
                return;
            }

            this.Cursor = Cursors.WaitCursor;

            MailAddress adresOD = new MailAddress(odEmailTextBox.Text, odTextBox.Text);
            MailAddress adresDO = new MailAddress(doEmailTextBox.Text, doComboBox.Text);
            MailMessage message = new MailMessage(adresOD, adresDO);

            message.CC.Add(doWiadomosciTextBox.Text);
            message.Subject = tematTextBox.Text;
            message.Body = trescTextBox.Text;

            if (File.Exists(sZal1TextBox.Text))
            {
                message.Attachments.Add(new Attachment(sZal1TextBox.Text));
            }

            if (File.Exists(sZal2TextBox.Text))
            {
                message.Attachments.Add(new Attachment(sZal2TextBox.Text));
            }

            if (File.Exists(sZal3TextBox.Text))
            {
                message.Attachments.Add(new Attachment(sZal3TextBox.Text));
            }

            if (File.Exists(dZal1TextBox.Text))
            {
                message.Attachments.Add(new Attachment(dZal1TextBox.Text));
            }

            if (File.Exists(dZal2TextBox.Text))
            {
                message.Attachments.Add(new Attachment(dZal2TextBox.Text));
            }

            if (File.Exists(dZal3TextBox.Text))
            {
                message.Attachments.Add(new Attachment(dZal3TextBox.Text));
            }

            SmtpClient klientSMTP = new SmtpClient("poczta.ct.com.pl");
            klientSMTP.Timeout = 1000000;
            //klientSMTP.EnableSsl = false;
            //klientSMTP.Credentials = new System.Net.NetworkCredential(User.getUser().Email, User.getUser().Password);
            //MessageBox.Show(User.getUser().Email + User.getUser().Password); 

            try
            {
                klientSMTP.Send(message);
                MessageBox.Show("Wiadomość została wysłana");
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message);
            }

            this.Cursor = Cursors.Default;
        }

        private void dodajSZal1Button_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                sZal1TextBox.Text = openFileDialog1.FileName;
            }
        }

        private void dodajSZal2Button_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                sZal2TextBox.Text = openFileDialog1.FileName;
            }

        }

        private void dodajSZal3Button_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                sZal3TextBox.Text = openFileDialog1.FileName;
            }
        }

        private void dodajDZal1Button_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                dZal1TextBox.Text = openFileDialog1.FileName;
            }
        }

        private void dodajDZal2Button_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                dZal2TextBox.Text = openFileDialog1.FileName;
            }
        }

        private void dodajDZal3Button_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                dZal3TextBox.Text = openFileDialog1.FileName;
            }
        }

        private void otworzSZal1Button_Click(object sender, EventArgs e)
        {
            if (File.Exists(sZal1TextBox.Text))
            {
                System.Diagnostics.Process.Start(sZal1TextBox.Text);
            }
        }

        private void otworzSZal2Button_Click(object sender, EventArgs e)
        {
            if (File.Exists(sZal2TextBox.Text))
            {
                System.Diagnostics.Process.Start(sZal2TextBox.Text);
            }
        }

        private void otworzSZal3Button_Click(object sender, EventArgs e)
        {
            if (File.Exists(sZal3TextBox.Text))
            {
                System.Diagnostics.Process.Start(sZal3TextBox.Text);
            }
        }

        private void otworzDZal1Button_Click(object sender, EventArgs e)
        {
            if (File.Exists(dZal1TextBox.Text))
            {
                System.Diagnostics.Process.Start(dZal1TextBox.Text);
            }
        }

        private void otworzDZal2Button_Click(object sender, EventArgs e)
        {
            if (File.Exists(dZal2TextBox.Text))
            {
                System.Diagnostics.Process.Start(dZal2TextBox.Text);
            }
        }

        private void otworzDZal3Button_Click(object sender, EventArgs e)
        {
            if (File.Exists(dZal3TextBox.Text))
            {
                System.Diagnostics.Process.Start(dZal3TextBox.Text);
            }
        }

        private void doComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (doComboBox.SelectedValue != null)
            {
                if (kLIENCIOSOBYDOKONTAKTUBindingSource.Current != null)
                {
                    doEmailTextBox.Text = doComboBox.SelectedValue.ToString();
                }
            }
        }

    }
}