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;

namespace WindowsFormsApplication1
{
    public partial class Frm_Noteee : Form
    {
        public Frm_Noteee()
        {
            InitializeComponent();
        }
       
        RichTextBox RTFbox;

        private void btn_Open_Click(object sender, EventArgs e)
        {

            string readpath;
            if (discardChanges())
            {
                openFileDialog1.Filter = "RTF Files|*.RTF|DOC Files|*.DOC|Text Files|*.TXT|All Files|*.*";
                openFileDialog1.ShowDialog();
                readpath = openFileDialog1.FileName;
                if (readpath.Length == 0)
                {
                    return;
                }
                else
                {
                    txt_editor.LoadFile(readpath, RichTextBoxStreamType.PlainText);
                }
            }
        }

        private void btn_Font_Click(object sender, EventArgs e)
        {
            FontDialog fd;
            fd = new FontDialog();
            fd.ShowDialog();
            txt_editor.Font = fd.Font;

        }

        private void btn_Color_Click(object sender, EventArgs e)
        {
            //ColorDialog cd;
            //cd = new ColorDialog();
            //cd.ShowDialog();
            //txt_editor.ForeColor = cd.Color;
            radColorDialog1.ShowDialog();
            txt_editor.ForeColor = radColorDialog1.SelectedColor;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            string savepath;
            if (txt_editor.Text == "")
            {
                return;
            }
            else
            {
                saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFileDialog1.ShowDialog();
                savepath = saveFileDialog1.FileName;
                    if (savepath.Length == 0)
                    {
                        return;
                    }
                    else
                    {
                    txt_editor.SaveFile(savepath, RichTextBoxStreamType.PlainText);
                }
            }
        }
        
        private void btn_New_Click(object sender, EventArgs e)
        {
            string savepath;
            if (discardChanges())
            {
                txt_editor.Clear();
                return;
            }
            else
            {
                saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFileDialog1.ShowDialog();
                savepath = saveFileDialog1.FileName;
                if (savepath.Length == 0)
                {
                    return;
                }
                else
                {
                    txt_editor.SaveFile(savepath, RichTextBoxStreamType.PlainText);
                }
            }
        }
        
        private void btn_SaveAs_Click(object sender, EventArgs e)
        {
            string savepath;
            if (txt_editor.Text == "")
            {
                return;
            }
            else
            {
                saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFileDialog1.ShowDialog();
                savepath = saveFileDialog1.FileName;
                if (savepath.Length == 0)
                {
                    return;
                }
                else
                {
                    txt_editor.SaveFile(savepath, RichTextBoxStreamType.PlainText);
                }
            } 
        }
        
        private void btn_Cut_Click(object sender, EventArgs e)
        {
            txt_editor.Cut();
        }

        private void btn_Copy_Click(object sender, EventArgs e)
        {
            txt_editor.Copy();
        }

        private void btn_Paste_Click(object sender, EventArgs e)
        {
            txt_editor.Paste();
        }
        
        private void btn_Clear_Click(object sender, EventArgs e)
        {
            txt_editor.Clear();
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
           
            string savepath;
            if (txt_editor.Text == "")
            {
                this.Close();
            } 
            else 
             {
                 saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                 saveFileDialog1.ShowDialog();
                 savepath = saveFileDialog1.FileName;
                 if (savepath.Length == 0)
                 {
                     return;
                 }
                 else
                 {
                     txt_editor.SaveFile(savepath, RichTextBoxStreamType.PlainText);
                 }     
              }
            
            txt_editor.Clear();
        }
        
        private void btn_About_Click(object sender, EventArgs e)
        {
            grpBox_About.Visible = true;
        }
        
        private void Frm_Noteee_Load(object sender, EventArgs e)
        {
            RTFbox = txt_editor;
            grpBox_ShortKeys.Visible = false;
            grpBox_FindReplace.Visible = false;
            grpBox_About.Visible = false;
            grpBox_Counter.Visible = false;
            
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            grpBox_About.Visible = false; 
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            SaveFileDialog sd;
            sd = new SaveFileDialog();
            string savepath;
            if (keyData == (Keys.Control | Keys.S))
            {
                if (txt_editor.Text == "")
                {
                    return true;
                }
                else
                {
                    saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                    saveFileDialog1.ShowDialog();
                    savepath = saveFileDialog1.FileName;
                    if (savepath.Length == 0)
                    {
                        return true;
                    }
                    else
                    {
                        txt_editor.SaveFile(savepath, RichTextBoxStreamType.PlainText);
                    }
                }
            }
            else if (keyData == (Keys.Alt | Keys.F)) {
                FontDialog fd;
                fd = new FontDialog();   //Opening FontDialog When Pressed Alt+F
                fd.ShowDialog();
                txt_editor.Font = fd.Font;

            }
            else if(keyData == (Keys.Alt | Keys.C)) {
                //ColorDialog cd;
                //cd = new ColorDialog();  //Opening ColorDialog When Pressed Alt+C
                //cd.ShowDialog();
                //txt_editor.ForeColor = cd.Color;
                radColorDialog1.ShowDialog();
                txt_editor.ForeColor = radColorDialog1.SelectedColor;
            }
            else if(keyData == (Keys.Control | Keys.O)) {
                string readpath;
                openFileDialog1.Filter = "RTF Files|*.RTF|DOC Files|*.DOC|Text Files|*.TXT|All Files|*.*";
                openFileDialog1.ShowDialog();
                readpath = openFileDialog1.FileName;
                if (readpath.Length == 0)
                {
                    return true;
                }
                else
                {
                    txt_editor.LoadFile(readpath, RichTextBoxStreamType.PlainText);
                }
            }
            else if (keyData == (Keys.Control | Keys.N)) {

                if (discardChanges())
                {
                    txt_editor.Clear();   //Opening NewDocument When Pressed Ctrl+N                    return true;
                }
                else
                {
                    saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                    saveFileDialog1.ShowDialog();
                    savepath = saveFileDialog1.FileName;
                    if (savepath.Length == 0)
                    {
                        return true;
                    }
                    else
                    {
                        txt_editor.SaveFile(savepath, RichTextBoxStreamType.PlainText);
                    }
                }   
            }
            else if( keyData == (Keys.Alt | Keys.X)) {
                
                if (discardChanges())
                {
                    this.Close();
                }
                else                        //Closing File When Pressed Alt+X
                {
                    saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                    saveFileDialog1.ShowDialog();
                    savepath = saveFileDialog1.FileName;
                    if (savepath.Length == 0)
                    {
                        return true;
                    }
                    else
                    {
                        txt_editor.SaveFile(savepath, RichTextBoxStreamType.PlainText);
                    }
                }

            }
            else if (keyData == (Keys.Control | Keys.F)) {

                
                grpBox_FindReplace.Show();
            }
            else if (keyData == (Keys.Alt | Keys.A)) {
                txt_editor.Select(0, txt_editor.TextLength); //Selecting All Text in TextEditor
            }
            else if (keyData == (Keys.Alt | Keys.Q)) {
                txt_editor.Cut(); //Cutting All Text in TextEditor
            }
            else if (keyData == (Keys.Alt| Keys.V))
            {
                txt_editor.Paste();//Paste All Text In TextEditor
           
            }
            else if(keyData == Keys.Escape) {
                grpBox_FindReplace.Visible = false;
                grpBox_ShortKeys.Visible = false;
                grpBox_Counter.Visible = false;
            }else if (keyData == (Keys.Alt | Keys.L)) {
                countCharactersWords();
                grpBox_Counter.Visible = true;
            }
            
                return base.ProcessCmdKey(ref msg, keyData);
        }

       
        private void btn_Replace_Click(object sender, EventArgs e)
        {

            grpBox_FindReplace.Show();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            grpBox_FindReplace.Visible = false;
        }

        public void countCharactersWords()//CountingWords And Characters in Your RichTextBox
        {
            int countCharacters = 0;
            string[] countWords = txt_editor.Text.Split("".ToCharArray());
            try
            {
                countCharacters = txt_editor.TextLength;
                btn_Characters.ControlText = countCharacters.ToString();
                btn_Words.ControlText = countWords.Length.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private RichTextBoxFinds setSearchMode() {
            
            RichTextBoxFinds mode;
            
            if (chkCase.Checked == true) {
                mode = RichTextBoxFinds.WholeWord;
            }
            else {
                mode = RichTextBoxFinds.None;
            }
            if (chkWord.Checked == true) {
                mode = mode | RichTextBoxFinds.WholeWord;
            }
            else {
                mode = mode | RichTextBoxFinds.None;
            }
            return mode;
        }

        private Boolean discardChanges() {
            
            DialogResult dr;
            
            if (txt_editor.Modified)
            {
                dr = MessageBox.Show("Text Hasn't Been Saved", "Discard changes?",MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (Convert.ToString(dr) == "No")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else 
            {
                return true;
            }
        }
        

        private void btn_Find_Click(object sender, EventArgs e)
        {
            try
            {   
                int wordIndex;
                RichTextBoxFinds searchWord;
                searchWord= setSearchMode();
                wordIndex = txt_editor.Find(txt_Find.Text,0,searchWord);
                if (wordIndex == -1) {
                    //MessageBox.Show("Cannot Find The Word");
                    return;
                }
                
                txt_editor.Select(wordIndex, txt_Find.Text.Length);
                txt_editor.SelectionBackColor = Color.Red;
                txt_Find.ScrollToCaret();

                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_Replace2_Click(object sender, EventArgs e)
        {

            try
            {
                if (txt_editor.SelectedText != " ") {
                    txt_editor.SelectedText = txt_Replace.Text;
                }
                btn_Find_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_ShortKeys_Click(object sender, EventArgs e)
        {
            grpBox_ShortKeys.Show();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            grpBox_ShortKeys.Visible = false;
        }
        
        private void btnCounter_Ok_Click(object sender, EventArgs e)
        {
            grpBox_Counter.Visible = false;
        }

    }
}

