using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using InABox.Core;
using InABox.Wpf;

namespace InABox.DynamicGrid
{
    /// <summary>
    ///     Interaction logic for PDFViewer.xaml
    /// </summary>
    public partial class DocumentEditor : ThemableWindow
    {
        private IEntityDocument[] _documents;
        private readonly bool bReady;

        public DocumentEditor(params IEntityDocument[] documents)
        {
            //PrintAllowed = false;
            SaveAllowed = false;
            InitializeComponent();
            _documents = documents;

            CreateTabs(CheckSuperceded(documents));

            bReady = true;

            Documents.SelectedIndex = -1;
        }

        private List<IEntityDocument> CheckSuperceded(IEntityDocument[] documents)
        {
            var docs = documents.ToList();
            if (docs.Count() > 1)
            {              
                docs.Sort((x, y) => x.Superceded.CompareTo(y.Superceded)); //ascending
            }
            return docs;
        }

        private void CreateTabs(List<IEntityDocument> documents)
        {
            foreach (var doc in documents)
            {
                var tab = new DynamicTabItem();
                tab.Header = Path.GetFileName(doc.DocumentLink.FileName);
                tab.Tag = doc;
                Documents.Items.Add(tab);
            }

        }

        //public bool PrintAllowed { get; set; }
        public bool SaveAllowed { get; set; }
        public bool ButtonsVisible { get; set; }
        public string Watermark { get; set; }

        private bool bClosing = false;
        
        private void Documents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!bReady || (!ButtonsVisible && bClosing))
                return;

            for (var i = 0; i < e.RemovedItems.Count; i++)
            {
                if (e.RemovedItems[i] is DynamicTabItem item)
                {
                    if (item.Content is IDocumentEditor viewer)
                        viewer.Document = null;
                    item.Content = null;
                }
            }

            for (var i = 0; i < e.AddedItems.Count; i++)
            {
                var item = e.AddedItems[i] as DynamicTabItem;
                if (item != null)
                {
                    var doc = item.Tag as IEntityDocument;
                    if (doc != null)
                    {
                        IDocumentEditor editor = null;

                        var extension = Path.GetExtension(doc.DocumentLink.FileName).ToLower();

                        if (extension.Equals(".pdf"))
                        {
                            var pdf = new PDFEditorControl();
                            //pdf.PrintAllowed = PrintAllowed;
                            pdf.SaveAllowed = SaveAllowed;
                            pdf.ButtonsVisible = ButtonsVisible;
                            pdf.Watermark = Watermark;
                            editor = pdf;
                        }
                        else if (extension.Equals(".png") || extension.Equals(".bmp") || extension.Equals(".jpg") || extension.Equals(".jpeg"))
                        {
                            editor = new ImageEditorControl();
                        }
                        else if (extension.Equals(".xls") || extension.Equals(".xlsx"))
                        {
                            editor = new SpreadsheetEditorControl();
                        }
                        else if (extension.Equals(".doc") || extension.Equals(".docx")  || extension.Equals(".rtf")  || extension.Equals(".html")  || extension.Equals(".htm")  || extension.Equals(".txt"))
                        {
                            editor = new RTFEditorControl();
                        }

                        if (editor != null)
                        {
                            item.Content = editor;
                            editor.Document = doc;
                        }
                    }
                }
            }

            e.Handled = true;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            bClosing = true;
            base.OnClosing(e);
            Documents.SelectedIndex = -1;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Editor.Document = _documents.FirstOrDefault();
        }
        
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            //Editor.Document = null;
            DialogResult = true;
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
}