using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using C1.Xaml.Chart;

namespace FlappyChart
{
    public class Bird : ChartPanelObject
    {
        C1Chart chart;
        RotateTransform rt = new RotateTransform();

        const int BIRD_SIZE = 32;

        public Bird(C1Chart chart)
        {
            this.chart = chart;
            DataPoint = new Point(0.5 * (chart.View.AxisX.Min + chart.View.AxisX.Max), 0);
            Gravitaion = 500;
            HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
            VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;

            Content = new Image
            {
                Source = new BitmapImage(new Uri("ms-appx:///Assets/bird.png")),
                Width = BIRD_SIZE,
                Height = BIRD_SIZE,
                Stretch = Stretch.None,
                RenderTransform = rt,
                RenderTransformOrigin = new Point(0.5, 0.5)
            };

            var layer = new ChartPanel();
            layer.Children.Add(this);
            chart.View.Layers.Add(layer);
        }

        public double Diameter
        {
            get { return 0.75*BIRD_SIZE; }
        }

        double Gravitaion { get; set; }

        double Speed { get; set; }
        
        double Angle
        {
            get { return rt.Angle; }
            set { rt.Angle = value; }
        }

        public bool IsAlive
        {
            get { return DataPoint.Y >= chart.View.AxisY.Min && DataPoint.Y <= chart.View.AxisY.Max; }
        }

        public void Update(double time)
        {
            var t = /*GameOptions.Speed */ time * 2.5;
            DataPoint = new Point(0.5 * (chart.View.AxisX.Min + chart.View.AxisX.Max), 
                DataPoint.Y + Speed * t);
            Speed -= Gravitaion * t;

            if (Angle < 90)
                Angle += 30 * t;
        }

        public void Flap()
        {
            Speed = 500;
            Angle = -20;
        }

        public void Resurrect()
        {
            Speed = 0;
            Angle = 0;
        }
    }
}
