using RimWorld;
using Verse;
using System.Linq; // For LINQ operations

namespace ArachnaeSwarm
{
    public class CompAbilityEffect_BindDrone : CompAbilityEffect
    {
        public override void Apply(LocalTargetInfo target, LocalTargetInfo dest)
        {
            base.Apply(target, dest);

            Pawn dronePawn = target.Pawn;
            Pawn masterPawn = parent.pawn; // The pawn casting the ability

            if (masterPawn != null && dronePawn != null)
            {
                Hediff_HiveMindMaster masterHediff = masterPawn.health.hediffSet.GetFirstHediffOfDef(HediffDef.Named("ARA_HiveMindMaster")) as Hediff_HiveMindMaster;
                if (masterHediff != null)
                {
                    if (masterHediff.TryBindDrone(dronePawn))
                    {
                        Messages.Message($"Successfully bound {dronePawn.LabelShort} to {masterPawn.LabelShort}'s hive mind.", MessageTypeDefOf.PositiveEvent, historical: false);
                    }
                    else
                    {
                        Messages.Message($"Failed to bind {dronePawn.LabelShort} to {masterPawn.LabelShort}'s hive mind. Check logs for details.", MessageTypeDefOf.NegativeEvent, historical: false);
                    }
                }
                else
                {
                    Log.Error($"[ArachnaeSwarm] Master {masterPawn.LabelShort} tried to bind a drone but does not have Hediff_HiveMindMaster.");
                }
            }
        }

        public override bool Valid(LocalTargetInfo target, bool throwMessages = false)
        {
            if (!base.Valid(target, throwMessages))
            {
                return false;
            }

            Pawn dronePawn = target.Pawn;
            Pawn masterPawn = parent.pawn;

            // Target must be a pawn
            if (dronePawn == null)
            {
                if (throwMessages)
                {
                    Messages.Message("MustTargetPawn".Translate(parent.def.label), MessageTypeDefOf.RejectInput, historical: false);
                }
                return false;
            }

            // Target must be on the same map as the caster
            if (dronePawn.Map != masterPawn.Map)
            {
                if (throwMessages)
                {
                    Messages.Message("CannotTargetDifferentMap".Translate(), MessageTypeDefOf.RejectInput, historical: false);
                }
                return false;
            }

            // Target must have ARA_HiveMindDrone hediff
            Hediff_HiveMindDrone droneHediff = dronePawn.health.hediffSet.GetFirstHediffOfDef(HediffDef.Named("ARA_HiveMindDrone")) as Hediff_HiveMindDrone;
            if (droneHediff == null)
            {
                if (throwMessages)
                {
                    Messages.Message($"Target {dronePawn.LabelShort} does not have the 'ARA_HiveMindDrone' hediff.", MessageTypeDefOf.RejectInput, historical: false);
                }
                return false;
            }

            // Target must not be already bound to another master
            if (droneHediff.target != null && droneHediff.target != masterPawn)
            {
                if (throwMessages)
                {
                    Messages.Message($"Target {dronePawn.LabelShort} is already bound to {droneHediff.target.LabelShort}.", MessageTypeDefOf.RejectInput, historical: false);
                }
                return false;
            }

            // Caster must have ARA_HiveMindMaster hediff
            Hediff_HiveMindMaster masterHediff = masterPawn.health.hediffSet.GetFirstHediffOfDef(HediffDef.Named("ARA_HiveMindMaster")) as Hediff_HiveMindMaster;
            if (masterHediff == null)
            {
                if (throwMessages)
                {
                    Messages.Message($"Caster {masterPawn.LabelShort} does not have the 'ARA_HiveMindMaster' hediff.", MessageTypeDefOf.RejectInput, historical: false);
                }
                return false;
            }

            // All checks passed
            return true;
        }

        public override bool CanApplyOn(LocalTargetInfo target, LocalTargetInfo dest)
        {
            return Valid(target);
        }
    }
}