using System;
using System.IO;
using System.Windows.Forms;

namespace AdvertsingProfitControl
{
    public partial class FrmLogFileViewer : Form
    {
        private string _clickedNode;
        //Create the right-click context menu.
        private readonly ContextMenu _menu = new ContextMenu();
        private readonly MenuItem _item = new MenuItem("Delete File");
        public FrmLogFileViewer()
        {
            InitializeComponent();
            RefreshLogFileListing();
            fileListTreeView.NodeMouseClick += NodeSelectionChangedOnMouseClick;
            _menu.MenuItems.Add(_item);
            _item.Click += DeleteFile;
        }

        private void DeleteFile(object sender, EventArgs e)
        {
            if (_clickedNode == string.Empty) return;
            if (!File.Exists(_clickedNode)) return;
            if (_clickedNode != null) File.Delete(_clickedNode);
            RefreshLogFileListing();
        }

        private void NodeSelectionChangedOnMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //Refresh the text boxes.
                ReadSelectedFile(e.Node.Index);                
            }
            else if(e.Button == MouseButtons.Right)
            {
                _clickedNode = e.Node.Name;
                _menu.Show(fileListTreeView, e.Location);
                fileListTreeView.SelectedNode = e.Node;
                ReadSelectedFile(e.Node.Index);
            }
        }

        private void RefreshLogFileListing()
        {
            var fileNames = Directory.GetFiles(GlobalClasses.LogFilePath);
            fileListTreeView.Nodes.Clear();
            foreach (var fileName in fileNames)
            {
                var file = new FileInfo(fileName);
                var node = new TreeNode(file.Name) { Name = file.FullName };
                fileListTreeView.Nodes.Add(node);
            }
            //Grab the HTML files
            fileNames = Directory.GetFiles(Application.StartupPath, "*.html", SearchOption.TopDirectoryOnly);
            foreach (var fileName in fileNames)
            {
                var file = new FileInfo(fileName);
                var node = new TreeNode(file.Name) { Name = file.FullName };
                fileListTreeView.Nodes.Add(node);
            }
            if (fileListTreeView.Nodes.Count <= 0) return;
            ReadSelectedFile();
        }

        private void ReadSelectedFile(int selectedIndex = -1)
        {
            var lines = File.ReadAllLines(selectedIndex == -1 ? fileListTreeView.Nodes[0].Name : fileListTreeView.Nodes[selectedIndex].Name);
            selectedFileNameLabel.Text = selectedIndex == -1
                ? fileListTreeView.Nodes[0].Text
                : fileListTreeView.Nodes[selectedIndex].Text;
            friendlyViewTextBox.Text = string.Empty;
            rawViewTextBox.Text = string.Empty;
            var lastCharWasWhiteSpace = false;
            foreach (var line in lines)
            {
                if (line == string.Empty)
                {
                    rawViewTextBox.Text += Environment.NewLine;  
                    if (!lastCharWasWhiteSpace)
                    {
                        friendlyViewTextBox.Text += Environment.NewLine;                      
                    }
                    lastCharWasWhiteSpace = true;
                    continue;
                }
                if (!line.Trim().StartsWith("at"))
                {
                    friendlyViewTextBox.Text += line + Environment.NewLine;
                }
                rawViewTextBox.Text += line + Environment.NewLine;
                lastCharWasWhiteSpace = false;
            }
        }
    }
}
