using System;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Core.Model.Notification;

namespace NzbDrone.Core.Providers.Jobs
{
    public class NewSeriesUpdate : IJob
    {
        private readonly SeriesProvider _seriesProvider;
        private readonly EpisodeProvider _episodeProvider;
        private readonly MediaFileProvider _mediaFileProvider;
        private readonly SeasonProvider _seasonProvider;

        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        public NewSeriesUpdate(SeriesProvider seriesProvider, EpisodeProvider episodeProvider, MediaFileProvider mediaFileProvider, SeasonProvider seasonProvider)
        {
            _seriesProvider = seriesProvider;
            _episodeProvider = episodeProvider;
            _mediaFileProvider = mediaFileProvider;
            _seasonProvider = seasonProvider;
        }

        public string Name
        {
            get { return "New Series Update"; }
        }

        public int DefaultInterval
        {
            get { return 1; }
        }

        public void Start(ProgressNotification notification, int targetId)
        {
            ScanSeries(notification);
        }

        private void ScanSeries(ProgressNotification notification)
        {
            var syncList = _seriesProvider.GetAllSeries().Where(s => s.LastInfoSync == null).ToList();
            if (syncList.Count == 0)
            {
                return;
            }

            foreach (var currentSeries in syncList)
            {
                try
                {
                    notification.CurrentMessage = String.Format("Searching for '{0}'",
                                                                new DirectoryInfo(currentSeries.Path).Name);
                    var updatedSeries = _seriesProvider.UpdateSeriesInfo(currentSeries.SeriesId);

                    notification.CurrentMessage = String.Format("Downloading episode info for '{0}'",
                                                                updatedSeries.Title);
                    _episodeProvider.RefreshEpisodeInfo(updatedSeries.SeriesId);

                    notification.CurrentMessage = String.Format("Scanning disk for '{0}' files", updatedSeries.Title);
                    _mediaFileProvider.Scan(_seriesProvider.GetSeries(updatedSeries.SeriesId));

                    if (_mediaFileProvider.GetSeriesFiles(currentSeries.SeriesId).Count() != 0)
                    {
                        Logger.Debug("Looking for seasons to ignore");
                        foreach (var season in updatedSeries.Seasons)
                        {
                            if (season.SeasonNumber != updatedSeries.Seasons.Max(s => s.SeasonNumber) && _mediaFileProvider.GetSeasonFiles(season.SeasonId).Count() == 0)
                            {
                                Logger.Info("Season {0} of {1} doesn't have any files on disk. season will not be monitored.", season.SeasonNumber, updatedSeries.Title);
                                season.Monitored = false;
                                _seasonProvider.SaveSeason(season);
                            }
                        }
                   }

                }
                catch (Exception e)
                {
                    Logger.ErrorException(e.Message, e);
                }
            }

            //Keep scanning until there no more shows left.
            ScanSeries(notification);
        }

    }
}