using System;
using System.IO;
using NotepadEnhanced.GUI.Controls;
using NotepadEnhanced.Helpers;
using System.Windows.Forms;
using NotepadEnhanced.Localization;

namespace NotepadEnhanced
{
    /// <summary>
    /// Represents the plain text document which is within a plaintextbox
    /// </summary>
    public class Document
    {
        /// <summary>
        /// Occurs when the value of the FileName property changes
        /// </summary>
        public event EventHandler FileNameChanged;
        private readonly PlainTextBox _plainTextBox;
        private FileSystemWatcher _watcher;

        public Document(PlainTextBox plainTextBox)
        {
            _plainTextBox = plainTextBox;
        }

        private void _watcher_Changed(object sender, EventArgs e)
        {
            string message = Path.GetFileName(_fileName) + " " + Strings.ChangedOutsideEditor;

            // Disable watching when dialog is open or more dialogs will open
            _watcher.EnableRaisingEvents = false;
            DialogResult result = MsgBox.Show(message, MessageBoxIcon.Question, MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                    MethodInvoker invoker = delegate
                        {
                            _plainTextBox.Text = File.ReadAllText(_fileName);
                            _plainTextBox.Modified = false;
                        };

                    _plainTextBox.Invoke(invoker);
            }

            _watcher.EnableRaisingEvents = true;
        }

        private string _fileName = string.Empty;
        /// <summary>
        /// Gets or sets the filename + path for the file
        /// </summary>
        public string FileName
        {
            get { return _fileName; }
            set
            {
                _fileName = value;
                _safeFileName = Path.GetFileName(_fileName);

                if (!value.Trim().Equals(string.Empty))
                {
                    _watcher = new FileSystemWatcher
                        {
                            Path = Path.GetDirectoryName(_fileName),
                            Filter = _safeFileName,
                            NotifyFilter = NotifyFilters.Size,
                            EnableRaisingEvents = true
                        };

                    _watcher.Changed +=_watcher_Changed;
                    
                }

                if (!value.Equals(_fileName, StringComparison.OrdinalIgnoreCase))
                {
                    if (FileNameChanged != null)
                        FileNameChanged(_plainTextBox, EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// Gets a bool which denotes whether or not the document has a file to write to
        /// </summary>
        public bool SavedOrOpened
        {
            get { return _fileName.Length > 0; }
        }

        private string _safeFileName = Strings.NewDoc;
        /// <summary>
        /// Gets or sets the filename, without the directory and with the extension.
        /// </summary>
        public string SafeFileName
        {
            get { return _safeFileName; }
            set 
            {
                if (_fileName.Length.Equals(0))
                {
                    _safeFileName = value;
                }
                else
                {
                    // Needs testing before future use
                    _safeFileName = value;
                    string dir = Path.GetDirectoryName(_safeFileName);
                    _fileName = Path.Combine(dir, _safeFileName);
                }

                _plainTextBox.Parent.Text = _safeFileName;
            }
        }

        public bool WatcherEnabled
        {
            get { return _watcher.EnableRaisingEvents; }
            set { _watcher.EnableRaisingEvents = value; }
        }
    }
}
