<commit_before>﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using TwitchLeecher.Core.Models;
using TwitchLeecher.Gui.Services;
using TwitchLeecher.Shared.Commands;
using TwitchLeecher.Shared.IO;

namespace TwitchLeecher.Gui.ViewModels
{
    public class DownloadWindowVM : DialogVM<DownloadParameters>
    {
        #region Fields

        private TwitchVideo video;
        private TwitchVideoResolution resolution;

        private string folder;
        private string filename;

        private ICommand chooseCommand;
        private ICommand downloadCommand;
        private ICommand cancelCommand;

        private IGuiService guiService;

        private object searchLock = new object();

        #endregion Fields

        #region Constructors

        public DownloadWindowVM(IGuiService guiService)
        {
            this.guiService = guiService;
        }

        #endregion Constructors

        #region Properties

        public TwitchVideo Video
        {
            get
            {
                return this.video;
            }
            set
            {
                this.SetProperty(ref this.video, value, nameof(this.Video));
            }
        }

        public TwitchVideoResolution Resolution
        {
            get
            {
                return this.resolution;
            }
            set
            {
                this.SetProperty(ref this.resolution, value, nameof(this.Resolution));
            }
        }

        public string Folder
        {
            get
            {
                return this.folder;
            }
            set
            {
                this.SetProperty(ref this.folder, value, nameof(this.Folder));
            }
        }

        public string Filename
        {
            get
            {
                return this.filename;
            }
            set
            {
                this.SetProperty(ref this.filename, value, nameof(this.Filename));
            }
        }

        public ICommand ChooseCommand
        {
            get
            {
                if (this.chooseCommand == null)
                {
                    this.chooseCommand = new DelegateCommand(this.Choose);
                }

                return this.chooseCommand;
            }
        }

        public ICommand DownloadCommand
        {
            get
            {
                if (this.downloadCommand == null)
                {
                    this.downloadCommand = new DelegateCommand<Window>(this.Download);
                }

                return this.downloadCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (this.cancelCommand == null)
                {
                    this.cancelCommand = new DelegateCommand<Window>(this.Cancel);
                }

                return this.cancelCommand;
            }
        }

        #endregion Properties

        #region Methods

        private void Choose()
        {
            try
            {
                this.guiService.ShowFolderBrowserDialog(this.folder, this.ChooseCallback);
            }
            catch (Exception ex)
            {
                this.guiService.ShowAndLogException(ex);
            }
        }

        private void ChooseCallback(bool cancelled, string folder)
        {
            try
            {
                if (!cancelled)
                {
                    this.Folder = folder;
                }
            }
            catch (Exception ex)
            {
                this.guiService.ShowAndLogException(ex);
            }
        }

        private void Download(Window window)
        {
            try
            {
                this.Validate();

                if (!this.HasErrors)
                {
                    string filename = Path.Combine(this.folder, this.filename);
                    this.ResultObject = new DownloadParameters(this.video, this.resolution, filename);
                    window.DialogResult = true;
                    window.Close();
                }
            }
            catch (Exception ex)
            {
                this.guiService.ShowAndLogException(ex);
            }
        }

        private void Cancel(Window window)
        {
            try
            {
                this.ResultObject = null;
                window.DialogResult = false;
                window.Close();
            }
            catch (Exception ex)<commit_msg>If download file exists, ask for overwrite action<commit_after>﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using TwitchLeecher.Core.Models;
using TwitchLeecher.Gui.Services;
using TwitchLeecher.Shared.Commands;
using TwitchLeecher.Shared.IO;

namespace TwitchLeecher.Gui.ViewModels
{
    public class DownloadWindowVM : DialogVM<DownloadParameters>
    {
        #region Fields

        private TwitchVideo video;
        private TwitchVideoResolution resolution;

        private string folder;
        private string filename;

        private ICommand chooseCommand;
        private ICommand downloadCommand;
        private ICommand cancelCommand;

        private IGuiService guiService;

        private object searchLock = new object();

        #endregion Fields

        #region Constructors

        public DownloadWindowVM(IGuiService guiService)
        {
            this.guiService = guiService;
        }

        #endregion Constructors

        #region Properties

        public TwitchVideo Video
        {
            get
            {
                return this.video;
            }
            set
            {
                this.SetProperty(ref this.video, value, nameof(this.Video));
            }
        }

        public TwitchVideoResolution Resolution
        {
            get
            {
                return this.resolution;
            }
            set
            {
                this.SetProperty(ref this.resolution, value, nameof(this.Resolution));
            }
        }

        public string Folder
        {
            get
            {
                return this.folder;
            }
            set
            {
                this.SetProperty(ref this.folder, value, nameof(this.Folder));
            }
        }

        public string Filename
        {
            get
            {
                return this.filename;
            }
            set
            {
                this.SetProperty(ref this.filename, value, nameof(this.Filename));
            }
        }

        public ICommand ChooseCommand
        {
            get
            {
                if (this.chooseCommand == null)
                {
                    this.chooseCommand = new DelegateCommand(this.Choose);
                }

                return this.chooseCommand;
            }
        }

        public ICommand DownloadCommand
        {
            get
            {
                if (this.downloadCommand == null)
                {
                    this.downloadCommand = new DelegateCommand<Window>(this.Download);
                }

                return this.downloadCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (this.cancelCommand == null)
                {
                    this.cancelCommand = new DelegateCommand<Window>(this.Cancel);
                }

                return this.cancelCommand;
            }
        }

        #endregion Properties

        #region Methods

        private void Choose()
        {
            try
            {
                this.guiService.ShowFolderBrowserDialog(this.folder, this.ChooseCallback);
            }
            catch (Exception ex)
            {
                this.guiService.ShowAndLogException(ex);
            }
        }

        private void ChooseCallback(bool cancelled, string folder)
        {
            try
            {
                if (!cancelled)
                {
                    this.Folder = folder;
                }
            }
            catch (Exception ex)
            {
                this.guiService.ShowAndLogException(ex);
            }
        }

        private void Download(Window window)
        {
            try
            {
                this.Validate();

                if (!this.HasErrors)
                {
                    string filename = Path.Combine(this.folder, this.filename);

                    if (File.Exists(filename))
                    {
                        MessageBoxResult result = this.guiService.ShowMessageBox("The file already exists. Do you want to overwrite it?", "Download", MessageBoxButton.YesNo, MessageBoxImage.Question);

                        if (result != MessageBoxResult.Yes)
                        {
                            return;
                        }
                    }

                    this.ResultObject = new DownloadParameters(this.video, this.resolution, filename);
                    window.DialogResult = true;
                    window.Close();
                }
            }
            catch (Exception ex)
            {
                this.guiService.ShowAndLogException(ex);
            }
        }

        private void Cancel(Window window)
        {
            try
            {
                this.ResultObject = null;
                window.DialogResult = false;
                window.Close();
            }
            catch (Exception ex)
{'fraction_non_alphanumeric': 0.06670842467898529, 'fraction_numerical': 0.0, 'mean_word_length': 1.0839678050902763, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5010938', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2120, 'n_words': 451}