using BackupEssentials.Backup;
using BackupEssentials.Controls;
using BackupEssentials.Pages;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;

namespace BackupEssentials{
    public partial class MainWindow : Window{
        public static MainWindow Instance { get; private set; }

        private new Rect RestoreBounds = new Rect();
        private bool IsMaximized = false;

        private string[] DropData = null;

        public MainWindow(){
            InitializeComponent();
            Instance = this;

            Loaded += (args, sender) => {
                Dispatcher.BeginInvoke(DispatcherPriority.Loaded,new Action(() => {
                    DataStorage.Load();
                }));
            };

            Closed += (args, sender) => {
                DataStorage.Save(true);
                ExplorerIntegration.Refresh(true);
            };

            ShowPage(typeof(Home));
        }

        private void ButtonWindowCloseClick(object sender, RoutedEventArgs e){
            Close();
        }

        private void ButtonWindowToggleClick(object sender, RoutedEventArgs e){
            if (IsMaximized){
                Left = RestoreBounds.X;
                Top = RestoreBounds.Y;
                Width = RestoreBounds.Width;
                Height = RestoreBounds.Height;
                
                ResizeMode = ResizeMode.CanResizeWithGrip;
                IsMaximized = false;
                ButtonWindowToggle.CXPathData = (string)FindResource("PathButtonMaximized");
            }
            else{
                RestoreBounds.X = Left;
                RestoreBounds.Y = Top;
                RestoreBounds.Width = Width;
                RestoreBounds.Height = Height;

                Screen screen = Screen.FromPoint(new System.Drawing.Point((int)Math.Round(Left+Width/2),(int)Math.Round(Top+Height/2)));
                Left = screen.WorkingArea.X;
                Top = screen.WorkingArea.Y;
                Width = screen.WorkingArea.Width;
                Height = screen.WorkingArea.Height;
                
                ResizeMode = ResizeMode.CanResize;
                IsMaximized = true;
                ButtonWindowToggle.CXPathData = (string)FindResource("PathButtonWindowed");
            }

            VisualStateManager.GoToState(ButtonWindowToggle,"Normal",false);
        }

        private void ButtonWindowMinimizeClick(object sender, RoutedEventArgs e){
            WindowState = WindowState.Minimized;
        }

        private void TitleBarLeftButtonDown(object sender, MouseButtonEventArgs e){
            if (e.ClickCount == 2)ButtonWindowToggleClick(sender,e);
            else if (!IsMaximized)DragMove();
        }

        private void ButtonMainMenuClick(object sender, RoutedEventArgs e){
            ButtonMainMenu btn = (ButtonMainMenu)sender;
            
            for(int child = 0; child < VisualTreeHelper.GetChildrenCount(btn.Parent); child++){
                ButtonMainMenu childBtn = VisualTreeHelper.GetChild(btn.Parent,child) as ButtonMainMenu;
                if (childBtn != null && childBtn != btn)childBtn.IsChecked = false;
            }

            btn.IsChecked = true;
            ShowPage(GetType().Assembly.GetType("BackupEssentials."+btn.ClickPage,false));
        }

        private void OnDragEnter(object sender, System.Windows.DragEventArgs e){
            if (DropData == null && e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))DropData = e.Data.GetData(System.Windows.DataFormats.FileDrop) as string[];
            else e.Effects = System.Windows.DragDropEffects.None;

            e.Handled = true;
        }

        private void OnDragLeave(object sender, System.Windows.DragEventArgs e){
            DropData = null;
        }

        private void OnDragDrop(object sender, System.Windows.DragEventArgs e){
            if (DropData != null){
                // TODO display backup page
                DropData = null;
            }
        }

        public void ShowPage(Type pageType){
            ShowPage(pageType,null);
        }

        public void ShowPage(Type pageType, object data){
            Page page = null;
            ContentFrame.Navigate(pageType == null ? null : page = AppPageManager.GetPage(pageType));

            IPageShowData pageDataHandler = page as IPageShowData;
            if (pageDataHandler != null)pageDataHandler.OnShow(data);

            if (!page.AllowDrop){
                page.AllowDrop = true;
                page.DragEnter += new System.Windows.DragEventHandler(OnDragEnter);
                page.DragLeave += new System.Windows.DragEventHandler(OnDragLeave);
                page.Drop += new System.Windows.DragEventHandler(OnDragDrop);
            }
        }
    }
}