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 System.Data.SqlClient;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;

namespace The_Contacts
{
    public partial class View : Form
    {
        public View(string No)
        {
            InitializeComponent();
            ctnNo = No.ToString();
            ContextMenu = contextMenu1;
           
        }
        public View(string Fristname,string LastName)
        {
            InitializeComponent();
            FName = Fristname;
            LName = LastName;
            ContextMenu = contextMenu1;

        }
        string ctnNo="";
        string FName;
        string LName;
        private void label7_Click(object sender, EventArgs e)
        {

        }
        SqlConnection objSqlConnection;
        SqlCommand objSQLcommand;
        String DataDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Pars Contacts\DataBase.mdf";
        private void View_Load(object sender, EventArgs e)
        {
            objSqlConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + DataDirectory + ";Integrated Security=True;User Instance=True");
            try
            {
                objSqlConnection.Open();
                objSQLcommand = new SqlCommand();
                objSQLcommand.Connection = objSqlConnection;

            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message, "Error");
                this.Close();
            }
            try
            {
                if (ctnNo != "")
                {
                    objSQLcommand.CommandText = "select * from contact where No=" + ctnNo;
                }
                else
                {
                    objSQLcommand.CommandText = "select * from contact where name=N'" +FName+"' and lastname=N'"+LName+"'";
                }
                SqlDataReader Dr = objSQLcommand.ExecuteReader();
                Dr.Read();
              lblName.Text= Dr[1].ToString() + " " + Dr[2].ToString();
               lblTelephone.Text = Dr[3].ToString();
              lblMobile.Text= Dr[4].ToString();
              lblWeb.Text= Dr[5].ToString();
              lblEmail.Text= Dr[6].ToString();
              lblJob.Text= Dr[7].ToString();
              lblAddress.Text= Dr[8].ToString() + " " + Dr[9].ToString();
             lblNote.Text= Dr[10].ToString();
                vistaPictureBox1.Image = IMGFROMBYTE((byte[])Dr[11]);
                if (Dr[14].ToString() != "False")
                {
                    lblBirthDay.Text = Dr[14].ToString();
                }
                else
                {
                    lblBirthDay.Text = "";
                }
                
            }
            catch (Exception ex) { MessageBox.Show(ex.Message + "\nPlease try later", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.Close(); }
        }
        Image IMGFROMBYTE(byte[] bytes)
        {
            System.IO.MemoryStream M = new System.IO.MemoryStream(bytes);
            return Image.FromStream(M);
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            this.Hide();
            new Edit(ctnNo).ShowDialog();
            this.Close();
        }

        private void vistaApiPicture1_Load(object sender, EventArgs e)
        {

        }

        private void vistaApiPicture1_Click(object sender, EventArgs e)
        {
            contextMenu1.Show(vistaPictureBox1, new Point(70, 100));
        }

        private void glassPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
           new Edit(ctnNo).ShowDialog();
           this.Close();
        }
        private  Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }


        Font objPrintFont = new Font("Tahoma", 10);
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try

            {
                PrintDocument PrintDocument = new PrintDocument();
                PrintDocument.DocumentName = "Print " + lblName + " Profile";
                printDialog1.AllowPrintToFile = printDialog1.AllowSelection = printDialog1.AllowSomePages = false;
                printDialog1.Document = PrintDocument;
                if (printDialog1.ShowDialog() == DialogResult.OK)
                {
                    PrintDocument.PrinterSettings = printDialog1.PrinterSettings;
                    PrintDocument.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);

                    PrintDocument.Print();
                }
                
               
   
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Error in print", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
  
        }

        void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.PageSettings.Color = true;
           //به نام خدا
            FarsiDate.FarsiDateTime F=new FarsiDate.FarsiDateTime();
            e.Graphics.DrawString("بسم تعالی", objPrintFont,Brushes.Black,390,170, new StringFormat());
            //Date  time
            e.Graphics.DrawString("تاریخ: "+F.Today, objPrintFont,Brushes.Black, 335, 190, new StringFormat());

            e.Graphics.DrawString("پیوست: ندارد ", objPrintFont,Brushes.Black, 390, 210, new StringFormat());
            //Header
            // up line
            e.Graphics.DrawLine(Pens.Black,150,250,750,250);
            e.Graphics.DrawRectangle(Pens.Black, new Rectangle(150, 130, 100, 100));
            //Set Quality
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            e.Graphics.DrawImage(resizeImage(vistaPictureBox1.Image,new Size(100,90)), 150, 130);
            //name ,family ,...
            e.Graphics.DrawString("نام و نام خانوادگی:" + lblName.Text, objPrintFont, Brushes.Black, 530,320);
            e.Graphics.DrawString("تلفن:" + lblTelephone.Text, objPrintFont, Brushes.Black, 200, 320);
            e.Graphics.DrawString("موبایل:" +lblMobile.Text, objPrintFont, Brushes.Black, 530, 340);
            e.Graphics.DrawString("ایمیل:" + lblEmail.Text, objPrintFont, Brushes.Black, 200, 340);
            e.Graphics.DrawString("وب:" + lblWeb.Text, objPrintFont, Brushes.Black, 530, 360);
            e.Graphics.DrawString("شفل:" + lblJob.Text, objPrintFont, Brushes.Black, 200, 360);
            //
            e.Graphics.DrawString("چاپ اطلاعات",new System.Drawing.Font("Tahoma",14), Brushes.Black, 640, 265);
            e.Graphics.DrawString("چاپ شده توسط نرم افزار هوشمند پارس تلفن", objPrintFont, Brushes.Black, 270, 900, new StringFormat());
            e.Graphics.DrawString("تمام حقوق محفوظ است", objPrintFont, Brushes.Black, 350, 920, new StringFormat());

          
        }

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

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Hide();
            new Delete(ctnNo, lblName.ToString()).ShowDialog();
            this.Close();

        }

   

        private void menuItem5_Click(object sender, EventArgs e)
        {
            linkLabel2_LinkClicked(null, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            linkLabel2_LinkClicked(null, null);
        }

        private void lblEmail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("mailto:"+lblEmail.Text);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void lblWeb_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                if(lblWeb.Text.ToLower().StartsWith("http://")==false)
                {
                     System.Diagnostics.Process.Start("http://"+lblWeb.Text);
                }
                else
                {
                    System.Diagnostics.Process.Start("http://"+lblWeb.Text);
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Export to TXT
            FarsiDate.FarsiDateTime FarsiDate=new global::FarsiDate.FarsiDateTime();
            string[] ToExport = { "نام:" + lblName.Text, "تلفن:" + lblTelephone.Text, "موبایل:" + lblMobile.Text, "وب:" + lblWeb.Text, "ایمیل:" + lblEmail.Text, "شعل:" + lblJob.Text, "تاریخ تولد:" + lblBirthDay.Text, "نشانی:" + lblAddress.Text, "یادداشت:" + lblNote.Text,Environment.NewLine+Environment.NewLine,"تاریخ ذخیره:" + FarsiDate.Today + " " + DateTime.Now.ToShortTimeString().Replace("PM","بعد از ظهر ").Replace("AM","قبل از ظهر "),"شماره مخاطب:" + ctnNo,"نرم افزار هوشمند ذخیره مخاطبین بیستا " };

            SaveFileDialog S1 = new SaveFileDialog();
            S1.Title = "Save contact";
            S1.Filter = "TextFile(*.txt)|*.txt|All Files(*.*)|*.*";
            S1.FileName = lblName.Text;
            if (S1.ShowDialog() == DialogResult.OK)
            {
                System.IO.File.WriteAllLines(S1.FileName, ToExport, UTF8Encoding.UTF8);
            }
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            linkLabel2_LinkClicked(null, null);
        }

        private void menuItem8_Click(object sender, EventArgs e)
        {
            button2_Click(null, null);
        }
    }
}
