using System;
using Microsoft.SPOT;

namespace CPF
{
    public class NMEA_Time
    {
        private bool _valid;
        private bool _updated;
        private uint _time;
        private uint _newTime;
        private long _lastCommitTime;

        public void Set(ByteString s) 
        {
            _newTime = (uint) Conversions.ParseDecimal(s);
        }

        public void Commit() 
        {
            _time = _newTime;
            _lastCommitTime = Utils.MillisecondsSinceLastReboot();
            _valid = _updated = true;
        }
        
        public bool IsValid() 
        { 
            return _valid; 
        }

        public bool IsUpdated() 
        { 
            return _updated; 
        }
        
        public long Age() 
        { 
            return _valid ? Utils.MillisecondsSinceLastReboot() - _lastCommitTime : Conversions.MAX_SIGNED; 
        }

        public uint Value() 
        { 
            _updated = false; 
            return _time; 
        }

        public uint Hour() 
        {
            _updated = false;
            return _time / 1000000;
        }

        public uint Minute() 
        {
            _updated = false;
            return (_time / 10000) % 100;
        }

        public uint Second() 
        {
            _updated = false;
            return (_time / 100) % 100;
        }

        public uint Centisecond() 
        {
            _updated = false;
            return _time % 100;
        }

        public NMEA_Time()
        {
            _valid = false;
            _updated = false;
            _time = 0;
            _lastCommitTime = 0;
        }
    }
}
