using Editor.Extensions;
using Igdc;
using Igdc.Scenes;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Editor
{
    public partial class MainWindow
    {
        private RoutedCommand NewCommand { get; set; } = new RoutedCommand();
        private RoutedCommand OpenCommand { get; set; } = new RoutedCommand();
        private RoutedCommand SaveCommand { get; set; } = new RoutedCommand();
        private RoutedCommand SaveAsCommand { get; set; } = new RoutedCommand();
        private RoutedCommand RunCommand { get; set; } = new RoutedCommand();

        private const string Filter = "JavaScript Object Notation File (*.json)|*.json|All files(*.*)|*.*";

        private string SavePath
        {
            get
            {
                if (string.IsNullOrEmpty(_savePath))
                {
                    var dialog = new SaveFileDialog { Filter = Filter };
                    if (dialog.ShowDialog(GetWindow(this)) == true)
                        _savePath = dialog.FileName;
                }
                return _savePath;
            }
            set { _savePath = value; }
        }
        private string _savePath;

        public MainWindow()
        {
            InitializeComponent();
            RegisterShortcut(NewCommand, New_Click, Key.N, ModifierKeys.Control);
            RegisterShortcut(OpenCommand, Open_Click, Key.O, ModifierKeys.Control);
            RegisterShortcut(SaveCommand, Save_Click, Key.S, ModifierKeys.Control);
            RegisterShortcut(SaveAsCommand, SaveAs_Click, Key.S, ModifierKeys.Control | ModifierKeys.Alt);
            RegisterShortcut(RunCommand, Run_Click, Key.F5, ModifierKeys.None);
        }

        private void RegisterShortcut(RoutedCommand command, ExecutedRoutedEventHandler handler, Key key, ModifierKeys modifiers)
        {
            command.InputGestures.Add(new KeyGesture(key, modifiers));
            CommandBindings.Add(new CommandBinding(command, handler));
        }

        private void New_Click(object sender, RoutedEventArgs e)
        {
            Canvas.Map = new Point[10, 10];
        }

        private void Open_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog { Filter = Filter };
            if (dialog.ShowDialog(GetWindow(this)) != true) return;
            var path = dialog.FileName;
            if (string.IsNullOrEmpty(path)) return;
            var json = File.ReadAllText(path);
            var dto = json.FromJson<MapDto>();
            Canvas.Map = dto.ToEditorFriendly();
            SavePath = path;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(SavePath)) return;
            var json = Canvas.Map.ToDto().ToJson();
            File.WriteAllText(SavePath, json, Encoding.UTF8);
        }

        private void SaveAs_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Run_Click(object sender, RoutedEventArgs e)
        {
            Hide();
            IsEnabled = false;
            Program.Main(Canvas.Map.ToDto());
            IsEnabled = true;
            Show();
        }

        private void ApplySize_Click(object sender, RoutedEventArgs e)
        {
            int.TryParse(XSizeBox.Text, out int x);
            int.TryParse(YSizeBox.Text, out int y);
            Canvas.SetSize(x, y);
        }

        private void ToolSelector_Selected(object sender, Point e)
        {
            Canvas.Tool = e;
        }

        private void Canvas_PointerOver(object sender, Point e)
        {
            var x = (int)e.X;
            var y = (int)e.Y;
            CurrentPointBlock.Text = $"(X: {x}; Y: {y};)";
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var textbox = (TextBox)sender;
            textbox.SelectAll();
        }
    }
}