using DertInfo.WEB.BLL;
using DertInfo.WEB.Models;
using DertInfo.WEB.ViewModels.CompetitionVMs;
using DertInfo.WEB.ViewModels.EventVMs;
using DertInfo.WEB.ViewModels.ResultVMs;
using DertInfo.WEB.ViewModels.TeamVMs;
using DertInfo.WEB.ViewModels.VenueVMs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DertInfo.WEB.ViewModels.ContentVMs
{
    public class Event_Detail_ContentViewModel
    {

        public Event_Detail_ContentViewModel(Event myEvent)
        {
            this.TeamVMs = new List<TeamViewModel>();
            this.VenueVMs = new List<VenueViewModel>();
            this.ToView(myEvent);

        }

        public EventViewModel EventVM { get; set; }
        public List<TeamViewModel> TeamVMs { get; set; }
        public List<VenueViewModel> VenueVMs { get; set; }

        public int MainCompetitionId { get; set; }
        public int ScoreCategoryBuzzId { get; set; }
        public int ScoreCategoryMusicId { get; set; }
        public int ScoreCategoryCharactersId { get; set; }
        

        internal void ToView(Event myEvent)
        {
            FillBasic(myEvent);
            FillTeams(myEvent);
            //FillVenues(myEvent);
        }

        private void FillBasic(Event myEvent)
        {
            this.EventVM = new EventViewModel(myEvent);

            

             
        }

        private void FillTeams(Event myEvent)
        {
            IEnumerable<Team> teams = myEvent.Registrations.Where(r => r.FlowState == Core.Enumerations.RegistrationState.Confirmed || r.FlowState == Core.Enumerations.RegistrationState.Closed).SelectMany(r => r.AttendingTeams.Select(at => at.Team));

            foreach (Team team in teams.OrderBy(t => t.TeamName))
            {
                TeamViewModel teamVM = new TeamViewModel(team);
                this.TeamVMs.Add(teamVM);
            }
        }

        //private void FillVenues(Event myEvent)
        //{
        //    IEnumerable<Venue> venues = myEvent.Venues;

        //    foreach (Venue venue in venues)
        //    {
        //        VenueViewModel venueVM = new VenueViewModel(venue);
        //        this.VenueVMs.Add(venueVM);
        //    }
        //}

        #region Publlic Methods

        public bool AreResultsPublished()
        {
            IEventSettingService eventSettingsServ = new EventSettingService(this.EventVM.EventId, new EventSettingRepository());

            bool resultsPublished = bool.Parse(eventSettingsServ.GetStringSetting(Core.Enumerations.EventSetting.RESULTS_PUBLISHED));

            return resultsPublished;
        }

        #endregion

        //public IEnumerable<CompetitionEntryResultsStub> GetCompetitionResultStubs(int competitionId, bool excludeCharacters)
        //{
            
        //    List<int> categoryOmitList = new List<int>();
        //    categoryOmitList.Add(this.ScoreCategoryCharactersId);

        //    CompetitionRepository competitionRepo = new CompetitionRepository();
        //    Competition competition = competitionRepo.Find(competitionId);
        //    ResultsByCompetitionViewModel resultsByCompetitionVM = new ResultsByCompetitionViewModel(competition);

        //    List<DertInfo.WEB.ViewModels.CompetitionEntryVMs.CompetitionEntryViewModel_WithCollatedDanceScores> scores = resultsByCompetitionVM.Dance_Listing_Collated_OnCompetitionEntry_VM.CompetitionEntryVMs;

        //    List<CompetitionEntryResultsStub> stubs = new List<CompetitionEntryResultsStub>();
        //    foreach (var collatedScore in scores)
        //    {
        //        decimal pointsTotal = 0;
        //        if (excludeCharacters)
        //        {
        //            pointsTotal = collatedScore.GetScoresTotalOmitting(categoryOmitList);
        //        }
        //        else
        //        {
        //            pointsTotal = collatedScore.GetAllScoresTotal();
        //        }

        //        stubs.Add(new CompetitionEntryResultsStub()
        //        {
        //            TeamName = collatedScore.TeamName,
        //            PointsTotal = pointsTotal,
        //            AttributeTags = collatedScore.GetEntryAttributeTags()
        //        });
        //    }

        //    return stubs.OrderByDescending(s => s.PointsTotal);
        //}

        //public IEnumerable<CompetitionEntryResultsStub> GetScoreCategoryResultStubs(int competitionId, int scoreCategoryId)
        //{
        //    CompetitionRepository competitionRepo = new CompetitionRepository();
        //    Competition competition = competitionRepo.Find(competitionId);
        //    ResultsByCompetitionViewModel resultsByCompetitionVM = new ResultsByCompetitionViewModel(competition);

        //    List<int> categoryIncludeList = new List<int>();
        //    categoryIncludeList.Add(scoreCategoryId);

        //    List<DertInfo.WEB.ViewModels.CompetitionEntryVMs.CompetitionEntryViewModel_WithCollatedDanceScores> scores = resultsByCompetitionVM.Dance_Listing_Collated_OnCompetitionEntry_VM.CompetitionEntryVMs;

        //    List<CompetitionEntryResultsStub> stubs = new List<CompetitionEntryResultsStub>();
        //    foreach (var collatedScore in scores)
        //    {
        //        decimal pointsTotal = 0;
        //        pointsTotal = collatedScore.GetScoresTotalIncluding(categoryIncludeList);
                

        //        stubs.Add(new CompetitionEntryResultsStub()
        //        {
        //            TeamName = collatedScore.TeamName,
        //            PointsTotal = pointsTotal,
        //            AttributeTags = collatedScore.GetEntryAttributeTags()
        //        });
        //    }

        //    return stubs.OrderByDescending(s => s.PointsTotal);
        //}
        
        public IEnumerable<CompetitionVMs.CompetitionViewModel> ListCompetitions()
        {
            CompetitionRepository competitionRepo = new CompetitionRepository();
            IQueryable<Competition> competitions = competitionRepo.AllByEventId(this.EventVM.EventId);

            List<CompetitionViewModel> competitionVMs = new List<CompetitionViewModel>();
            foreach (var competition in competitions.OrderByDescending(c => c.CompetitionEntries.Count())) //Order to Support to help view render better.
            {
                CompetitionViewModel competitionVM = new CompetitionViewModel(competition);
                competitionVMs.Add(competitionVM);

            }

            return competitionVMs;
        }
    }

    public class CompetitionEntryResultsStub
    {
        public string TeamName { get; set; }
        public decimal PointsTotal { get; set; }
        public List<string> AttributeTags { get; set; }
    }
}