using Valg.EVA.Skanning.Contracts.Security;
using Valg.EVA.Skanning.Identity;
using Valg.EVA.Skanning.JobManagement.Startup.Data;
using Valg.EVA.Skanning.ServicesModel.ServiceContracts.JobManage.Write;
using Valg.EVA.Skanning.UserInterfaceElements;
using Valg.EVA.Skanning.Services.Count;

namespace Valg.EVA.Skanning.JobManagement.Startup.Actions.Counting
{
    internal class StartCountingAction
    {
        private readonly IIdentityProvider identityProvider;
        private readonly ICountingService countingService;

        public StartCountingAction(ICountingService countingService, IIdentityProvider identityProvider)
        {
            this.identityProvider = identityProvider;
            this.countingService = countingService;
        }

        internal void StartFinalCounting(VotingCategoryType votingCategoryType, PollingDistrictModel pollingDistrict)
        {
            string electionEventId = identityProvider.ClaimValue(ClaimDefinitions.ElectionEventIdClaimType);
            var role = identityProvider.CurrentSelectedRole;

            countingService.StartFinalCounting(new StartCountingParameters(
                electionEventId,
                role.ContestId,
                pollingDistrict.Id,
                role.CountingEmlAreaContextId,
                role.ElectionIdentifier,
                role.ElectionGroup,
                votingCategoryType.Key(),
                identityProvider.GetCurrentUserName(),
                identityProvider.IsInEmergencyMode));
        }

        internal void StartPreliminaryCounting(VotingCategoryType votingCategoryType, PollingDistrictModel pollingDistrict)
        {
            var electionEventId = identityProvider.ClaimValue(ClaimDefinitions.ElectionEventIdClaimType);
            var role = identityProvider.CurrentSelectedRole;

            countingService.StartPreliminaryCounting(new StartCountingParameters(
                electionEventId,
                role.ContestId,
                pollingDistrict.Id,
                role.CountingEmlAreaContextId,
                role.ElectionIdentifier,
                role.ElectionGroup,
                votingCategoryType.Key(),
                identityProvider.GetCurrentUserName(),
                identityProvider.IsInEmergencyMode));
        }
    }
}
