﻿using DertInfo.WEB.Core;
using DertInfo.WEB.Core.Factory;
using DertInfo.WEB.Core.Objects;
using DertInfo.WEB.Models;
using DertInfo.WEB.ViewModels.Base;
using DertInfo.WEB.ViewModels.CompetitionVMs;
using DertInfo.WEB.ViewModels.DanceVMs;
using DertInfo.WEB.ViewModels.EventVMs;
using DertInfo.WEB.ViewModels.ScoreCategoryVMs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DertInfo.WEB.ViewModels.ResultVMs
{
    public class ResultsByCompetitionViewModel : DertViewModelBase_Accessible
    {

        public ResultsByCompetitionViewModel(Competition myCompetition)
            : base(myCompetition.AccessToken)
        {
            
            this.ScoreCatagoryVMs = new List<ScoreCategoryViewModel>();

            this.ToView(myCompetition);
        }

        public int DanceCount { get; set; }
        public CompetitionViewModel CompetitionVM { get; set; }
        public List<ScoreCategoryViewModel> ScoreCatagoryVMs { get; set; }
        public Dance_Listing_Collated_OnCompetitionEntry_ViewModel Dance_Listing_Collated_OnCompetitionEntry_VM { get; set; }
        public Dance_Listing_Detailed_ViewModel Dance_Listing_Detailed_VM { get; set; }

        internal void ToView(Competition myCompetition)
        {
            this.CompetitionVM = new CompetitionViewModel(myCompetition);

            IDanceRepository danceRepo = new DanceRepository();
            IQueryable<Dance> dances = danceRepo.AllByCompetitionIdIncluding(myCompetition.Id, d => d.DanceScores);

            this.DanceCount = dances.Count();

            IScoreCategoryRepository scoreCategoryRepo = new ScoreCategoryRepository();
            IQueryable<ScoreCategory> scoreCategories = scoreCategoryRepo.AllByCompetitionId(myCompetition.Id);

            //Determine if there is an excluding character mark.
            IEventSettingRepository eventSettingRepository = new EventSettingRepository();
            EventSetting mainCompScoreCategory = eventSettingRepository.FindByKey(myCompetition.EventId, Enumerations.EventSetting.MAINCOMP_CHARACTERS_SCORECATEGORY_ID.ToString());
            int mainCompScoreCategoryId = int.Parse(mainCompScoreCategory.Value);
            bool hasOmissionColumn = scoreCategories.Any(sc => sc.Id == mainCompScoreCategoryId);

            FillDances(dances, scoreCategories, hasOmissionColumn);
            FillCollatedDances(dances, scoreCategories, hasOmissionColumn);

            
        }

        private void FillDances(IQueryable<Dance> dances, IQueryable<ScoreCategory> scoreCategories, bool hasOmissionColumn)
        {
            this.Dance_Listing_Detailed_VM = new DanceVMs.Dance_Listing_Detailed_ViewModel(dances, scoreCategories);
            this.Dance_Listing_Detailed_VM.ShowExcludingCharactersCol = hasOmissionColumn;
            
        }

        private void FillCollatedDances(IQueryable<Dance> dances, IQueryable<ScoreCategory> scoreCategories, bool hasOmissionColumn)
        {
            CompetitionEntry_CollateDances_Factory competitionEntry_CollateDances_Factory = new CompetitionEntry_CollateDances_Factory(this.CompetitionVM.CompetitionId, dances, scoreCategories);
            IEnumerable<CompetitionEntryObj_WithScores> collatedDances = competitionEntry_CollateDances_Factory.CollateDances();

            this.Dance_Listing_Collated_OnCompetitionEntry_VM = new Dance_Listing_Collated_OnCompetitionEntry_ViewModel(collatedDances, scoreCategories);
            this.Dance_Listing_Collated_OnCompetitionEntry_VM.ShowExcludingCharactersCol = hasOmissionColumn;
        }

        //private void FillDances(Competition myCompetition)
        //{
        //    IDanceRepository danceRepo = new DanceRepository();
        //    IEnumerable<Dance> dances = danceRepo.AllByCompetitionIdIncluding(myCompetition.Id, d=> d.DanceScores);

        //    foreach (Dance dance in dances)
        //    {
        //        DanceViewModel_WithScores danceVM_WithScores = new DanceViewModel_WithScores(dance);
        //        this.DanceVMs.Add(danceVM_WithScores);
        //    }
        //}

        //private void FillScoreCategories(Competition myCompetition)
        //{
        //    IScoreCategoryRepository scoreCategoryRepo = new ScoreCategoryRepository();
        //    IEnumerable<ScoreCategory> scoreCategories = scoreCategoryRepo.AllByCompetitionId(myCompetition.Id);

        //    foreach (ScoreCategory scoreCategory in scoreCategories)
        //    {
        //        ScoreCategoryViewModel scoreCategoryVM = new ScoreCategoryViewModel(scoreCategory);
        //        this.ScoreCatagoryVMs.Add(scoreCategoryVM);
        //    }
        //}




    }
}