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

namespace SCE
{
    public static class NudityUtility
    {
        public static bool HasCoveredPart(this Pawn pawn, params BodyPartGroupDef[] groups)
        {
            // Would rather have what could be the larger list on the outer loop, but it
            // may turn out to be a better idea to just drop this method at some point.
            foreach (var apparel in pawn.apparel.WornApparel)
            {
                if (!apparel.def.apparel.countsAsClothingForNudity)
                {
                    continue;
                }
                foreach (var defGroup in apparel.def.apparel.bodyPartGroups)
                {
                    if (groups.Contains(defGroup))
                        return true;
                }
            }
            return false;
        }

        public static bool IdeoForbidsWearing(this Pawn pawn, ThingDef thing)
        {
            if (pawn.Ideo == null || !thing.IsApparel || !thing.apparel.countsAsClothingForNudity)
            {
                return false;
            }

            var apparel = thing.apparel;
            var ideo = pawn.Ideo;
            var femaleNudityish = SCEPreceptDefOf.Nudity_Female_CoveringGroinOrChestDisapproved;

            // Ideally this would check whether the apparel covers genitals specifically but RJW puts those in the wrong BodyPartGroup.
            if (apparel.bodyPartGroups.Contains(BodyPartGroupDefOf.Legs))
            {
                if (pawn.gender == Gender.Female && ideo.HasPrecept(SCEPreceptDefOf.Nudity_Female_CoveringGroinOrChestDisapproved))
                {
                    return true;
                }
                else if (pawn.gender == Gender.Male && ideo.HasPrecept(SCEPreceptDefOf.Nudity_Male_CoveringGroinDisapproved))
                {
                    return true;
                }
            }
            else
            {
                if ((pawn.gender == Gender.Male && pawn.Ideo.HasPrecept(SCEPreceptDefOf.Nudity_Male_CoveringAnythingButGroinDisapproved))
                    || (pawn.gender == Gender.Female && pawn.Ideo.HasPrecept(SCEPreceptDefOf.Nudity_Female_CoveringAnythingButGroinDisapproved)))
                {
                    return true;
                }
            }

            if (apparel.CoversBodyPart(Genital_Helper.get_breastsBPR(pawn)) && pawn.gender == Gender.Female 
                && ideo.HasPrecept(SCEPreceptDefOf.Nudity_Female_CoveringGroinOrChestDisapproved))
            {
                return true;
            }

            return false;
        }
    }
}