//  
//   Rebex Sample Code License
// 
//   Copyright 2026, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;

namespace Rebex.Samples
{
    /// <summary>
    /// Represents progress of a download operation.
    /// </summary>
    public struct DownloadProgressInfo
    {
        private double? _downloadedBytesInMb;
        private double? _percentsDownloaded;
        public const int BYTES_IN_MEGABYTE = 1024 * 1024;
        public const double UNKNOWN_VALUE = -1d;

        public DownloadProgressInfo(long? totalLength, long downloadedBytes, string fileName, TimeSpan elapsedTime)
        {
            TotalLength = totalLength;
            TotalLengthInMB =  totalLength == null
                ? (double?)null
                : (double)totalLength / BYTES_IN_MEGABYTE;
            DownloadedBytes = downloadedBytes;
            _downloadedBytesInMb = null;
            _percentsDownloaded = null;
            FileName = fileName;
            ElapsedTime = elapsedTime;
        }

        private DownloadProgressInfo(long? totalLength, double? totalLengthInMb, long downloadedBytes, string fileName, TimeSpan elapsedTime)
        {
            TotalLength = totalLength;
            TotalLengthInMB = totalLengthInMb;
            DownloadedBytes = downloadedBytes;
            _downloadedBytesInMb = null;
            _percentsDownloaded = null;
            FileName = fileName;
            ElapsedTime = elapsedTime;
        }

        public long? TotalLength { get; }
        public double? TotalLengthInMB { get; }
        public long DownloadedBytes { get; }

        public double DownloadedBytesInMB
        {
            get
            {
                if (!_downloadedBytesInMb.HasValue)
                {
                    _downloadedBytesInMb = (double)DownloadedBytes / BYTES_IN_MEGABYTE;
                }

                return _downloadedBytesInMb.Value;
            }
        }

        public double PercentsDownloaded
        {
            get
            {
                if (_percentsDownloaded == null)
                {
                    _percentsDownloaded = TotalLength == null
                        ? UNKNOWN_VALUE
                        : (double)DownloadedBytes / TotalLength.Value;
                }

                return _percentsDownloaded.Value;
            }
        }

        public string FileName { get; }
        public TimeSpan ElapsedTime { get; }

        public DownloadProgressInfo With(TimeSpan elapsedTime, long downloadedBytes)
        {
            return new DownloadProgressInfo(TotalLength, TotalLengthInMB, downloadedBytes, FileName, elapsedTime);
        }
    }
}
