using System;
using System.IO;
using System.Windows.Threading;
using DPackLib;

namespace McLaren_WPF.Controls
{

    public partial class FuelPanel 
    {

        public string Description { get; set; }
        private string dataPath { get; set; }
        private DispatcherTimer resTimer;
        BinSerializer serializer { get; set; }
        private double fuel = 20;

        public FuelPanel()
        {
            InitializeComponent();
        }

        public void StartReception()
        {
            DescriptionTxt.Text = Description;
            dataPath = Path.Combine(CommonConstants.RootDir, Description + ".dat");
            serializer = new BinSerializer();
            CreateTimer();
        }

        private void CreateTimer()
        {
            resTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
            resTimer.Tick += GenTimer_Tick;
            resTimer.Start();
        }

        private void GenTimer_Tick(object sender, EventArgs e)
        {
            ReadData();
        }

        private void ReadData()
        {
            DataPackEventArgs dataPack = serializer.DeSerializeData(dataPath);
            if (dataPack == null) return;
            InfoTxt.Text = dataPack.Message;

            DelayTxt.Text = "Delay: "
                + new TimeSpan(DateTime.Now.Ticks - dataPack.DateTimeTicks).TotalMilliseconds.ToString("0") + " ms";

            if(fuel>0)
            { fuel -= dataPack.Data / 50;}

            FuelIndicator.Value = fuel;
            FuelTxt.Text = fuel.ToString("0.0") + " Liters";
        }

    }
}
