using System;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using System.Collections.Generic;
using System.Windows.Shapes;
using System.Windows.Media;
using Microsoft.Phone.Tasks;

namespace FindIT
{
    public partial class MainPage : PhoneApplicationPage
    {
        private Camera cam;
        private Recognizer rec;
        private string[] str, urls;

        public static MainPage main;

        private static Brush[] bru = new Brush[] { null, new SolidColorBrush(Colors.Red), new SolidColorBrush(Colors.Blue), new SolidColorBrush(Colors.Green) };

        public MainPage()
        {
            InitializeComponent();

            main = this;
        }

        /// <summary>
        /// Обновить экран
        /// </summary>
        private void ReplaceFrame()
        {
            canvas.Dispatcher.BeginInvoke(new Action(() =>
            {
                canvas.Children.Clear();

                float scalex = (float)Functions.ImageWidth / (float)canvas.RenderSize.Width, scaley = (float)Functions.ImageHeight / (float)canvas.RenderSize.Height;
                Queue<RecognizedRectangle> l = rec.List;
                foreach (RecognizedRectangle r in l)
                {
                    Rectangle rect = new Rectangle();
                    Canvas.SetLeft(rect, r.X / scalex);
                    Canvas.SetTop(rect, r.Y / scaley);
                    rect.Width = r.Width / scalex;
                    rect.Height = r.Height / scaley;
                    rect.Stroke = bru[(current_id = (int)r.prob)];
                    canvas.Children.Add(rect);

                    Button btn = new Button();
                    btn.Content = str[current_id];
                    btn.Margin = new System.Windows.Thickness(10);
                    btn.Click += CheckInformation;
                    canvas.Children.Add(btn);
                    canvas.UpdateLayout();
                    Canvas.SetLeft(btn, (int)((r.X + r.Width / 2) / scalex) - btn.RenderSize.Width / 2);
                    Canvas.SetTop(btn, (int)((r.Y + r.Height / 2) / scaley) - btn.RenderSize.Height / 2);
                }
            }));
        }

        private int current_id;

        /// <summary>
        /// Вывести информацию об объекте
        /// </summary>
        private void CheckInformation(object sender, System.Windows.RoutedEventArgs e)
        {
            WebBrowserTask t = new WebBrowserTask();
            t.Uri = new Uri(urls[current_id]);
            t.Show();
        }

        public void Deactive()
        {
             cam.Stop();
        }

        public void Active()
        {
             cam.Start();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            popup1.IsOpen = false;
        }

        internal void Show(string p)
        {
            text_popup.Text = p;
            popup1.IsOpen = true;
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if (e.Orientation == PageOrientation.PortraitUp)
            {
                RotateTransform tr = new RotateTransform();
                tr.Angle = 90;
                tr.CenterX = 0.5;
                tr.CenterY = 0.5;
                canvas.RenderTransform = tr;
            }
            else
                if (e.Orientation == PageOrientation.LandscapeLeft)
                {
                    RotateTransform tr = new RotateTransform();
                    tr.Angle = 0;
                    tr.CenterX = 0.5;
                    tr.CenterY = 0.5;
                    canvas.RenderTransform = tr;
                }
        }

        private void PhoneApplicationPage_Loaded_1(object sender, System.Windows.RoutedEventArgs e)
        {

            cam = new Camera(canvas);
            cam.OnFrameAvailable += ReplaceFrame;

            rec = new Recognizer(cam);

            str = new string[] { "", "Васнецов \"Три богатыря\"", "Шишкин \"Утро в сосновом бору\"" };
            urls = new string[] { "", "https://ru.wikipedia.org/wiki/%D0%91%D0%BE%D0%B3%D0%B0%D1%82%D1%8B%D1%80%D0%B8_(%D0%BA%D0%B0%D1%80%D1%82%D0%B8%D0%BD%D0%B0_%D0%92%D0%B0%D1%81%D0%BD%D0%B5%D1%86%D0%BE%D0%B2%D0%B0)", "https://ru.wikipedia.org/wiki/%D0%A3%D1%82%D1%80%D0%BE_%D0%B2_%D1%81%D0%BE%D1%81%D0%BD%D0%BE%D0%B2%D0%BE%D0%BC_%D0%BB%D0%B5%D1%81%D1%83" };
            
            if (this.Orientation == PageOrientation.PortraitUp)
            {
                RotateTransform tr = new RotateTransform();
                tr.Angle = 90;
                tr.CenterX = 0.5;
                tr.CenterY = 0.5;
                canvas.RenderTransform = tr;
            }
            else
                if (this.Orientation == PageOrientation.LandscapeLeft)
                {
                    RotateTransform tr = new RotateTransform();
                    tr.Angle = 0;
                    tr.CenterX = 0.5;
                    tr.CenterY = 0.5;
                    canvas.RenderTransform = tr;
                }
        }
    }
}