using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net.Mail;
using System.Data.SqlServerCe;
using System.Data;
using System.Collections;
namespace WpfRibbonApplication1
{
    /// <summary>
    /// Interaction logic for create.xaml
    /// </summary>
    public partial class create : Window
    {
        public create()
        {
            InitializeComponent();
     //       add();
           
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Class1 cls = new Class1();
            String username = cls.getun();
            String pass = cls.getpwd();
           
            try{
            if (username != "" && pass != "" && textBox3.Text!="")
            {
                
                    MailMessage msg = new MailMessage();
                    msg.From = new MailAddress(username);
                    msg.To.Add(comboBox1.SelectedValue.ToString());

                    msg.Subject = textBox1.Text;
                    msg.Body = textBox2.Text;

                    System.Net.Mail.SmtpClient mailClient = new SmtpClient("smtp.gmail.com");
                    mailClient.Credentials = new System.Net.NetworkCredential(username, pass);
                    mailClient.Port = 587;
                    mailClient.EnableSsl = true;
                    mailClient.Send(msg);
                    connect();
               
            }else
            {
                MessageBox.Show("Please First sign in with your Gmail Account or Select your frient from the list","Alert",MessageBoxButton.OK,MessageBoxImage.Exclamation);
            }

            }catch(Exception s)
            {

                MessageBox.Show("There is some error occur Please check your connection","Error",MessageBoxButton.OK,MessageBoxImage.Error);

            }
           

        }
        private void connect()
        {
            SqlCeConnection con=new SqlCeConnection(@"Data Source=dddd.sdf");
            SqlCeCommand cmd = new SqlCeCommand("INSERT INTO mail VALUES(@dt,@to,@sub,@body)", con);
            cmd.Parameters.AddWithValue("@dt",DateTime.Now.ToShortDateString());
            cmd.Parameters.AddWithValue("@to", textBox3.Text);
            cmd.Parameters.AddWithValue("@sub", textBox1.Text);
            cmd.Parameters.AddWithValue("@body", textBox2.Text);
            con.Open();
            int i=cmd.ExecuteNonQuery();
          //  MessageBox.Show(i.ToString());
            con.Close();
            }
        private void add()
        {
            SqlCeConnection con = new SqlCeConnection(@"Data Source=dddd.sdf");
            //MessageBox.Show(con.State.ToString());
            DataSet ds2 = new DataSet();
            SqlCeCommand dap = new SqlCeCommand("Select name from ddd", con);
            con.Open();
            SqlCeDataReader dr = dap.ExecuteReader();
            ArrayList ar = new ArrayList();
            while (dr.Read())
            {
                ar.Add(dr["name"]);

            }

            comboBox1.ItemsSource = ar;

            con.Close();
            

        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SqlCeConnection con = new SqlCeConnection(@"Data Source=dddd.sdf");

            SqlCeCommand dap2 = new SqlCeCommand("Select email from ddd where name='" +comboBox1.SelectedItem +"'", con);
            con.Open();
            SqlCeDataReader dr2 = dap2.ExecuteReader();
            while (dr2.Read())
            {
                textBox3.Text = dr2[0].ToString();
            }
            con.Close();   
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();


        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                add();
            }
            catch (Exception es)
            {
                MessageBox.Show(es.ToString()+"Restart your application again", "There is error ");

            }
        }

        
        
    }
}
