using RimWorld;
using System.Collections.Generic;
using System.Linq;
using Verse;
using AbilityUser;
using AlienRace;
using UnityEngine;


using RimWorld;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using UnityEngine;

using Verse;
using Verse.Sound;

namespace LTF_Crystal_Lockdowner
{
    [StaticConstructorOnStartup]
    public static class TexButton
    {
        public static readonly Texture2D FireAbility = ContentFinder<Texture2D>.Get("UI/Lockdown", true);
    }

    // reference: https://github.com/roxxploxx/RimWorldModGuide/wiki/SHORTUTORIAL%3A-JecsTools.CompAbilityUser

    public class CompLockdowner : GenericCompAbilityUser //GenericCompAbilityUser
    {
        public bool? Lockdowner;

        // Provides ability without affecting save.a
        public override void CompTick()
        {
            //Log.Warning("CompTick");
            if (AbilityUser?.Spawned == true)
            {
                if (Lockdowner != null)
                {
                    if (Lockdowner == true)
                    {
                        base.CompTick();
                    }
                }
                else
                {
                    Lockdowner = TryTransformPawn();
                    Initialize();
                }
            }

        }

        public override void PostInitialize()
        {
            base.PostInitialize();
            //if (Lockdowner == true) this.AddPawnAbility(LockdownerDefOf.Lockdowner);
            if (Lockdowner == true) this.AddPawnAbility(LockdownerDefOf.LTF_Crystal_Lockdowner);
        }

        private BodyPartRecord GetHeart(Pawn pawn)
        {
            BodyPartRecord bodyPart = null;
            pawn.RaceProps.body.GetPartsWithTag("BloodPumpingSource").TryRandomElement(out bodyPart);
            if (bodyPart == null)
            {
                Log.Warning("null heart ?");
            }

            return bodyPart;
        }


        public bool IsLockdowner
        {
            get
            {
                bool val = false;
                if (this.AbilityUser == null)
                    return false;

                // race
                val = IsCrystal(AbilityUser);
                if (!val)
                {
                    //Log.Warning(AbilityUser.LabelShort + " is not Crystal");
                    return false;
                }

                // heart
                BodyPartRecord heart = GetHeart(AbilityUser);
                if (heart == null)
                {
                    //Log.Warning(AbilityUser.LabelShort + " has no heart");
                    return false;
                }

                // crysta heart
                if (heart.def.defName != "CrystalHeart")
                {
                   // Log.Warning(AbilityUser.LabelShort + " has a non legit " + heart.def.defName);
                    return false;
                }


                // crystal clear health
                List<Hediff> myHediffs = AbilityUser.health.hediffSet.hediffs;
                for (int i = 0; i < myHediffs.Count; i++)
                {
                    BodyPartRecord myBP = myHediffs[i].Part;

                    if (myBP != heart) {
                        continue;
                    }
                    else
                    {
                        //Log.Warning(AbilityUser.LabelShort + " is special");
                        val = false;
                        break;
                    }

                    AddedBodyPartProps addedPartProps = myHediffs[i].def.addedPartProps;
                    if (addedPartProps != null && addedPartProps.isBionic)
                    {
                        val = false;
                        break;
                    }
                }

                return val;
            }
        }


        private bool IsCrystal(Pawn pawn)
        {
            return (pawn?.def.defName == "Alien_Crystal");
        }

        public override bool TryTransformPawn()
        {
            //Log.Warning("TryTransformPawn");
            //if (!LockdownerMod.settings.enableLockdownerAbility) return false;
            return IsLockdowner;
        }

        public override IEnumerable<Gizmo> CompGetGizmosExtra()
        {
            if (this.AbilityUser.Drafted)
            {
                IEnumerator<Gizmo> enumerator = base.CompGetGizmosExtra().GetEnumerator();
                while (enumerator.MoveNext())
                {
                    Gizmo current = enumerator.Current;
                    yield return current;
                }
                for (int i = 0; i < this.AbilityData.AllPowers.Count; i++)
                    yield return this.AbilityData.AllPowers[i].GetGizmo();
            }
        }

    }

    [DefOf]
    public static class LockdownerDefOf
    {
        //public static AbilityDef Lockdowner;
        public static AbilityDef LTF_Crystal_Lockdowner;
    }

    /*
    public class LockdownerSpark : Projectile_AbilityBase
    {
        protected override void Impact(Thing hitThing)
        {
            base.Impact(hitThing);
            //LockdownerUtility.CreateLockBlocks(hitThing, DestinationCell);
            LockdownerUtility.CreateLockBlocks(hitThing);
        }
    }
    */

}