using ICSharpCode.TextEditor;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using XmlPad;

namespace ryProcessManager.hezuo
{
    public partial class ContextMenuStripHighlightText : ContextMenuStrip
    {
        public ContextMenuStripHighlightText()
        {
            InitializeComponent();
            AddMenu("撤销", "undo").Click += Undo_Click;
            AddMenu("重做", "redo").Click += Redo_Click;
            AddSeparatorMenu();
            AddMenu("剪切", "cut").Click += Cut_Click;
            AddMenu("复制", "copy").Click += Copy_Click;
            AddMenu("粘贴", "paste").Click += Paste_Click;
            AddMenu("删除", "del").Click += Del_Click;
            AddMenu("全选", "selectall").Click += SelectAll_Click;
            AddMenu("查找", "find").Click += Find_Click;
        }
        private void Find_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is ICSharpCode.TextEditor.TextEditorControl)) { return; }
            var rich_txt = (ICSharpCode.TextEditor.TextEditorControl)base.SourceControl;
            FindAndReplaceForm _findForm = new FindAndReplaceForm();
            TextEditorControl editor = rich_txt;
            if (editor == null) return;
            _findForm.ShowFor(editor, false);
            _findForm.ReplaceMode = false;
        }
        private void SelectAll_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is ICSharpCode.TextEditor.TextEditorControl)) { return; }
            //var rich_txt = (ICSharpCode.TextEditor.TextEditorControl)base.SourceControl;
            Thread th = new Thread(sendkey);
            th.Start();
            void sendkey()
            {
                Thread.Sleep(100);
                this.Invoke(new Action(() =>
                {
                    SendKeys.SendWait("^a");
                }));
            }
        }
        private void Undo_Click(object sender, EventArgs e)
        {
           if(!(base.SourceControl is ICSharpCode.TextEditor.TextEditorControl)) { return; }
            var rich_txt = (ICSharpCode.TextEditor.TextEditorControl)base.SourceControl;
            rich_txt.Undo();
        }
        private void Redo_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is ICSharpCode.TextEditor.TextEditorControl)) { return; }
            var rich_txt = (ICSharpCode.TextEditor.TextEditorControl)base.SourceControl;
            rich_txt.Redo();
        }
        private void Cut_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is ICSharpCode.TextEditor.TextEditorControl)) { return; }
            var rich_txt = (ICSharpCode.TextEditor.TextEditorControl)base.SourceControl;
            ryCommon.RyFiles.CopyToClip(rich_txt.ActiveTextAreaControl.SelectionManager.SelectedText);
            rich_txt.ActiveTextAreaControl.TextArea.InsertString("");
        }
        private void Copy_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is ICSharpCode.TextEditor.TextEditorControl)) { return; }
            var rich_txt = (ICSharpCode.TextEditor.TextEditorControl)base.SourceControl;
            ryCommon.RyFiles.CopyToClip(rich_txt.ActiveTextAreaControl.SelectionManager.SelectedText);
        }
        private void Paste_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is ICSharpCode.TextEditor.TextEditorControl)) { return; }
            var rich_txt = (ICSharpCode.TextEditor.TextEditorControl)base.SourceControl;
            rich_txt.ActiveTextAreaControl.TextArea.InsertString(Clipboard.GetText().Replace("\r\n", "\r").Replace("\r", "\r\n"));
        }
        private void Del_Click(object sender, EventArgs e)
        {
            if (!(base.SourceControl is ICSharpCode.TextEditor.TextEditorControl)) { return; }
            var rich_txt = (ICSharpCode.TextEditor.TextEditorControl)base.SourceControl;
            rich_txt.ActiveTextAreaControl.TextArea.InsertString("");
        }
        public ToolStripSeparator AddSeparatorMenu()
        {
            ToolStripSeparator item = new ToolStripSeparator();
            base.Items.Add(item);
            return item;
        }
        public ToolStripMenuItem AddMenu(string name, string tag)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(name)
            {
                Tag = tag
            };
            base.Items.Add(item);
            return item;
        }

        private void ContextMenuStripRichText_Opening(object sender, CancelEventArgs e)
        {
            if (!(base.SourceControl is ICSharpCode.TextEditor.TextEditorControl)) { return; }
            var rich_txt = (ICSharpCode.TextEditor.TextEditorControl)base.SourceControl;
            for (int i = 0; i < base.Items.Count; i++)
            {
                var item = base.Items[i];
                if (item.Tag == null) { continue; }
                switch(item.Tag.ToString())
                {
                    case "undo":
                        item.Enabled = rich_txt.EnableUndo;
                        break;
                    case "redo":
                        item.Enabled = rich_txt.EnableRedo;
                        break;
                    case "cut":
                        item.Enabled = (rich_txt.ActiveTextAreaControl.SelectionManager.SelectedText != "" && !rich_txt.IsReadOnly) ? true : false;
                        break;
                    case "copy":
                        item.Enabled = rich_txt.ActiveTextAreaControl.SelectionManager.SelectedText != "" ? true : false;
                        break;
                    case "paste":
                        item.Enabled = !rich_txt.IsReadOnly;
                        break;
                    case "del":
                        item.Enabled =(rich_txt.ActiveTextAreaControl.SelectionManager.SelectedText != "" && !rich_txt.IsReadOnly) ? true : false;
                        break;
                    case "selectall":
                        item.Enabled = rich_txt.ActiveTextAreaControl.SelectionManager.SelectedText != rich_txt.Text ? true : false;
                        break;
                }
            }
        }
    }
}
