using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using CustomDockingPanesFactory.ViewModels;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Docking;

namespace CustomDockingPanesFactory
{
    public class ShellViewModel : ViewModelBase
    {
        private readonly string SettingFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SettingsLayout.xml");
        public ShellViewModel()
        {
            Panes = new ObservableCollection<IPaneViewModel>()
            {
	            new BottomViewModel(),
                new LeftViewModel(),
                new RightViewModel(),
                new DocumentViewModel()
            };

            OnLoadedCmd = new RelayCommand<object>(OnLoaded);
            SaveLayoutCmd = new RelayCommand<object>(SaveLayout);
            AddDocumentCmd = new RelayCommand<object>(o => { Panes.Add(new DocumentViewModel()); });
            AddLeftCmd = new RelayCommand<object>(o => { Panes.Add(new LeftViewModel()); });
            AddRightCmd = new RelayCommand<object>(o => { Panes.Add(new RightViewModel()); });
            AddBottomCmd = new RelayCommand<object>(o => { Panes.Add(new BottomViewModel()); });
            OnPreviewShowCompassCmd = new RelayCommand<PreviewShowCompassEventArgs>(OnPreviewShowCompass);
        }

        /// <summary>
        /// Gets or sets Panes and notifies for changes
        /// </summary>
        public ObservableCollection<IPaneViewModel> Panes { get; set; }
        public RelayCommand<object> OnLoadedCmd { get; set; }
        public RelayCommand<object> SaveLayoutCmd { get; set; }
        public RelayCommand<object> AddDocumentCmd { get; set; }
        public RelayCommand<object> AddLeftCmd { get; set; }
        public RelayCommand<object> AddRightCmd { get; set; }
        public RelayCommand<object> AddBottomCmd { get; set; }
        public RelayCommand<PreviewShowCompassEventArgs> OnPreviewShowCompassCmd { get; set; }

        private void OnLoaded(object o)
        {
	        if (o is RadDocking radDocking)
	        {
#warning If LoadSettingsFromFile is set as comment, all groups have Name property.
                LoadSettingsFromFile(radDocking, SettingFile);
            }
        }
        private void OnPreviewShowCompass(PreviewShowCompassEventArgs e)
		{
			if (e.TargetGroup != null)
			{
				var targetGroupName = e.TargetGroup.Name;
				if (targetGroupName.Equals(""))
				{
					targetGroupName = "Name property is empty";
				}
				LogMessage($"Target group: \" {targetGroupName}\"");
			}
		}

        private void SaveLayout(object o)
        {
	        if (o is RadDocking radDocking)
	        {
		        try
		        {
			        using (var stream = new FileStream(SettingFile, FileMode.Create))
			        {
				        radDocking.Tag = "Settings";
				        radDocking.SaveLayout(stream);
				        LogMessage("Layout was properly saved.");
                    }
		        }
		        catch (Exception e)
		        {
			        LogMessage(e.Message);
                }
            }
        }

        private void LoadSettingsFromFile(RadDocking radDocking, string settingsLayoutFile)
        {
	        try
	        {
		        using (var stream = new FileStream(settingsLayoutFile, FileMode.Open))
		        {
			        radDocking.Tag = "Settings";
			        radDocking.LoadLayout(stream);
			        LogMessage("Layout was properly opened.");
                }
	        }
	        catch (Exception e)
	        {
		        LogMessage(e.Message);
            }
        }

        private void LogMessage(string msg)
        {
	        if (Panes.FirstOrDefault(p => p.Header.Contains("Bottom")) is BottomViewModel bvm)
	        {
		        bvm.Logs.Add(new Tuple<DateTime, string>(DateTime.Now, $"{msg}"));
	        }
        }
	}
}
