using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RobotLib
{
    public class DevBattery : DevBase
    {
        private float voltage;

        public event EventHandler<BatteryEventArgs> BatteryChanged;

        public float Voltage 
        { 
            get { return voltage; }
            private set
            {
                // value changed?
                if(voltage != value)
                {
                    // set new voltage and raise event
                    voltage = value;
                    BatteryChanged?.Invoke(this, new BatteryEventArgs(voltage));
                }
            }
        }

        public DevBattery(Com com) : base(com, "battery") { }

        public override void Refresh()
        {
            SendMessage("get battery status!");
        }

        protected override void ParseMessage(string message)
        {
            // example message = "Battery: 1.25 V"
            var keyValue = message.Trim().Split(':');
            string key = keyValue[0].Trim();
            string value = keyValue[1].Trim();

            switch (key)
            {
                case "battery":
                    value = value.Trim(' ', 'V');
                    this.Voltage = float.Parse(value);
                    break;

                default:
                    log.Warn($"Unkown element {key}: {value} in message {message}");
                    break;
            }
            

        }
    }
}
