<commit_before>﻿using RudeFox.Helpers;
using RudeFox.Models;
using RudeFox.Mvvm;
using RudeFox.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

namespace RudeFox.ViewModels
{
    public class OperationVM : BindableBase
    {
        #region Constructor
        public OperationVM()
        {
            CancellationTokenSource = new CancellationTokenSource();
            TaskProgress = new Progress<int>();
            _lastProgressReport = DateTime.Now;

            CancelCommand = new DelegateCommand(o =>
            {
                CancellationTokenSource.Cancel();
            });

            TaskProgress.ProgressChanged += (sender, newBytes) =>
            {
                BytesComplete += newBytes;

                if (Bytes != -1)
                    Progress = ((double)BytesComplete / Bytes) * 100;
            };
        }
        #endregion

        #region Fields
        private double _oldProgress = 0.0;
        private DateTime _lastProgressReport;
        private LinkedList<double> _progressHistory = new LinkedList<double>();
        #endregion

        #region Properties
        private string _path;
        public string Path
        {
            get { return _path; }
            set
            {
                if (SetProperty(ref _path, value))
                    CalculateBytes();
            }

        }

        private ItemType _type;
        public ItemType Type
        {
            get
            {
                if (_type == ItemType.NotSet)
                {
                    if (File.Exists(Path))
                        Type = ItemType.File;
                    else if (Directory.Exists(Path))
                        Type = ItemType.Folder;
                    else
                        Type = ItemType.Unknown;
                }

                return _type;
            }
            set
            {
                if (SetProperty(ref _type, value))
                {
                    RaisePropertyChanged(nameof(Image));
                }
            }
        }
        
        private double _progress;
        public double Progress
        {
            get { return _progress; }
            private set
            {
                _oldProgress = _progress;
                if (SetProperty(ref _progress, value))
                {
                    var change = _progress - _oldProgress;
                    var interval = (DateTime.Now - _lastProgressReport).TotalSeconds;
                    var rate = change / interval;
                    _progressHistory.AddFirst(rate);

                    var secondsRemaining = (100.0 - _progress) / _progressHistory.Average();
                    TimeRemaining = TimeSpan.FromSeconds(secondsRemaining);
                    _lastProgressReport = DateTime.Now;
                }
            }
        }

        private TimeSpan _timeRemaining;
        public TimeSpan TimeRemaining
        {
            get { return _timeRemaining; }
            private set { SetProperty(ref _timeRemaining, value); }
        }

        private long _bytesComplete;
        public long BytesComplete
        {
            get { return _bytesComplete; }
            set { SetProperty(ref _bytesComplete, value); }
        }<commit_msg>Fixed a few bugs in OperationVM<commit_after>﻿using RudeFox.Helpers;
using RudeFox.Models;
using RudeFox.Mvvm;
using RudeFox.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

namespace RudeFox.ViewModels
{
    public class OperationVM : BindableBase
    {
        #region Constructor
        public OperationVM()
        {
            CancellationTokenSource = new CancellationTokenSource();
            TaskProgress = new Progress<int>();
            _lastProgressReport = DateTime.Now;

            CancelCommand = new DelegateCommand(o =>
            {
                CancellationTokenSource.Cancel();
            });

            TaskProgress.ProgressChanged += (sender, newBytes) =>
            {
                BytesComplete += newBytes;

                if (Bytes != -1)
                    Progress = ((double)BytesComplete / Bytes) * 100;
            };
        }
        #endregion

        #region Fields
        private double _oldProgress = 0.0;
        private DateTime _lastProgressReport;
        private LinkedList<double> _progressHistory = new LinkedList<double>();
        #endregion

        #region Properties
        private string _path;
        public string Path
        {
            get { return _path; }
            set
            {
                if (SetProperty(ref _path, value))
                {
                    RaisePropertyChanged(nameof(Type));
                    RaisePropertyChanged(nameof(Image));
                    CalculateBytes();
                }
            }

        }

        public ItemType Type
        {
            get
            {
                if (File.Exists(Path))
                    return ItemType.File;
                else if (Directory.Exists(Path))
                    return ItemType.Folder;
                else
                    return ItemType.Unknown;
            }
        }

        private double _progress;
        public double Progress
        {
            get { return _progress; }
            private set
            {
                _oldProgress = _progress;
                if (SetProperty(ref _progress, value))
                {
                    var change = _progress - _oldProgress;
                    var interval = (DateTime.Now - _lastProgressReport).TotalSeconds;
                    var rate = change / interval;
                    _progressHistory.AddFirst(rate);

                    if (_progressHistory.Count > 25)
                        _progressHistory.RemoveLast();

                    var secondsRemaining = (100.0 - _progress) / _progressHistory.Average();
                    TimeRemaining = TimeSpan.FromSeconds(secondsRemaining);
                    _lastProgressReport = DateTime.Now;
                }
            }
        }

        private TimeSpan _timeRemaining;
        public TimeSpan TimeRemaining
        {
            get { return _timeRemaining; }
            private set { SetProperty(ref _timeRemaining, value); }
        }

        private long _bytesComplete;
        public long BytesComplete
        {
            get { return _bytesComplete; }
            set { SetProperty(ref _bytesComplete, value); }
        }
{'fraction_non_alphanumeric': 0.0722495894909688, 'fraction_numerical': 0.003284072249589491, 'mean_word_length': 1.354181307097681, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4614843', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1538, 'n_words': 371}