﻿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 AnimationWorker_KeyframesExtended : AnimationWorker_Keyframes
    {

        public AnimationWorker_KeyframesExtended(AnimationDef def, Pawn pawn, AnimationPart part, PawnRenderNode node) : base(def, pawn, part, node)
        {
        }

        public override bool Enabled()
        {
            return true;
        }

        public override Vector3 OffsetAtTick(int tick, PawnDrawParms parms)
        {
            //Todo: Use this for bodyoffsets
            //or maybe make a rendersubworker for it instead
            return base.OffsetAtTick(tick, parms);
        }

        public VoiceTagDef voiceAtTick(int tick)
        {

            Verse.Keyframe keyframe2 = this.part.keyframes[this.part.keyframes.Count - 1];
            foreach (Verse.Keyframe keyframe in this.part.keyframes)
            {
                if (tick == keyframe.tick)
                {
                    VoiceTagDef voiceTag = (keyframe as ExtendedKeyframe).voice;

                    //probability checking null fix
                    if (voiceTag != null && Rand.Chance(voiceTag.probability))
                    {

                        return voiceTag;

                    }

                    return null;
                }

            }

            return null;

        }

        public SoundDef soundAtTick(int tick)
        {

            //Verse.Keyframe keyframe = this.part.keyframes[0];
            Verse.Keyframe keyframe2 = this.part.keyframes[this.part.keyframes.Count - 1];
            foreach (Verse.Keyframe keyframe in this.part.keyframes)
            {
                if (tick == keyframe.tick)
                {
                    return (keyframe as ExtendedKeyframe).sound;
                }
                
            }

            return null;
        }



        //use extendedkeyframes to determine addon facing
        public Rot4 facingAtTick(int tick)
        {

            //if ticks are < first keyframe tick, just be stuck to first keyframe rot
            if (tick <= this.part.keyframes[0].tick) {

                return (this.part.keyframes[0] as ExtendedKeyframe).rotation;

            }

            //if ticks are > last keyframe tick, just be stuck to last keyframe rot
            if (tick >= this.part.keyframes[this.part.keyframes.Count - 1].tick)
            {

                return (this.part.keyframes[this.part.keyframes.Count - 1] as ExtendedKeyframe).rotation;

            }
            Verse.Keyframe keyframe = this.part.keyframes[0];
            Verse.Keyframe keyframe2 = this.part.keyframes[this.part.keyframes.Count - 1];
            int i = 0;
            while (i < this.part.keyframes.Count)
            {
                if (tick <= this.part.keyframes[i].tick)
                {
                    keyframe2 = this.part.keyframes[i];
                    if (i > 0)
                    {
                        keyframe = this.part.keyframes[i - 1];
                        break;
                    }
                    break;
                }
                else
                {
                    i++;
                }
            }

            return (keyframe as ExtendedKeyframe).rotation;

        }
        public bool visibleAtTick(int tick)
        {
            //if ticks are < first keyframe tick, just be stuck to first keyframe rot
            if (tick <= this.part.keyframes[0].tick)
            {

                return (this.part.keyframes[0] as ExtendedKeyframe).visible;

            }

            //if ticks are > last keyframe tick, just be stuck to last keyframe rot
            if (tick >= this.part.keyframes[this.part.keyframes.Count - 1].tick)
            {

                return (this.part.keyframes[this.part.keyframes.Count - 1] as ExtendedKeyframe).visible;

            }

            Verse.Keyframe keyframe = this.part.keyframes[0];
            Verse.Keyframe keyframe2 = this.part.keyframes[this.part.keyframes.Count - 1];

            int i = 0;
            while (i < this.part.keyframes.Count)
            {
                if (tick <= this.part.keyframes[i].tick)
                {
                    keyframe2 = this.part.keyframes[i];
                    if (i > 0)
                    {
                        keyframe = this.part.keyframes[i - 1];
                        break;
                    }
                    break;
                }
                else
                {
                    i++;
                }
            }

            return (keyframe as ExtendedKeyframe).visible;


        }

        public virtual bool shouldRecache(int tick)
        {
            if (facingAtTick(tick) != facingAtTick(tick - 1) 
                || visibleAtTick(tick) != visibleAtTick(tick - 1)
                || TexPathVariantAtTick(tick) != TexPathVariantAtTick(tick - 1))
            {
                return true;
            }

            return true;
        }

        public int? TexPathVariantAtTick(int tick)
        {

            //if ticks are < first keyframe tick, just be stuck to first keyframe rot
            if (tick <= this.part.keyframes[0].tick)
            {

                return (this.part.keyframes[0] as ExtendedKeyframe).variant;

            }

            //if ticks are > last keyframe tick, just be stuck to last keyframe rot
            if (tick >= this.part.keyframes[this.part.keyframes.Count - 1].tick)
            {

                return (this.part.keyframes[this.part.keyframes.Count - 1] as ExtendedKeyframe).variant;

            }
            Verse.Keyframe keyframe = this.part.keyframes[0];
            Verse.Keyframe keyframe2 = this.part.keyframes[this.part.keyframes.Count - 1];
            int i = 0;
            while (i < this.part.keyframes.Count)
            {
                if (tick <= this.part.keyframes[i].tick)
                {
                    keyframe2 = this.part.keyframes[i];
                    if (i > 0)
                    {
                        keyframe = this.part.keyframes[i - 1];
                        break;
                    }
                    break;
                }
                else
                {
                    i++;
                }
            }

            return (keyframe as ExtendedKeyframe).variant;

        }
    }
}
