using System.Linq;
using Intrepid.Gameplay.Codex.Knowledge;

namespace Intrepid.Gameplay.Codex.Perception
{
    /// <summary>
    /// First deterministic placeholder for the future vector/prototype classifier.
    /// Today it only separates known concepts from semantic residue.
    /// </summary>
    public class CodexSemanticClassifier
    {
        private readonly CodexRuntime knowledge;

        public CodexSemanticClassifier(CodexRuntime knowledge)
        {
            this.knowledge = knowledge;
        }

        public CodexInterpretationResult Classify(CodexInterpretationRequest request)
        {
            var manifest = request.Readable.Manifest.Where(e => e.Concept != null).ToList();
            var recognized = manifest.Where(e => knowledge.Knows(e.Concept)).Select(e => e.Concept).Distinct().ToList();
            var residue = manifest.Where(e => !knowledge.Knows(e.Concept)).Select(e => e.Concept).Distinct().ToList();
            var confidence = manifest.Count == 0 ? 0f : (float)recognized.Count / manifest.Count;

            return new CodexInterpretationResult
            {
                Readable = request.Readable,
                ReadingState = request.ReadingState,
                RecognizedConcepts = recognized,
                ResidueConcepts = residue,
                Confidence = confidence
            };
        }
    }
}
