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

namespace WindowsFormsApplication8
{
    public partial class frmEmail : Form
    {
        private PictureBox LDG;

        public frmEmail()
        {
            InitializeComponent();
            //Init Loading PictureBox
            LDG = new PictureBox();
            LDG.Size = panel1.Size;
            LDG.Location = panel1.Location;
            LDG.Visible = false;
            this.Controls.Add(LDG);
        }
        string attach = "";

        private string EmailTo_;
        public string EmailTo
        {
            get { return EmailTo_; }
            set { EmailTo_ = value; }
        }
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            ShowMask("... در حال فرستادن ایمیل لطفا صبر کنید");
            bool send = ClsSetting.SendMessage(txtSubject.Text, txtText.Text, ClsSetting.EmailUserid, ClsSetting.EmailPassword, ClsSetting.EmailDisplayname, txtTo.Text, attach);
            HideMask();
            if(!send)
            {
                FarsiMessegeBox.Show("ایمیل ارسال نشد.", "ایمیل", FMessegeBoxButtons.Ok, FMessegeBoxIcons.Error);
            }
            Close();
        }
        private void ShowMask(string Txt)
        {
            //New Bitmap
            Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
            //Draw Form To BMP
            panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
            //
            Graphics GR = Graphics.FromImage(bmp);
            //Transparent Overlay
            GR.FillRectangle(new SolidBrush(Color.FromArgb(150, 0, 0, 0)), panel1.ClientRectangle);
            //Font
            Font fnt = new Font("Tahoma", 13, FontStyle.Bold, GraphicsUnit.Pixel);
            //Measure Text Size
            SizeF Siz = GR.MeasureString(Txt, fnt);
            //Draw Text on center
            GR.DrawString(Txt, fnt, new SolidBrush(Color.White), bmp.Width / 2 - Siz.Width / 2, bmp.Height / 2 - Siz.Height / 2);
            //Assign to PictureBox
            LDG.Image = bmp;
            LDG.BringToFront();
            LDG.Visible = true;
            //
            this.Refresh();
            //avoid flashing ;)
            System.Threading.Thread.Sleep(500);
        }
        private void HideMask()
        {
            LDG.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog op = new OpenFileDialog())
            {
                op.Title = "ارسال فایل";
                op.CheckFileExists = true;
                op.CheckPathExists = true;
                op.Filter = "";
                if (op.ShowDialog() == DialogResult.OK)
                {
                    attach += op.FileName.Trim() + ";";
                }
            }
        }

        private void frmEmail_Load(object sender, EventArgs e)
        {
            txtTo.Text = EmailTo;
        }
    }
}
