using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Navigation;

using C1.Xaml.Chart;

namespace FlappyChart
{
    public sealed partial class MainPage : Page
    {
        DispatcherTimer dt;

        Bird bird;
        Walls walls;
        int highscore = 0;

        public MainPage()
        {
            this.InitializeComponent();

            // setup chart
            chart.ChartType = ChartType.Line;
            chart.View.AxisY.Min = -1000;
            chart.View.AxisY.Max = 1000;

            chart.View.AxisX.MajorGridStroke = null;
            chart.View.AxisX.AxisLine = new Line() { StrokeThickness = 0 };
            chart.View.AxisX.MajorTickHeight = 0;
            chart.View.AxisX.MinorTickHeight = 0;
            chart.View.AxisX.FontSize = 24;
            chart.View.AxisY.AnnoPosition = AnnoPosition.None;
            chart.View.AxisY.AxisLine = new Line() { StrokeThickness = 0 };
            chart.View.AxisY.MajorGridStroke = null;
            chart.View.AxisY.MajorTickHeight = 0;
            chart.View.AxisY.MinorTickHeight = 0;
            chart.View.Margin = new Thickness(10);
            chart.View.PlotShape.Stroke = new SolidColorBrush(Colors.Black);
            chart.View.PlotShape.StrokeThickness = 2;

            // game objects
            walls = new Walls(chart);
            bird = new Bird(chart);

            // interaction
            chart.PointerPressed += (s, e) =>
                {
                    if (dt.IsEnabled)
                    {
                        bird.Flap();
                        if (GameOptions.PlaySounds)
                            mediaFlap.Play();
                    }
                    else
                    {
                        title.Text = "";
                        dt.Start();
                    }
                };

            // game timer
            dt = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(0.03) };
            dt.Tick += (s, e) => Update();

            Restart();
        }

        DateTime? lastUpdate = null;

        // game cycle
        void Update()
        {
            if (lastUpdate != null)
            {
                var now = DateTime.Now;
                var time = (now - lastUpdate.Value).TotalSeconds;
                lastUpdate = now;

                chart.BeginUpdate();

                var dx = 0.5 * time * GameOptions.WallSpeed;

                chart.View.AxisX.Min += dx;
                chart.View.AxisX.Max += dx;

                bird.Update(time);

                if (!bird.IsAlive)
                {
                    if (GameOptions.PlaySounds)
                        mediaCrash.Play();
                    Restart();
                }
                if (walls.Overlap(bird))
                {
                    if (GameOptions.PlaySounds)
                        mediaCrash.Play();
                    Restart();
                }

                // end of level
                if (chart.View.AxisX.Min > GameOptions.NumberOfSteps)
                    Restart();

                var x = (int)Math.Floor(0.5 * (chart.View.AxisX.Min + chart.View.AxisX.Max));
                if (x > highscore)
                    highscore = x;

                chart.EndUpdate();
            }
            else
                lastUpdate = DateTime.Now;
        }

        void Restart()
        {
            dt.Stop();
            lastUpdate = null;
            chart.View.AxisX.Min = -5;
            chart.View.AxisX.Max = 0;
            walls.CreateNewLevel(GameOptions.NumberOfSteps);
            bird.DataPoint = new Point(0.5 * (chart.View.AxisX.Min + chart.View.AxisX.Max), 0);
            bird.Resurrect();

            title.Text = "Tap to start";
            score.Text = string.Format("High score: {0}", highscore);
        }
    }
}
