using System;
using Microsoft.SPOT;

namespace CPF
{
    public class NMEA_Date
    {
        private bool _valid;
        private bool _updated;
        private uint _date;
        private uint _newDate;
        private long _lastCommitTime;

        public void Set(ByteString s) 
        {
            _newDate = (uint) Conversions.ByteStringToInteger(s);
        }

        public void Commit() 
        {
            _date = _newDate;
            _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 Year() 
        {
            _updated = false;
            uint year = _date % 100;
            return year + 2000;
        }

        public uint Month() 
        {
            _updated = false;
            return (_date / 100) % 100;
        }

        public uint Day() 
        {
            _updated = false;
            return _date / 10000;
        }

        public NMEA_Date()
        {
            _valid = _updated = false;
            _date = 0;
            _lastCommitTime = 0;
        }
    }
}
