using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace Jeti_Test
{
    public class Polygon_vm : INotifyPropertyChanged
    {
        private PointCollection _points;
        //private ObservableCollection<Point> _points;
        public event PropertyChangedEventHandler PropertyChanged;
        private double XWidth = 314;

        //public ObservableCollection<Point> Points
        public PointCollection Points
        {
            get => _points;
            set
            {
                _points = value;
                OnPropertyChanged(nameof(Points));
            }
        }
        public Polygon_vm()
        {
            _points = new()
            //_points = new ObservableCollection<Point>
            {
                new Point(0, 418),
                new Point(3, 400),
                new Point(6, 397),
                new Point(8, 388),
                new Point(11, 373),
                new Point(14, 328),
                new Point(17, 295),
                new Point(20, 257),
                new Point(23, 215),
                new Point(25, 198),
                new Point(28, 190),
                new Point(31, 230),
                new Point(34, 164),
                new Point(37, 153),
                new Point(40, 123),
                new Point(42, 90),
                new Point(45, 93),
                new Point(48, 85),
                new Point(51, 81),
                new Point(54, 87),
                new Point(57, 69),
                new Point(59, 61),
                new Point(62, 81),
                new Point(65, 58),
                new Point(68, 51),
                new Point(71, 67),
                new Point(74, 65),
                new Point(76, 51),
                new Point(79, 58),
                new Point(82, 51),
                new Point(85, 36),
                new Point(88, 21),
                new Point(91, 16),
                new Point(93, 33),
                new Point(96, 21),
                new Point(99, 17),
                new Point(102, 11),
                new Point(105, 32),
                new Point(108, 33),
                new Point(110, 52),
                new Point(113, 49),
                new Point(116, 44),
                new Point(119, 36),
                new Point(122, 107),
                new Point(125, 79),
                new Point(127, 56),
                new Point(130, 41),
                new Point(133, 48),
                new Point(136, 60),
                new Point(139, 58),
                new Point(142, 72),
                new Point(144, 88),
                new Point(147, 73),
                new Point(150, 76),
                new Point(153, 82),
                new Point(156, 112),
                new Point(159, 113),
                new Point(161, 93),
                new Point(164, 89),
                new Point(167, 92),
                new Point(170, 96),
                new Point(173, 104),
                new Point(176, 117),
                new Point(178, 167),
                new Point(181, 163),
                new Point(184, 156),
                new Point(187, 131),
                new Point(190, 109),
                new Point(193, 114),
                new Point(195, 241),
                new Point(198, 207),
                new Point(201, 184),
                new Point(204, 85),
                new Point(207, 50),
                new Point(210, 14),
                new Point(212, 4),
                new Point(215, 0),
                new Point(218, 285),
                new Point(221, 214),
                new Point(224, 23),
                new Point(227, 15),
                new Point(229, 18),
                new Point(232, 35),
                new Point(235, 82),
                new Point(238, 69),
                new Point(241, 91),
                new Point(244, 83),
                new Point(246, 112),
                new Point(249, 230),
                new Point(252, 189),
                new Point(255, 160),
                new Point(258, 186),
                new Point(261, 150),
                new Point(263, 125),
                new Point(266, 112),
                new Point(269, 134),
                new Point(272, 155),
                new Point(275, 125),
                new Point(278, 141),
                new Point(280, 133),
                new Point(283, 141),
                new Point(286, 150),
                new Point(289, 156),
                new Point(292, 161),
                new Point(295, 245),
                new Point(297, 305),
                new Point(300, 243),
                new Point(303, 304),
                new Point(306, 320),
                new Point(309, 268),
                new Point(312, 294),
                new Point(314, 367),
                new Point(317, 403),
                new Point(320, 366),
                new Point(323, 389),
                new Point(326, 401),
                new Point(329, 374),
                new Point(331, 382),
                new Point(334, 360),
                new Point(337, 315),
                new Point(340, 352),
                new Point(343, 323),
                new Point(346, 294),
                new Point(348, 284),
                new Point(351, 264),
                new Point(354, 282),
                new Point(354, 418)
            };
        }
        public void BuildPolygon(float[] arr, int XSpan, int YSpan)
        {
            _points.Clear();
            _points.Add(new Point(0, 0));
            int len = arr.Length;
            float xval = 0;
            float ymax = 0;
            foreach (float p in arr)
            {
                if (p > ymax) ymax = p;
            }

            foreach (float p in arr)
            {
                xval += (float)XSpan / len;
                _points.Add(new Point((int)(xval + 0.5), (int)(p * (YSpan + 0.5) / ymax)));
            }
            _points.Add(new Point(XSpan, 0));
            OnPropertyChanged(nameof(Points));
        }
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
