using Microsoft.Extensions.Logging;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Beam.Abstractions;
using Beam.Downloaders;
using Beam.Models;
using Beam.Stealth.Strategies;

namespace Beam.Stealth {
    using File = System.IO.File;
    
    public class StealthUnitDownloader<OutType> : UnitDownloader<OutType> {
        public StealthConfig Config { get; }
        public StealthAsyncManipulator Manipulator { get; }
        private ILogger? Logger => Config.Logger;

        private IDownloadStrategy _downloadStrategy { get; }
        
        public StealthUnitDownloader(UnitDownloaderOptions<OutType> options, StealthConfig config, StealthAsyncManipulator manipulator) : base(options) {
            Config = config;
            Manipulator = manipulator;

            _downloadStrategy = options.Target switch {
                DownloadTarget.URL or DownloadTarget.InURL => new PageDownloadStrategy(),
                DownloadTarget.Complex => new WaitingDownloadStrategy(),
                _ => throw new NotSupportedException() // TODO add an exception message
            };
        }

        protected override async Task DownloadToStream(string url, int bufferSize, Stream destinationStream,
            IProgress<IDownloadReport> progress, CancellationToken ct) {
            var driver = Config.Driver;
            await driver.Navigate().GoToUrlAsync(url);
            await Manipulator(driver);

            await _downloadStrategy.DownloadToStream(url, bufferSize, destinationStream, progress, Config, Logger, ct);
        }


    }
}
