﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace Rimworld_Animations
{
    public class GroupAnimationDef : Def
    {
        public int numActors;
        public List<AnimationStage> animationStages;
        public List<BaseGroupAnimationContext> contexts;
        public List<AnimationOffsetDef> offsetDefs;


        public bool canAnimationBeUsed(List<Pawn> actors)
        {

            if (!contexts.NullOrEmpty())
            {
                foreach (BaseGroupAnimationContext context in contexts)
                {
                    if (context.CanAnimationBeUsed(actors, numActors))
                    {
                        //find all where context matches actors
                        return true;

                    }
                }
            }
            

            return false;
        }

        public int Priority(List<Pawn> actors)
        {
            int priority = -999999999;

            foreach (BaseGroupAnimationContext context in contexts)
            {
                if (context.CanAnimationBeUsed(actors, numActors))
                {
                    if (context.AnimationPriority() > priority)
                    {
                        //get highest priority context for fitting animation
                        priority = context.AnimationPriority();

                    }

                }
            }

            return priority;

        }

        public int Reorder(List<Pawn> actors)
        {
            int priority = -999999999;
            int reorder = 0;

            foreach (BaseGroupAnimationContext context in contexts)
            {
                if (context.CanAnimationBeUsed(actors, numActors))
                {
                    if (context.AnimationPriority() > priority)
                    {
                        //get the reorder for highest priority context for fitting animation
                        priority = context.AnimationPriority();
                        reorder = context.AnimationReorder();

                    }

                }
            }

            return reorder;

        }

        public List<AnimationDef> GetAllAnimationsForActor(int actor, int seed, int reorder = 0)
        {
            List<AnimationDef> animations = new List<AnimationDef>();
            int actorNumber = (actor + reorder) % numActors;


            foreach (AnimationStage stage in animationStages)
            {
                //add all new animations to list of animations
                animations.AddRange(stage.GetAnimations(actorNumber, seed));
            }

            return animations;
        }

        public bool GetOffset(int actor, Pawn pawn, out Vector3? position, out int? rotation, int reorder = 0)
        {

            position = null;
            rotation = null;

            //element at or default to stop errors
            if (offsetDefs == null) return false;
            if ((actor + reorder) % numActors >= offsetDefs.Count) return false;
            if (offsetDefs[(actor + reorder) % numActors].FindOffset(pawn, out BaseAnimationOffset animationOffset))
            {
                position = animationOffset.getOffset(pawn);
                rotation = animationOffset.getRotation(pawn);
                return true;
            }

            return false;
        }
    }
}

1.5/Source/Animations/GroupAnimations/GroupAnimationDef.cs