﻿using System;
using System.Collections.Generic;
using System.Linq;
using TelegramBot.Models;

namespace TelegramBot.Manager
{
    public class ScheduleManager
    {
        private TimeTable[] _timeTable;
        private IEnumerable<Schedule> _schedule;
        private DateTime _time = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, Configuration.TimeZone);
        //private DateTime _time = new DateTime(2017, 9, 9, 19, 0, 0); //for test

        public ScheduleManager()
        {
            var file = new FileData();
            _timeTable = file.GetTimeTable().ToArray();
            _schedule = file.GetSchedule();
        }

        public LessonDto GetCurrent()
        {
            var result = new LessonDto() { OutputMessage = "Зараз в тебе вільний час \U0001F6A9" };
            var schedule = _schedule.Where(x => x.Day == (int)_time.DayOfWeek).FirstOrDefault();

            if (schedule == null || _time.DayOfWeek == (int)Day.Sunday)
            {
                result.OutputMessage = "Пар не знайдено. Сьогодні ж неділя! \n\rВідпочивай \U0001F60F";
                return result;
            }
            else if (_time < _timeTable[schedule.Lessons.First().Position].Start)
            {
                result.OutputMessage = "Твої пари сьогоді розпочнуться о " +
                    _timeTable[schedule.Lessons.First().Position].Start.ToShortTimeString() + " \U0001F55C";
                return result;
            }
            else if (_time >= _timeTable[schedule.Lessons.Last().Position].End)
            {
                if ((int)_time.DayOfWeek == (int)Day.Saturday)
                    result.OutputMessage = "На сьогодні пари закінчились.\n\rПопереду вихідні \U0001F609";
                else
                    result.OutputMessage = "На сьогодні пари закінчились.\n\rПриходьте завтра \U0001F609";
                return result;
            }


            foreach (var lesson in schedule.Lessons)
            {
                if (_time >= _timeTable[lesson.Position].Start && _time < _timeTable[lesson.Position].End)
                {
                    result.Current = lesson;
                    return result;
                }
            }
            return result;
        }

        public LessonDto GetNext()
        {
            var result = new LessonDto();
            int nextSubject = -1;
            var schedule = _schedule.Where(x => x.Day == (int)_time.DayOfWeek).FirstOrDefault();

            if (schedule == null || _time.DayOfWeek == (int)Day.Sunday)
            {
                schedule = _schedule.Where(x => x.Day == (int)Day.Monday).FirstOrDefault();
                result.Current = schedule.Lessons.FirstOrDefault();
                result.OutputMessage = "В понеділок\n\r";
                return result;
            }

            if (_time >= _timeTable[schedule.Lessons.Last().Position].Start)
            {
                if ((int)_time.DayOfWeek == (int)Day.Saturday)
                {
                    schedule = _schedule.Where(x => x.Day == (int)Day.Monday).FirstOrDefault();
                    result.Current = schedule.Lessons.FirstOrDefault();
                    result.OutputMessage = "В понеділок\n\r";
                    return result;
                }
                else
                {
                    schedule = _schedule.Where(x => x.Day == (int)_time.DayOfWeek + 1).FirstOrDefault();
                    result.Current = schedule.Lessons.FirstOrDefault();
                    result.OutputMessage = "Завтра\n\r";
                    return result;
                }
            }

            int pos = -1;
            foreach (var day in schedule.Lessons)
            {
                pos = day.Position;
                if (_time < _timeTable[pos].Start)
                {
                    nextSubject = pos;
                    break;
                }
            }
            result.Current = schedule.Lessons.Where(x => x.Position == nextSubject).FirstOrDefault();
            result.OutputMessage = "";
            return result;
        }

        public IEnumerable<Schedule> GetWeek()
        {
            return _schedule;
        }
    }
}
