using System.Collections.Generic;
using System.Linq;
using Verse;
using RimWorld;

namespace SCE
{
    public static class IncestUtility
    {
        public static readonly List<PawnRelationDef> siblingRelations = new() { 
            PawnRelationDefOf.Sibling, PawnRelationDefOf.HalfSibling 
        };

        public static readonly List<PawnRelationDef> parentalRelations = new() { 
            PawnRelationDefOf.Parent, DefDatabase<PawnRelationDef>.GetNamed("RJW_Sire"), 
            DefDatabase<PawnRelationDef>.GetNamed("RJW_Pup")
        };

        public static readonly List<PawnRelationDef> parentChildRelations = new(parentalRelations) { PawnRelationDefOf.Child };

        public static readonly List<PawnRelationDef> lineageRelations = new(parentChildRelations) {
            PawnRelationDefOf.Grandparent, PawnRelationDefOf.Grandchild, PawnRelationDefOf.GreatGrandparent, PawnRelationDefOf.GreatGrandchild
        };

        public static bool IsChildOf(this Pawn pawn, Pawn otherPawn)
        {
            return pawn.relations.DirectRelationExists(PawnRelationDefOf.Child, otherPawn);
        }
        
        public static bool IsParentOrChildOf(this Pawn pawn, Pawn otherPawn)
        {
            return pawn.GetRelations(otherPawn).Any(rel => parentChildRelations.Contains(rel));
        }

        public static bool IsRecentAncestorOrDescendantOf(this Pawn pawn, Pawn otherPawn)
        {
            return pawn.GetRelations(otherPawn).Any(rel => lineageRelations.Contains(rel));
        }

		public static bool IsSiblingOf(this Pawn pawn, Pawn otherPawn)
		{
			return pawn.GetRelations(otherPawn).Any(rel => siblingRelations.Contains(rel));
		}

        public static PawnRelationDef GetMostImportantBloodRelation(this Pawn pawn, Pawn otherPawn)
        {
            float importance = 0;
            PawnRelationDef result = null;
            foreach (var relation in pawn.GetRelations(otherPawn).Where(rel => rel.familyByBloodRelation))
            {
                if (relation.importance > importance)
                {
                    importance = relation.importance;
                    result = relation;
                }
            }
            return result;
        }

    }
}