﻿using DertInfo.WEB.Core;
using DertInfo.WEB.Models;
using DertInfo.WEB.ViewModels.Base;
using DertInfo.WEB.ViewModels.TeamVMs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DertInfo.WEB.ViewModels.CompetitionVMs
{
    public class CompetitionDetailsViewModel : DertViewModelBase_Accessible
    {
        #region Constructors

        //Constructors
        public CompetitionDetailsViewModel(Models.Competition competition)
            : base(competition.AccessToken)
        {
            this.ToView(competition);
        }

        //private void VariablesConstructor()
        //{
            

        //    // todo - clean up - moved from system setting to event settings. 
        //    //this.PaymentDueDate = DertInfo.WEB.BLL.BLLSystemSettings.GetDateSetting("PAYMENTDUEDATE");
        //    //this.RegistrationOpenDate = DertInfo.WEB.BLL.BLLSystemSettings.GetDateSetting("REGISTRATIONOPENDATE");
        //    //this.RegistrationCloseDate = DertInfo.WEB.BLL.BLLSystemSettings.GetDateSetting("REGISTRATIONCLOSEDATE");
        //    //this.ResultsPublic = DertInfo.WEB.BLL.BLLSystemSettings.GetBooleanSetting("RESULTSPUBLISHED");
        //}

        #endregion

        #region Properties

        //Static Properties
        //public DateTime PaymentDueDate { get; set; }
        //public DateTime RegistrationOpenDate { get; set; }
        //public DateTime RegistrationCloseDate { get; set; }
        //public bool ResultsPublic { get; set; }

        //Variable Properties
        public CompetitionViewModel CompetitionVM { get; set; }
        public CompetitionEntryAttributeVMs.CompetitionEntryAttribute_Listing_ByCompetition_ViewModel CompetitionEntryAttribute_Listing_ByCompetition_VM { get; set; }
        public CompetitionEntryVMs.CompetitionEntry_Listing_ByCompetition_ViewModel CompetitionEntry_Listing_ByCompetition_VM { get; set; }
        
        public VenueVMs.Venue_Listing_ByCompetition_ViewModel Venue_Listing_ByCompetition_VM { get; set; }
        public ScoreSetVMs.ScoreSet_Listing_ByCompetition_ViewModel ScoreSet_Listing_ByCompetition_VM { get; set; }
        public DanceVMs.DanceSummaryViewModel DanceSummaryVM { get; set; }

        //public List<RegistrationVMs.RegistrationViewModel> PreviousRegistrationVMs { get; set; }
        //public List<RegistrationVMs.RegistrationViewModel> CurrentRegistrationVMs { get; set; }
        //public List<Models.CompetitionImage> CompetitionImages { get; set; }
        //public ImageVMs.ImageAjaxSingleSelectionViewModel ImageAjaxSingleSelectionVM { get; set; }
        //public TeamVMs.TeamGalleryViewModel TeamGalleryVM { get; set; }
        //public EventVMs.Event_SelectionListing_ByCompetition_ViewModel Event_SelectionListing_ByCompetition_VM { get; set; }
        //public CompetitionMemberVMs.CompetitionMember_Listing_ByCompetition_ViewModel CompetitionMember_Listing_ByCompetition_VM { get; set; }
        //public List<InvoiceVMs.InvoiceViewModel> InvoiceVMs { get; set; }

        #endregion

        #region ToView / ToModel methods

        private void ToView(Models.Competition competition)
        {
            FillBasic(competition);
            FillCompetitionEntryAttributes(competition);
            FillCompetitionEntries(competition);
            
            FillVenues(competition);
            FillScoreSets(competition);
            FillDanceSummary(competition);

            //FillCompetitionImages(competition.Id);
            //FillCompetitionRegistrations(competition.Id);
            //FillTeams(competition.Id);
            //FillEventSelection(competition.Id);
            //FillCompetitionMembers(competition.Id);
            //FillInvoice(competition.Id);
        }

       
        

        private void FillBasic(Models.Competition competition)
        {
            CompetitionViewModel competitionVM = new CompetitionViewModel(competition);
            this.CompetitionVM = competitionVM;
        }

        private void FillCompetitionEntryAttributes(Models.Competition competition)
        {
            ICompetitionEntryAttributeRepository competitionEntryAttributeRepo = new CompetitionEntryAttributeRepository();
            List<Models.CompetitionEntryAttribute> competitionMembers = competitionEntryAttributeRepo.AllByCompetitionId(competition.Id).ToList();

            this.CompetitionEntryAttribute_Listing_ByCompetition_VM = new CompetitionEntryAttributeVMs.CompetitionEntryAttribute_Listing_ByCompetition_ViewModel(competition.Id, competitionMembers);
        }

        private void FillCompetitionEntries(Competition competition)
        {
            ICompetitionEntryRepository competitionEntryRepo = new CompetitionEntryRepository();
            List<Models.CompetitionEntry> competitionEntries = competitionEntryRepo.AllByCompetitionId(competition.Id).Where(ce => ce.AttendingTeam.Registration.FlowState == Enumerations.RegistrationState.Confirmed || ce.AttendingTeam.Registration.FlowState == Enumerations.RegistrationState.Closed).ToList();

            this.CompetitionEntry_Listing_ByCompetition_VM = new CompetitionEntryVMs.CompetitionEntry_Listing_ByCompetition_ViewModel(competition.Id, competitionEntries);
        }


        

        private void FillVenues(Competition competition)
        {
            IVenueRepository venueRepo = new VenueRepository();
            List<Models.Venue> venues = venueRepo.AllByCompetitionId(competition.Id).ToList();

            this.Venue_Listing_ByCompetition_VM = new VenueVMs.Venue_Listing_ByCompetition_ViewModel(competition, venues);
        }

        private void FillScoreSets(Competition competition)
        {
            IScoreSetRepository scoreSetRepo = new ScoreSetRepository();
            List<Models.ScoreSet> scoreSets = scoreSetRepo.AllByCompetitionId(competition.Id).ToList();

            this.ScoreSet_Listing_ByCompetition_VM = new ScoreSetVMs.ScoreSet_Listing_ByCompetition_ViewModel(competition.Id, scoreSets);
        }

        private void FillDanceSummary(Competition competition)
        {
            this.DanceSummaryVM = new DanceVMs.DanceSummaryViewModel(competition);
        }


        //private void FillCompetitionImages(int competitionId)
        //{
        //    Models.IImageRepository imageRepo = new Models.ImageRepository();
        //    Models.ICompetitionImageRepository competitionImageRepo = new Models.CompetitionImageRepository();

        //    int selectedId = competitionImageRepo.GetPrimaryImageId(competitionId);
        //    //List<Models.Image> selectedTeamImages = imageRepo.AllByTeamId(teamId).ToList();
        //    List<Models.Image> selectionSetImages = imageRepo.AllByCompetitionId(competitionId).ToList();

        //    this.ImageAjaxSingleSelectionVM = new ImageVMs.ImageAjaxSingleSelectionViewModel(competitionId, "JsonSetCompetitionPrimary", "Image", selectionSetImages, selectedId);

        //    //ICompetitionImageRepository competitionImageRepo = new CompetitionImageRepository();
        //    //this.CompetitionImages = competitionImageRepo.AllByCompetitionIdIncluding(competitionId,gi => gi.Image).ToList();

        //    ////Models.CompetitionImage competitionImage = new Models.CompetitionImage();
        //    ////competitionImage.ListByCompetition(competitionId);

        //    //this.CompetitionImageGalleryVM = new ImageVMs.CompetitionImageGalleryViewModel(competitionId, this.CompetitionImages);
        //}

        //private void FillCompetitionRegistrations(int competitionId)
        //{
        //    IRegistrationRepository registrationRepo = new RegistrationRepository();
        //    IQueryable<Models.Registration> registrations = registrationRepo.AllByCompetitionIdIncluding(competitionId,g => g.Event,g=>g.Competition);
           
        //   foreach(Models.Registration competitionRegistration in registrations.Where(r=>r.FlowState != Core.Enumerations.RegistrationState.Closed))
        //   {
        //       RegistrationVMs.RegistrationViewModel competitionRegistrationVM = new RegistrationVMs.RegistrationViewModel(competitionRegistration);
        //       this.CurrentRegistrationVMs.Add(competitionRegistrationVM);
        //   }

        //   foreach (Models.Registration competitionRegistration in registrations.Where(r => r.FlowState == Core.Enumerations.RegistrationState.Closed))
        //   {
        //       RegistrationVMs.RegistrationViewModel competitionRegistrationVM = new RegistrationVMs.RegistrationViewModel(competitionRegistration);
        //       this.PreviousRegistrationVMs.Add(competitionRegistrationVM);
        //   }

        //}

        //private void FillTeams(int competitionId)
        //{
        //    ITeamRepository teamRepo = new TeamRepository();

        //    List<Models.Team> teams = teamRepo.AllByCompetitionId(competitionId).ToList();

        //    this.TeamGalleryVM = new TeamGalleryViewModel(competitionId, teams);
        //}

        //private void FillEventSelection(int competitionId)
        //{
        //    IEventRepository eventRepo = new EventRepository();

        //    List<Models.Event> openEvents = eventRepo.AllByRegistrationOpen(DateTime.Now).ToList();

        //    //todo - push down into business logic
        //    List<Models.Event> openForCompetitionEvents = new List<Event>();

        //    foreach (Models.Event myEvent in openEvents)
        //    {
        //        if (!this.CurrentRegistrationVMs.Where(rvm => rvm.EventId == myEvent.Id).Any())
        //        {
        //            //Business Logic - If the competition is not already registered for the event
        //            openForCompetitionEvents.Add(myEvent);
        //        }
        //    }

        //    this.Event_SelectionListing_ByCompetition_VM = new EventVMs.Event_SelectionListing_ByCompetition_ViewModel(competitionId, openForCompetitionEvents);
        //}

        //private void FillCompetitionMembers(int competitionId)
        //{
        //    ICompetitionMemberRepository competitionMemberRepo = new CompetitionMemberRepository();
        //    List<Models.CompetitionMember> competitionMembers = competitionMemberRepo.AllByCompetitionId(competitionId).ToList();

        //    this.CompetitionMember_Listing_ByCompetition_VM = new CompetitionMemberVMs.CompetitionMember_Listing_ByCompetition_ViewModel(competitionId, competitionMembers);
        //}

        //private void FillInvoice(int competitionId)
        //{
        //    IInvoiceRepository invoiceRepo = new InvoiceRepository();
        //    List<Models.Invoice> invoices = invoiceRepo.AllByCompetitionId(competitionId).ToList();

        //    foreach (var invoice in invoices)
        //    {
        //        InvoiceVMs.InvoiceViewModel invoiceVM = new InvoiceVMs.InvoiceViewModel(invoice);
        //        this.InvoiceVMs.Add(invoiceVM);
        //    }
        //}


        #endregion 
    }
}