using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
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.Navigation;
using System.Windows.Shapes;
using GaugeTester.Annotations;
using Telerik.Windows.Persistence.Services;

namespace GaugeTester
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private string m_val;
        private double m_max;
        private double m_min;
        private double m_tickOffset;

        Timer timer= new Timer(800);
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }

        private double i;
        public string Val
        {
            get => m_val;
            set
            {
                m_val = value;
                OnPropertyChanged(nameof(Val));
            }
        }

        public double Max
        {
            get => m_max;
            set
            {
                m_max = value;
                OnPropertyChanged(nameof(Max));
            }
        }

        public double Min
        {
            get => m_min;
            set
            {
                m_min = value;
                OnPropertyChanged(nameof(Min));
            }
        }

        public double TickOffset
        {
            get => m_tickOffset;
            set
            {
                m_tickOffset = value;
                OnPropertyChanged(nameof(TickOffset));
            }
        }

        private bool m_ShowLastLabel;
        private bool m_ShowFirstLabel;

        public bool ShowLastLabel
        {
            get => m_ShowLastLabel;
            set
            {
                m_ShowLastLabel = value;
                OnPropertyChanged(nameof(ShowLastLabel));
            }
        }
        public bool ShowFirstLabel
        {
            get => m_ShowFirstLabel;
            set
            {
                m_ShowFirstLabel = value;
                OnPropertyChanged(nameof(ShowFirstLabel));
            }
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            timer.Stop();
            Method(i++);
            timer.Start();
        }

        private void Method(double value)
        {
            Val = value.ToString();
            Min = value - 10;
            Max = value + 10;
            double startValue = Math.Round(Min / 10) * 10;
            Dispatcher.Invoke(() =>
            {
                TickOffset = startValue - Min;
                ShowFirstLabel = startValue >= Min;
                var lastValue = startValue + 20;
                ShowLastLabel = lastValue <= Max;

            });



        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
