using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace Wordinator
{

    public class formCodeUpdateJournal : Form
    {
        static formCodeUpdateJournal _instance = null;
        public static formCodeUpdateJournal instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new formCodeUpdateJournal();
                }
                return _instance;
            }
        }

        public static void ShowJournal()
        {
            instance.Show();
        }

        static int _intScrollToCaretIndex_Load = 0;
        static public int ScrollToCaretIndex_Load
        {
            get { return _intScrollToCaretIndex_Load; }
            set { _intScrollToCaretIndex_Load = value; }
        }
        static int _intSelectionStart_Load = 0;
        static public int SelectionStart_Load
        {
            get { return _intSelectionStart_Load; }
            set { _intSelectionStart_Load = value; }
        }


        static Point _ptLocation_Load = new Point(int.MinValue, int.MinValue);
        static public Point ptLocation_Load  
        {
            get 
            { 
                if (_ptLocation_Load.X < -Screen.PrimaryScreen.Bounds.Width)
                {
                    _ptLocation_Load = new Point(10, 10);
                }
                return _ptLocation_Load; 
            }
            set { _ptLocation_Load = value; }
        }        

        static Size _szSize_Load = new Size(1, 1);
        static public Size szSize_Load

        {
            get 
            { 
                if (_szSize_Load.Width  <10)
                {

                    Size szScreen = Screen.PrimaryScreen.Bounds.Size;
                    _szSize_Load = new Size(szScreen.Width / 4, szScreen.Height / 2);
                }
                return _szSize_Load; 
            }
            set { _szSize_Load = value; }
        }



        RichTextBox rtx = new RichTextBox();

        public formCodeUpdateJournal()
        {
            TopMost = TopMostLoad;

            Controls.Add(rtx);
            rtx.Dock  = DockStyle.Fill;
            rtx.ContextMenu = new ContextMenu();
            rtx.ContextMenu.Popup += ContextMenu_Popup;
            rtx.KeyDown += Rtx_KeyDown;
            rtx.LostFocus += Rtx_LostFocus;

            Text = "Code Updates Journal";
            File_Load();

            //Size szScreen = Screen.PrimaryScreen.Bounds.Size;
            //Size = new Size(szScreen.Width / 4, szScreen.Height / 2);
            //Icon = Properties.Resources.bug_icon;
            Hide();
            VisibleChanged += FormCodeUpdateJournal_VisibleChanged;
            FormClosing += FormCodeUpdateJournal_FormClosing;
            GotFocus += FormCodeUpdateJournal_GotFocus;
            LostFocus += FormCodeUpdateJournal_LostFocus;
        }

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

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

        private void FormCodeUpdateJournal_GotFocus(object sender, EventArgs e)
        {
            rtx.Focus();
        }

        bool bolFirstAppearance = true;
        private void FormCodeUpdateJournal_VisibleChanged(object sender, EventArgs e)
        {
            if (bolFirstAppearance)
            {
                Size = szSize_Load;
                Location = ptLocation_Load;
                rtx.Select(ScrollToCaretIndex_Load, 0);
                rtx.ScrollToCaret();
                rtx.Select(SelectionStart_Load, 0);


                bolFirstAppearance = false;
            }
        }

        private void FormCodeUpdateJournal_FormClosing(object sender, FormClosingEventArgs e)
        {
            szSize_Load = Size;
            ptLocation_Load = Location;
            ScrollToCaretIndex_Load = rtx.GetCharFromPosition(new Point(0, 0));
            SelectionStart_Load = rtx.SelectionStart;

            File_Save();
            _instance = null;
        }

        private void Rtx_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control)
            {
                switch (e.KeyCode)
                {
                    case Keys.K:
                        {
                            Font fntSel = rtx.SelectionFont;
                            if (fntSel != null)
                            {
                                bool bolStrikeOut = !fntSel.Strikeout;
                                bool bolUnderline = fntSel.Underline;
                                bool bolBold = fntSel.Bold;
                                bool bolItalic = fntSel.Italic;

                                FontStyle fntStyleNew =
                                                            (bolStrikeOut ? FontStyle.Strikeout : FontStyle.Regular)
                                                           | (bolUnderline ? FontStyle.Underline : FontStyle.Regular)
                                                           | (bolItalic ? FontStyle.Italic : FontStyle.Regular)
                                                           | (bolBold ? FontStyle.Bold : FontStyle.Regular);
                                fntSel = new Font(fntSel.Name, fntSel.Size, fntStyleNew);

                                rtx.SelectionFont = fntSel;
                            }
                            e.SuppressKeyPress = true;
                        }
                        break;

                    case Keys.B:
                        {
                            Font fntSel = rtx.SelectionFont;
                            if (fntSel != null)
                            {
                                bool bolStrikeOut = fntSel.Strikeout;
                                bool bolUnderline = fntSel.Underline;
                                bool bolBold = !fntSel.Bold;
                                bool bolItalic = fntSel.Italic;

                                FontStyle fntStyleNew =
                                                            (bolStrikeOut ? FontStyle.Strikeout : FontStyle.Regular)
                                                           | (bolUnderline ? FontStyle.Underline : FontStyle.Regular)
                                                           | (bolItalic ? FontStyle.Italic : FontStyle.Regular)
                                                           | (bolBold ? FontStyle.Bold : FontStyle.Regular);
                                fntSel = new Font(fntSel.Name, fntSel.Size, fntStyleNew);

                                rtx.SelectionFont = fntSel;
                            }
                            e.SuppressKeyPress = true;
                        }
                        break;

                    case Keys.U:
                        {
                            Font fntSel = rtx.SelectionFont;
                            if (fntSel != null)
                            {
                                bool bolStrikeOut = fntSel.Strikeout;
                                bool bolUnderline = !fntSel.Underline;
                                bool bolBold = fntSel.Bold;
                                bool bolItalic = fntSel.Italic;

                                FontStyle fntStyleNew =
                                                            (bolStrikeOut ? FontStyle.Strikeout : FontStyle.Regular)
                                                           | (bolUnderline ? FontStyle.Underline : FontStyle.Regular)
                                                           | (bolItalic ? FontStyle.Italic : FontStyle.Regular)
                                                           | (bolBold ? FontStyle.Bold : FontStyle.Regular);
                                fntSel = new Font(fntSel.Name, fntSel.Size, fntStyleNew);

                                rtx.SelectionFont = fntSel;
                            }
                            e.SuppressKeyPress = true;
                        }
                        break;

                    case Keys.I:
                        {
                            Font fntSel = rtx.SelectionFont;
                            if (fntSel != null)
                            {
                                bool bolStrikeOut = fntSel.Strikeout;
                                bool bolUnderline = fntSel.Underline;
                                bool bolBold = fntSel.Bold;
                                bool bolItalic = !fntSel.Italic;

                                FontStyle fntStyleNew =
                                                            (bolStrikeOut ? FontStyle.Strikeout : FontStyle.Regular)
                                                           | (bolUnderline ? FontStyle.Underline : FontStyle.Regular)
                                                           | (bolItalic ? FontStyle.Italic : FontStyle.Regular)
                                                           | (bolBold ? FontStyle.Bold : FontStyle.Regular);
                                fntSel = new Font(fntSel.Name, fntSel.Size, fntStyleNew);

                                rtx.SelectionFont = fntSel;
                            }
                            e.SuppressKeyPress = true;
                        }
                        break;


                }

            }
            else
            {
                switch (e.KeyCode)
                {
                    case Keys.Escape:
                        {
                            Close();
                            return;
                        }
                }
            }
        }

        static bool _bolTopMostLoad = true;
        static public bool TopMostLoad
        {
            get { return _bolTopMostLoad; }
            set { _bolTopMostLoad = value; }
        }


        private void ContextMenu_Popup(object sender, EventArgs e)
        {
            rtx.ContextMenu.MenuItems.Clear();

            MenuItem mnuTopMost = new MenuItem("TopMost", mnuTopMost_Click);
            mnuTopMost.Checked = TopMost;
            rtx.ContextMenu.MenuItems.Add(mnuTopMost);

            rtx.ContextMenu.MenuItems.Add(new MenuItem("store current file", mnuStoreCurrentFile_Click));

        }

        void mnuTopMost_Click(object sender, EventArgs e)
        {
            TopMost = !TopMost;
            TopMostLoad = TopMost;
        }

        void mnuStoreCurrentFile_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Storing current file will create a fresh journal. \r\n\tproceed storing current file?", "store file", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                File_Save();
                if (System.IO.File.Exists(FilenameCurrentUpdates))
                {
                    string strFilenameStamped = FilenameStamped();
                    if (System.IO.File.Exists(strFilenameStamped))
                    {
                        MessageBox.Show("the file already exists - call your local coder");
                        return;
                    }
                    System.IO.File.Copy(FilenameCurrentUpdates, strFilenameStamped);
                    
                    if (System.IO.File.Exists(FilenameCurrentUpdates))
                        System.IO.File.Delete(FilenameCurrentUpdates);
                    File_Load();
                }
            }
        }

        static string dateStamp()
        {
            DateTime dtNow = DateTime.Now;
            string strRetVal = dtNow.ToLongDateString() + "-" + dtNow.ToLongTimeString();
            return strRetVal;
        }

        static string FilenameStamped()
        {
            DateTime dtNow = DateTime.Now;
            string strRetVal = FilenameBase
                              + dtNow.Year.ToString("0000") + dtNow.Month.ToString("00") + dtNow.Day.ToString("00")
                              + "_" + dtNow.Hour.ToString("00") + dtNow.Minute.ToString("00")
                              + ".rtf";

            return strRetVal;
        }

        static string FilenameCurrentUpdates 
        {
            get
            {
                string strRetVal = FilenameBase + "CurrentUpdates.rtf";
                return strRetVal;
            }
        }
        static string FilenameBase
        {
            get
            {
                string strRetVal = Directory + "\\CodeUpdatesJournal_";
                return strRetVal;
            }
        }

        static string _strDirectory = "";
        static public string Directory
        {
            get 
            {
                if (_strDirectory.Length == 0)
                {
                    _strDirectory = System.IO.Directory.GetCurrentDirectory();
                }
                return _strDirectory; 
            }
            set { _strDirectory = value; }
        }


        void File_Save()
        {
            rtx.SaveFile(FilenameCurrentUpdates);
        }

        void File_Load()
        {
            if (System.IO.File.Exists(FilenameCurrentUpdates))
            {
                rtx.LoadFile(FilenameCurrentUpdates);
            }
            else
            {
                Rtx_InitFile();
            }
        }

        void Rtx_InitFile()
        {
            rtx.Text = dateStamp()
                          + "\r\n  ";
            rtx.Select(rtx.Text.Length, 0);
            rtx.SelectedRtf = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\nouicompat\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Calibri;}}\r\n{\\*\\generator Riched20 10.0.22621}{\\*\\mmathPr\\mdispDef1\\mwrapIndent1440 }\\viewkind4\\uc1 \r\n\\pard\\sl240\\slmult1\\f0\\fs22\\lang9\\par\r\n\\par\r\n\r\n\\pard \r\n{\\pntext\\f0 1.\\tab}{\\*\\pn\\pnlvlbody\\pnf0\\pnindent0\\pnstart1\\pndec{\\pntxta.}}\r\n\\fi-360\\li360\\sl240\\slmult1 fixed bug - \\par\r\n{\\pntext\\f0 2.\\tab}fixed bug - \\par\r\n{\\pntext\\f0 3.\\tab}fixed bug - \\par\r\n{\\pntext\\f0 4.\\tab}fixed bug - \\par\r\n{\\pntext\\f0 5.\\tab}fixed bug - \\par\r\n{\\pntext\\f0 6.\\tab}fixed bug - \\par\r\n{\\pntext\\f0 7.\\tab}fixed bug - \\par\r\n\r\n\\pard\\sl240\\slmult1\\par\r\n\\par\r\n\\par\r\n\r\n\\pard \r\n{\\pntext\\f0 1.\\tab}{\\*\\pn\\pnlvlbody\\pnf0\\pnindent0\\pnstart1\\pndec{\\pntxta.}}\r\n\\fi-360\\li360\\sl240\\slmult1 new feature - \\par\r\n{\\pntext\\f0 2.\\tab}new feature - \\par\r\n{\\pntext\\f0 3.\\tab}new feature - \\par\r\n{\\pntext\\f0 4.\\tab}new feature - \\par\r\n{\\pntext\\f0 5.\\tab}new feature - \\par\r\n{\\pntext\\f0 6.\\tab}new feature - \\par\r\n\r\n\\pard\\sl240\\slmult1\\par\r\n\r\n\\pard\\sl240\\slmult1\\par\r\n\\par\r\n\\par\r\n\\par\r\n\r\n\\pard\\sl240\\slmult1\\par\r\n}\r\n ";
            rtx.Refresh();    
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(formCodeUpdateJournal));
            this.SuspendLayout();
            // 
            // formCodeUpdateJournal
            // 
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "formCodeUpdateJournal";
            this.ResumeLayout(false);

        }
    }
}
