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.IO;

namespace NotePad
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        OpenFileDialog OpenFile = new OpenFileDialog();
        SaveFileDialog SaveFile = new SaveFileDialog();
        string _pathFile = string.Empty;

        private void Open()
        {
            OpenFile.Title = "Open Document";
            OpenFile.Filter = "Text Files|*.txt";
            OpenFile.FileName = string.Empty;
            if (OpenFile.ShowDialog() == DialogResult.OK)
            {
                if (OpenFile.FileName == string.Empty)
                    return;
                else
                {
                    richTextBox1.LoadFile(OpenFile.FileName,RichTextBoxStreamType.PlainText);
                    _pathFile = OpenFile.FileName;
                    this.Text = Path.GetFileName(OpenFile.FileName + " - Notepad");
                }
            }
        }

        private void Save()
        {
            richTextBox1.Modified = false;
            if (this.Text == "Untitled - Notepad")
            {
                SaveFile.Title = "Save As";
                SaveFile.Filter = "Text Document|*.txt";
                if (SaveFile.ShowDialog() == DialogResult.OK)
                {
                    richTextBox1.SaveFile(SaveFile.FileName, RichTextBoxStreamType.PlainText);
                    this.Text = Path.GetFileName(SaveFile.FileName + " - Notepad");
                    _pathFile = SaveFile.FileName;
                }
            }
            else
                richTextBox1.SaveFile(_pathFile, RichTextBoxStreamType.PlainText);  
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Modified == true)
            {
                DialogResult dr = MessageBox.Show("آیا مایل به ذخیره سازی هستید؟", "ذخیره", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    Save();
                    Open();
                }
                else
                    Open();
            }
            else
                Open();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Modified == true)
            {
                DialogResult dr = MessageBox.Show("آیا مایل به ذخیره سازی هستید؟", "ذخیره", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    Save();
                    richTextBox1.Clear();
                    this.Text = "Untitled - Notepad";
                }
                else
                {
                    richTextBox1.Clear();
                    this.Text = "Untitled - Notepad";
                }
            }

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Modified == true)
            {
                richTextBox1.Modified = false;
                DialogResult dr = MessageBox.Show("آیا مایل به ذخیره سازی هستید؟", "ذخیره", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    Save();
                    Application.Exit();
                }
                else
                    Application.Exit();
            }
            else
                Application.Exit();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Modified = false;
            SaveFile.Title = "Save As";
            SaveFile.Filter = "Text Document|*.txt";
            if (SaveFile.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.SaveFile(SaveFile.FileName, RichTextBoxStreamType.PlainText);
                this.Text = Path.GetFileName(SaveFile.FileName + " - Notepad");
                _pathFile = SaveFile.FileName;
            }
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.CanUndo)
                richTextBox1.Undo();
        }

        private void timeDateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += Convert.ToString(DateTime.Now);
        }

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmFind fFine = new frmFind();
            fFine.Show();
        }

        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd=new FontDialog ();
            fd.Font=richTextBox1.SelectionFont;
            if (fd.ShowDialog() == DialogResult.OK)
                richTextBox1.Font = fd.Font;
        }

        private void goToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmGoTo fGoTo = new frmGoTo();
            fGoTo.Show();
        }

 

        private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

       
 

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = "";
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout fAbout = new frmAbout();
            fAbout.Show();
        }

        private void statusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusBarToolStripMenuItem.Checked = !statusBarToolStripMenuItem.Checked;
            statusStrip1.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void printToolStripMenuItem_Click_1(object sender, EventArgs e)
        {

        }

        private void replaceToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            frmReplace fRap = new frmReplace();
            fRap.Show();
        }

        private void richTextBox1_TextChanged_1(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Line:   " + (richTextBox1.GetLineFromCharIndex(Int32.MaxValue) + 1) + "   Cols" + richTextBox1.Text.Length;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (richTextBox1.Modified == true)
            {
                richTextBox1.Modified = false;
                DialogResult dr = MessageBox.Show("آیا مایل به ذخیره سازی هستید؟", "ذخیره", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    Save();
                    Application.Exit();
                }
                else
                    Application.Exit();
             
            }
            else
                Application.Exit();
        }

        private void Print_Click(object sender, EventArgs e)
        {
            printDialog1.Document = printDocument1;
            if (printDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();
        }

        private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 100);
        }

        private void Form1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {

        }
    }
}
