﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RimWorld;
using rjw.Modules.Interactions.Helpers;
using rjw.Modules.Interactions.Objects;
using UnityEngine;
using Verse;
using Verse.AI;
using rjw.Modules.Interactions.Enums;

namespace Rimworld_Animations {
    public static class AnimationUtility {


        public static void StartAnimation(List<Pawn> participants)
        {
            participants[0].Drawer.renderer.SetAnimation(AnimationDefOf.TestAnimation1);
            participants[1].Drawer.renderer.SetAnimation(AnimationDefOf.TestAnimation2);
        }

        //startgroupanimator with anchor
        //don't anchor to self if anchor is self
        public static void StartGroupAnimation(List<Pawn> participants, GroupAnimationDef groupAnimationDef, int reorder, Thing anchor)
        {

            int seed = GenTicks.TicksGame;

            for (int i = 0; i < participants.Count; i++)
            {
                
                groupAnimationDef.GetOffset(i, participants[i], out Vector3? position, out int? rotation, reorder);

                if (anchor is Pawn pawn && pawn == participants[i])
                {

                    List<AnimationDef> allAnimationsForPawn = groupAnimationDef.GetAllAnimationsForActor(i, seed, reorder);
                    participants[i].TryGetComp<CompExtendedAnimator>().PlayGroupAnimation(allAnimationsForPawn, position, rotation);
                }
                else
                {
                    //each participant gets their own unique extendedanimatoranchor, important for scribe_deep saving
                    List<AnimationDef> allAnimationsForPawn = groupAnimationDef.GetAllAnimationsForActor(i, seed, reorder);
                    BaseExtendedAnimatorAnchor animatorAnchor = new ExtendedAnimatorAnchor_Thing(anchor);

                    if (RJWAnimationSettings.debugMode)
                    {
                        Log.Message("Now playing animation: " + groupAnimationDef.defName + " Actor Shift: " + reorder);
                    }

                    participants[i].TryGetComp<CompExtendedAnimator>().PlayGroupAnimation(allAnimationsForPawn, position, rotation, animatorAnchor);
                }
            }
        }
        
        //startgroupanimation without anchor; just play where standing
        public static void StartGroupAnimation(List<Pawn> participants, GroupAnimationDef groupAnimationDef, int reorder)
        {
            int seed = GenTicks.TicksGame;

            for (int i = 0; i < participants.Count; i++)
            {
                List<AnimationDef> allAnimationsForPawn = groupAnimationDef.GetAllAnimationsForActor(i, seed, reorder);
                groupAnimationDef.GetOffset(i, participants[i], out Vector3? position, out int? rotation, reorder);
                participants[i].TryGetComp<CompExtendedAnimator>().PlayGroupAnimation(allAnimationsForPawn, position, rotation);
            }
        }


        public static void StopGroupAnimation(List<Pawn> participants)
        {
            foreach(Pawn pawn in participants)
            {
                pawn.TryGetComp<CompExtendedAnimator>()?.StopAnimating();
            }
        }
        
        public static void StopGroupAnimation(Pawn participant)
        {
            participant.TryGetComp<CompExtendedAnimator>()?.StopAnimating();
        }

        public static GroupAnimationDef FindGroupAnimation(List<Pawn> participants, out int reorder)
        {
            // go through each context in each GroupAnimationDef
            // if you find one where it returns canAnimationBeUsed (and reorders),
            // return that animation


            //find all, reorder randomly, then find max priority context
            DefDatabase<GroupAnimationDef>.AllDefsListForReading
                .FindAll((GroupAnimationDef x) => x.canAnimationBeUsed(participants))
                .OrderBy(_ => Rand.Int)
                .TryMaxBy((GroupAnimationDef x) => x.Priority(participants), out GroupAnimationDef result);

            
            reorder = result != null ? result.Reorder(participants) : 0;
            return result;

        }

        public static int GetAnimationLength(Pawn pawn)
        {
            return pawn.TryGetComp<CompExtendedAnimator>().AnimationLength;
        }

    }
}
