using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;


namespace VisualPathFinder
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : System.Windows.Window
    {
        PathFind pathFinder;
        long ticks;
        bool run;
        private readonly EventHandler CompositionTarget_RenderingHandler;

        public Window1()
        {
            InitializeComponent();
            pathFinder = new PathFind();
            PathSettings.DataContext = pathFinder;
            run = false;
            CompositionTarget_RenderingHandler = new EventHandler(CompositionTarget_Rendering);
            this.Loaded += new RoutedEventHandler(_Loaded);
            this.Unloaded += new RoutedEventHandler(_Unloaded);
        }

        void _Loaded(object sender, RoutedEventArgs e)
        {
            CompositionTarget.Rendering += CompositionTarget_RenderingHandler;
            moveDelay.TextChanged += new TextChangedEventHandler(moveDelay_TextChanged);
            Breadth.DataContext = pathFinder;
        }

        void moveDelay_TextChanged(object sender, TextChangedEventArgs e)
        {
            ticks = System.DateTime.Now.Ticks;
        }

        void _Unloaded(object sender, RoutedEventArgs e)
        {
            CompositionTarget.Rendering -= CompositionTarget_RenderingHandler;
        }

        void highlight_click(object sender, RoutedEventArgs e)
        {
            pathFinder.PathHighlight = !pathFinder.PathHighlight;
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (run)
            {
                if (moveDelay.Text.ToUpper() != "MIN")
                {
                    long diff = System.DateTime.Now.Ticks - ticks;
                    double seconds;
                    try
                    {
                        seconds = Double.Parse(moveDelay.Text);
                    }
                    catch (Exception ex)
                    {
                        seconds = 1;
                    }
                    if (seconds == 0)
                    {
                        seconds = .000001;
                    }
                    int runs = (int)Math.Floor((double)diff / (System.TimeSpan.TicksPerSecond * seconds));
                    long remaining = diff % (long)(System.TimeSpan.TicksPerSecond * seconds);
                    while (runs > 0)
                    {
                        if (pathFinder.run(Map))
                        {
                            stop();
                            break;
                        }
                        runs--;
                    }
                    ticks = System.DateTime.Now.Ticks - remaining;
                }
                else
                {
                    if (pathFinder.run(Map))
                    {
                        stop();
                    }
                }
            }
        }

        void btn_Clear(object sender, RoutedEventArgs e)
        {
            stop();
            Map.clearPath();
        }

        void btn_ClearAll(object sender, RoutedEventArgs e)
        {
            stop();
            Map.clearAll();
        }

        void btn_Run(object sender, RoutedEventArgs e)
        {
            if (!run)
            {
                Map.clearPath();
                pathFinder.setup(Map);
                run = true;
                Best.IsEnabled = false;
                Breadth.IsEnabled = false;
                columns.IsEnabled = false;
                rows.IsEnabled = false;
                ticks = System.DateTime.Now.Ticks;
                x_RunButton.Content = "Stop";
            }
            else
            {
                stop();
            }
        }

        void change_rows(object sender, RoutedEventArgs e)
        {
            if (rows.Text != "")
            {
                int rowsCount = Int32.Parse(rows.Text);
                if(rowsCount > 200)
                {
                    rowsCount = 200;
                    rows.Text = "200";
                }
                Map.Rows = rowsCount;
            }
        }

        void optimal_click(object sender, RoutedEventArgs e)
        {
            pathFinder.Optimal = ((CheckBox)sender).IsChecked.Value;
        }

        void diagonals_click(object sender, RoutedEventArgs e)
        {
            pathFinder.AllowDiagonals = ((CheckBox)sender).IsChecked.Value;
        }

        void randomPath_Click(object sender, RoutedEventArgs e)
        {
            pathFinder.RandomMoves = ((CheckBox)sender).IsChecked.Value;
        }

        void change_columns(object sender, RoutedEventArgs e)
        {
            if (columns.Text != "")
            {
                int cols = Int32.Parse(columns.Text);
                if(cols > 200)
                {
                    cols = 200;
                    columns.Text = "200";
                }
                Map.Columns = cols;
            }
        }

        void stop()
        {
            x_RunButton.Content = "Run";
            run = false;
            Best.IsEnabled = true;
            Breadth.IsEnabled = true;
            rows.IsEnabled = true;
            columns.IsEnabled = true;
        }

        void btn_Random(object sender, RoutedEventArgs e)
        {
            stop();
            Map.fillRandom();
        }

    }
}