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

namespace Rimworld_Animations
{
    public class AnimationStage_LoopRandomSelectChance : AnimationStage
    {
        public List<AnimationLoopOption> loopOptions;

        public override List<AnimationDef> GetAnimations(int actorNumber, int seed)
        {
            int numberOfActors = loopOptions[0].animationDefs.Count;
            List<AnimationDef> animations = new List<AnimationDef>();

            for (int i = 0; i < loops; i++)
            {
                AnimationLoopOption option = getAnimationLoopOptionByWeight(seed + i);
                animations.Add(option.animationDefs[actorNumber]);
            }

            return animations;
        }



        //select random element from loop options by weight; also calculate the longest anim length
        private AnimationLoopOption getAnimationLoopOptionByWeight(int seed)
        {
            int totalWeight = loopOptions.Sum(x => x.probability);
            int randomNumber = Rand.RangeInclusiveSeeded(0, loopOptions.Count - 1, seed);

            int cumulativeWeight = 0;
            for (int i = 0; i < loopOptions.Count; i++) {

                
                cumulativeWeight += loopOptions[i].probability;

                //random number is same for all pawns because they all have the same seed
                
                if (randomNumber <= cumulativeWeight)
                {
                    return loopOptions[i];
                }
            }

            //default
            return loopOptions.Last();
        }
    }

    public class AnimationLoopOption
    {
        public int probability;
        public List<AnimationDef> animationDefs;
    }
}
