using System.Linq;
using RimWorld;
using Verse;

namespace ArachnaeSwarm
{
    public class CompProperties_AbilityBodyPartCheck : CompProperties_AbilityEffect
    {
        public BodyPartDef requiredPart;
        public float minimumHealth = 0.8f;
        public string failMessage = "Missing or damaged body part.";

        public CompProperties_AbilityBodyPartCheck()
        {
            compClass = typeof(CompAbilityEffect_BodyPartCheck);
        }
    }

    public class CompAbilityEffect_BodyPartCheck : CompAbilityEffect
    {
        public new CompProperties_AbilityBodyPartCheck Props => (CompProperties_AbilityBodyPartCheck)props;

        public override bool GizmoDisabled(out string reason)
        {
            Pawn caster = parent.pawn;
            if (caster != null && caster.health != null && caster.health.hediffSet != null)
            {
                var part = caster.health.hediffSet.GetNotMissingParts()
                                  .FirstOrDefault(p => p.def == Props.requiredPart);

                if (part == null)
                {
                    reason = Props.failMessage;
                    return true;
                }

                float partHealth = caster.health.hediffSet.GetPartHealth(part) / part.def.GetMaxHealth(caster);
                if (partHealth < Props.minimumHealth)
                {
                    reason = Props.failMessage;
                    return true;
                }
            }
            
            reason = null;
            return false;
        }
    }
}