using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Valg.EVA.Skanning.Entity.ReadModel;
using Valg.EVA.Skanning.ServicesModel.ElectionConfig.DataProvider;
using Valg.EVA.Skanning.ServicesModel.Internal.BallotNumber;
using Valg.EVA.Skanning.ServicesModel.Internal.Interpreter.Modifications;

namespace Valg.EVA.Skanning.ServicesModel.Internal.Interpreter
{
    public class BallotHeaderInterpreter
    {
        private ICandidateListDataProvider _candidateListProvider;
        private IElectionConfigDataProvider _electionConfigDataProvider;
        public BallotHeaderInterpreter(ICandidateListDataProvider candidateListProvider, IElectionConfigDataProvider electionConfigDataProvider)
        {
            _candidateListProvider = candidateListProvider;
            _electionConfigDataProvider = electionConfigDataProvider;
        }

        public void CheckIntegrity(BallotPaperInBoxCounting ballotPaper, IReadOnlyCollection<BallotPaperFieldValue> ballotPaperFieldValues,
            InterpreterModificationBuffer interpreterModificationBuffer, string boxBarcodeLong)
        {
            var ballotNumberField = ballotPaperFieldValues.GetBallotNumberField();
            if (!ballotNumberField.NeedsVerification)
            {
                var expectedElectionNameText = GetExpectedElectionNameOnBallotPaper(boxBarcodeLong);
                var expectedAffiliationNameText = GetExpectedAffiliationNameOnBallotPaper(boxBarcodeLong, ballotPaperFieldValues.GetBallotNumberField().Value.AffiliationShortCode());

                foreach (var affiliationNameField in ballotPaperFieldValues.GetAffiliationNameFields())
                {
                    if (!IsWithinFaultTolerance(affiliationNameField.Value, expectedAffiliationNameText))
                    {
                        interpreterModificationBuffer.MarkFieldToNeedVerification(affiliationNameField);
                    }
                }
            }
        }

        private static bool IsWithinFaultTolerance(string text, string expectedText)
        {
            var words = expectedText.Split(' ').Length;
            var difference = LevenshteinDistance(text, expectedText);
            return difference <= words;
        }

        private string GetExpectedElectionNameOnBallotPaper(string boxBarcodeLong)
        {
            var election = _electionConfigDataProvider.GetElection(boxBarcodeLong.GetElectionEventIdFromBarcodeLong(), boxBarcodeLong.GetElectionIdFromBarcodeLong());
            var contest = _electionConfigDataProvider.GetContest(boxBarcodeLong.GetElectionEventIdFromBarcodeLong(), boxBarcodeLong.GetContestIdFromBarcodeLong());
            if (election == null || contest == null)
            {
                return string.Empty;
            }
            return $"{election.Name} i {contest.Name}";
        }
        private static int LevenshteinDistance(string s, string t)
            {
                int n = s.Length;
                int m = t.Length;
                int[,] d = new int[n + 1, m + 1];

                
                if (n == 0)
                {
                    return m;
                }

                if (m == 0)
                {
                    return n;
                }

                
                for (int i = 0; i <= n; d[i, 0] = i++)
                {
                }

                for (int j = 0; j <= m; d[0, j] = j++)
                {
                }

                
                for (int i = 1; i <= n; i++)
                {
                    
                    for (int j = 1; j <= m; j++)
                    {
                        
                        int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;

                        
                        d[i, j] = Math.Min(
                            Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                            d[i - 1, j - 1] + cost);
                    }
                }
                
                return d[n, m];
            }

        private string GetExpectedAffiliationNameOnBallotPaper(string boxBarcodeLong, string affiliationShortCode)
        {
            return _candidateListProvider.GetAffiliation(boxBarcodeLong.GetElectionEventIdFromBarcodeLong(), boxBarcodeLong.GetContestIdFromBarcodeLong(), affiliationShortCode)?.RegisteredName;
        }
    }
}

