using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;

[Serializable]
public class AMCameraSwitcherTrack : AMTrack
{
	public Camera[] cachedAllCameras
	{
		get
		{
			if (this._cachedAllCameras == null)
			{
				this._cachedAllCameras = this.getAllCameras();
			}
			return this._cachedAllCameras;
		}
		set
		{
			this._cachedAllCameras = value;
		}
	}

	public override string getTrackType()
	{
		return "Camera Switcher";
	}

	public void addKey(int _frame, Camera camera = null, AMCameraSwitcherKey keyToClone = null)
	{
		foreach (AMKey amkey in this.keys)
		{
			AMCameraSwitcherKey amcameraSwitcherKey = (AMCameraSwitcherKey)amkey;
			if (amcameraSwitcherKey.frame == _frame)
			{
				if (camera != null)
				{
					amcameraSwitcherKey.camera = camera;
					amcameraSwitcherKey.type = 0;
					this.updateCache();
				}
				return;
			}
		}
		AMCameraSwitcherKey amcameraSwitcherKey2 = ScriptableObject.CreateInstance<AMCameraSwitcherKey>();
		if (keyToClone)
		{
			amcameraSwitcherKey2 = (AMCameraSwitcherKey)keyToClone.CreateClone();
		}
		else
		{
			amcameraSwitcherKey2.type = 0;
			amcameraSwitcherKey2.still = !AMTake.isProLicense;
			amcameraSwitcherKey2.easeType = 13;
		}
		amcameraSwitcherKey2.frame = _frame;
		if (camera != null)
		{
			amcameraSwitcherKey2.camera = camera;
			amcameraSwitcherKey2.type = 0;
		}
		this.keys.Add(amcameraSwitcherKey2);
		this.updateCache();
	}

	public override void updateCache()
	{
		base.destroyCache();
		this.cache = new List<AMAction>();
		base.sortKeys();
		for (int i = 0; i < this.keys.Count; i++)
		{
			AMCameraSwitcherAction amcameraSwitcherAction = ScriptableObject.CreateInstance<AMCameraSwitcherAction>();
			amcameraSwitcherAction.startFrame = this.keys[i].frame;
			if (this.keys.Count > i + 1)
			{
				amcameraSwitcherAction.endFrame = this.keys[i + 1].frame;
			}
			else
			{
				amcameraSwitcherAction.endFrame = -1;
			}
			amcameraSwitcherAction.startTargetType = (this.keys[i] as AMCameraSwitcherKey).type;
			if (amcameraSwitcherAction.startTargetType == 0)
			{
				amcameraSwitcherAction.startCamera = (this.keys[i] as AMCameraSwitcherKey).camera;
			}
			else
			{
				amcameraSwitcherAction.startColor = (this.keys[i] as AMCameraSwitcherKey).color;
			}
			if (amcameraSwitcherAction.endFrame != -1)
			{
				amcameraSwitcherAction.endTargetType = (this.keys[i + 1] as AMCameraSwitcherKey).type;
				if (amcameraSwitcherAction.endTargetType == 0)
				{
					amcameraSwitcherAction.endCamera = (this.keys[i + 1] as AMCameraSwitcherKey).camera;
				}
				else
				{
					amcameraSwitcherAction.endColor = (this.keys[i + 1] as AMCameraSwitcherKey).color;
				}
			}
			amcameraSwitcherAction.cameraFadeType = (this.keys[i] as AMCameraSwitcherKey).cameraFadeType;
			amcameraSwitcherAction.cameraFadeParameters = new List<float>((this.keys[i] as AMCameraSwitcherKey).cameraFadeParameters);
			amcameraSwitcherAction.irisShape = (this.keys[i] as AMCameraSwitcherKey).irisShape;
			amcameraSwitcherAction.still = (this.keys[i] as AMCameraSwitcherKey).still;
			amcameraSwitcherAction.easeType = (this.keys[i] as AMCameraSwitcherKey).easeType;
			amcameraSwitcherAction.customEase = new List<float>(this.keys[i].customEase);
			this.cache.Add(amcameraSwitcherAction);
		}
		this._cachedAllCameras = this.getAllCameras();
		base.updateCache();
	}

	public override void previewFrame(float frame, AMTrack extraTrack = null)
	{
		if (this.cache == null || this.cache.Count <= 0)
		{
			return;
		}
		bool isPreview = !Application.isPlaying;
		int i = 0;
		while (i < this.cache.Count)
		{
			if (frame <= (float)(this.cache[i] as AMCameraSwitcherAction).startFrame)
			{
				AMCameraFade.reset();
				if (!(this.cache[i] as AMCameraSwitcherAction).hasStartTarget())
				{
					return;
				}
				if ((this.cache[i] as AMCameraSwitcherAction).startTargetType == 0)
				{
					AMTween.SetTopCamera((this.cache[i] as AMCameraSwitcherAction).startCamera, this.cachedAllCameras);
				}
				else
				{
					this.showColor((this.cache[i] as AMCameraSwitcherAction).startColor, isPreview);
				}
				return;
			}
			else if (frame <= (float)(this.cache[i] as AMCameraSwitcherAction).endFrame)
			{
				if (!(this.cache[i] as AMCameraSwitcherAction).hasStartTarget() || !(this.cache[i] as AMCameraSwitcherAction).hasEndTarget())
				{
					return;
				}
				if ((this.cache[i] as AMCameraSwitcherAction).targetsAreEqual())
				{
					AMCameraFade.reset();
					if ((this.cache[i] as AMCameraSwitcherAction).startTargetType == 0)
					{
						AMTween.SetTopCamera((this.cache[i] as AMCameraSwitcherAction).startCamera, this.cachedAllCameras);
					}
					else
					{
						this.showColor((this.cache[i] as AMCameraSwitcherAction).startColor, isPreview);
					}
				}
				else
				{
					AMCameraFade.clearRenderTexture();
					this.previewCameraFade(frame, this.cache[i] as AMCameraSwitcherAction, isPreview);
				}
				return;
			}
			else if (i == this.cache.Count - 2)
			{
				AMCameraFade.reset();
				if (!(this.cache[i] as AMCameraSwitcherAction).hasEndTarget())
				{
					return;
				}
				if ((this.cache[i] as AMCameraSwitcherAction).endTargetType == 0)
				{
					AMTween.SetTopCamera((this.cache[i] as AMCameraSwitcherAction).endCamera, this.cachedAllCameras);
				}
				else
				{
					this.showColor((this.cache[i] as AMCameraSwitcherAction).endColor, isPreview);
				}
				return;
			}
			else
			{
				i++;
			}
		}
	}

	public Camera[] getAllCameras()
	{
		List<Camera> list = new List<Camera>();
		foreach (AMKey amkey in this.keys)
		{
			AMCameraSwitcherKey amcameraSwitcherKey = (AMCameraSwitcherKey)amkey;
			if (amcameraSwitcherKey.type == 0 && amcameraSwitcherKey.camera)
			{
				list.Add(amcameraSwitcherKey.camera);
			}
		}
		return list.Distinct<Camera>().ToArray<Camera>();
	}

	public Texture[] getAllTextures()
	{
		List<Texture> list = new List<Texture>();
		foreach (AMKey amkey in this.keys)
		{
			AMCameraSwitcherKey amcameraSwitcherKey = (AMCameraSwitcherKey)amkey;
			if (amcameraSwitcherKey.irisShape && AMCameraFade.needsTexture(amcameraSwitcherKey.cameraFadeType))
			{
				list.Add(amcameraSwitcherKey.irisShape);
			}
		}
		return list.Distinct<Texture>().ToArray<Texture>();
	}

	private void previewCameraFade(float frame, AMCameraSwitcherAction action, bool isPreview)
	{
		if (action.cameraFadeType == 5)
		{
			if (action.endTargetType == 0)
			{
				if (action.endCamera)
				{
					AMTween.SetTopCamera(action.endCamera, this.cachedAllCameras);
				}
				AMCameraFade.reset();
			}
			else
			{
				this.showColor(action.endColor, isPreview);
			}
			return;
		}
		AMCameraFade cameraFade = AMCameraFade.getCameraFade(isPreview);
		if (Application.isPlaying)
		{
			cameraFade.keepAlivePreview = true;
		}
		cameraFade.isReset = false;
		bool flag = action.isReversed();
		int firstTargetType = (!flag) ? action.startTargetType : action.endTargetType;
		int secondTargetType = (!flag) ? action.endTargetType : action.startTargetType;
		this.setRenderTexture(cameraFade, frame, firstTargetType, secondTargetType, flag, action, isPreview);
		this.setColors(cameraFade, firstTargetType, secondTargetType, flag, action);
		if (cameraFade.irisShape != action.irisShape)
		{
			cameraFade.irisShape = action.irisShape;
		}
		cameraFade.mode = action.cameraFadeType;
		cameraFade.setupMaterials();
		cameraFade.r = action.cameraFadeParameters.ToArray();
		AnimationCurve curve = null;
		AMTween.EasingFunction easingFunction;
		if (action.hasCustomEase())
		{
			if (AMCameraSwitcherTrack.<>f__mg$cache0 == null)
			{
				AMCameraSwitcherTrack.<>f__mg$cache0 = new AMTween.EasingFunction(AMTween.customEase);
			}
			easingFunction = AMCameraSwitcherTrack.<>f__mg$cache0;
			curve = action.easeCurve;
		}
		else
		{
			easingFunction = AMTween.GetEasingFunction((AMTween.EaseType)action.easeType);
		}
		float num = (frame - (float)action.startFrame) / (float)(action.endFrame - action.startFrame);
		float value = easingFunction(1f, 0f, num, curve);
		cameraFade.value = value;
		cameraFade.percent = num;
	}

	private void setColors(AMCameraFade cf, int firstTargetType, int secondTargetType, bool isReversed, AMCameraSwitcherAction action)
	{
		Color colorTex = (!isReversed) ? action.startColor : action.endColor;
		Color colorBG = (!isReversed) ? action.endColor : action.startColor;
		if (firstTargetType == 1)
		{
			cf.colorTex = colorTex;
			cf.hasColorTex = true;
		}
		else
		{
			cf.hasColorTex = false;
		}
		if (secondTargetType == 1)
		{
			cf.colorBG = colorBG;
			cf.hasColorBG = true;
		}
		else
		{
			cf.hasColorBG = false;
		}
		if (!Application.isPlaying && (firstTargetType == 1 || secondTargetType == 1))
		{
			cf.transform.position = new Vector3(cf.transform.position.x, cf.transform.position.y, cf.transform.position.z);
		}
	}

	private void setRenderTexture(AMCameraFade cf, float frame, int firstTargetType, int secondTargetType, bool isReversed, AMCameraSwitcherAction action, bool isPreview)
	{
		Camera camera = (!isReversed) ? action.startCamera : action.endCamera;
		Camera top = (!isReversed) ? action.endCamera : action.startCamera;
		if (isReversed && frame == (float)action.startFrame)
		{
			if (firstTargetType == 0)
			{
				AMTween.SetTopCamera(camera, this.cachedAllCameras);
			}
		}
		else if (secondTargetType == 0)
		{
			AMTween.SetTopCamera(top, this.cachedAllCameras);
		}
		if (action.still || (firstTargetType != 0 && secondTargetType != 0))
		{
			return;
		}
		bool isProLicense = AMTake.isProLicense;
		if (firstTargetType == 0 && (cf.shouldUpdateRenderTexture || (isProLicense && (!camera.targetTexture || !cf.isRenderTextureSetupFor(camera)))))
		{
			if (isProLicense)
			{
				cf.setupRenderTexture(camera);
			}
			else
			{
				cf.useRenderTexture = false;
				cf.colorTex = Color.white;
				cf.tex2d = (Texture2D)Resources.Load("am_indie_placeholder");
				cf.hasColorTex = false;
				if (!cf.placeholder)
				{
					cf.placeholder = true;
				}
			}
		}
	}

	public AMCameraSwitcherTrack.cfTuple getCameraFadeTupleForFrame(int frame)
	{
		if (this.cache == null || this.cache.Count <= 0)
		{
			return new AMCameraSwitcherTrack.cfTuple(0, 0, 0, null, null, false);
		}
		int i = 0;
		while (i < this.cache.Count)
		{
			if (frame < (this.cache[i] as AMCameraSwitcherAction).startFrame)
			{
				break;
			}
			if (frame < (this.cache[i] as AMCameraSwitcherAction).endFrame)
			{
				if (!(this.cache[i] as AMCameraSwitcherAction).still || (this.cache[i] as AMCameraSwitcherAction).cameraFadeType == 5 || (this.cache[i] as AMCameraSwitcherAction).targetsAreEqual())
				{
					break;
				}
				bool flag = (this.cache[i] as AMCameraSwitcherAction).isReversed();
				AMCameraSwitcherAction amcameraSwitcherAction = this.cache[i] as AMCameraSwitcherAction;
				if (flag)
				{
					return new AMCameraSwitcherTrack.cfTuple(amcameraSwitcherAction.endFrame, amcameraSwitcherAction.endTargetType, amcameraSwitcherAction.startTargetType, amcameraSwitcherAction.endCamera, amcameraSwitcherAction.startCamera, flag);
				}
				return new AMCameraSwitcherTrack.cfTuple(amcameraSwitcherAction.startFrame, amcameraSwitcherAction.startTargetType, amcameraSwitcherAction.endTargetType, amcameraSwitcherAction.startCamera, amcameraSwitcherAction.endCamera, flag);
			}
			else
			{
				i++;
			}
		}
		return new AMCameraSwitcherTrack.cfTuple(0, 0, 0, null, null, false);
	}

	private void showColor(Color color, bool isPreview)
	{
		AMCameraFade cameraFade = AMCameraFade.getCameraFade(isPreview);
		bool flag = false;
		if (!cameraFade.hasColorTex || cameraFade.colorTex != color)
		{
			cameraFade.colorTex = color;
			cameraFade.hasColorTex = true;
			flag = true;
		}
		if (cameraFade.isReset)
		{
			cameraFade.isReset = false;
			flag = true;
		}
		if (cameraFade.hasColorBG)
		{
			cameraFade.hasColorBG = false;
			flag = true;
		}
		if (cameraFade.value != 1f)
		{
			cameraFade.value = 1f;
			cameraFade.percent = 0f;
			flag = true;
		}
		if (cameraFade.mode != 0)
		{
			cameraFade.mode = 0;
			flag = true;
		}
		if (!Application.isPlaying && flag)
		{
			cameraFade.transform.position = new Vector3(cameraFade.transform.position.x, cameraFade.transform.position.y, cameraFade.transform.position.z);
		}
	}

	public override AnimatorTimeline.JSONInit getJSONInit()
	{
		if (this.keys.Count <= 0)
		{
			return null;
		}
		string text;
		if ((this.keys[0] as AMCameraSwitcherKey).type == 0)
		{
			if ((this.keys[0] as AMCameraSwitcherKey).camera == null)
			{
				return null;
			}
			text = "camera";
		}
		else
		{
			text = "color";
		}
		AnimatorTimeline.JSONInit jsoninit = new AnimatorTimeline.JSONInit();
		jsoninit.type = "cameraswitcher";
		jsoninit.typeExtra = text;
		if (text == "camera")
		{
			jsoninit.go = (this.keys[0] as AMCameraSwitcherKey).camera.gameObject.name;
		}
		else
		{
			AnimatorTimeline.JSONColor jsoncolor = new AnimatorTimeline.JSONColor();
			jsoncolor.setValue((this.keys[0] as AMCameraSwitcherKey).color);
			jsoninit._color = jsoncolor;
		}
		Camera[] allCameras = this.getAllCameras();
		jsoninit.strings = new string[allCameras.Length];
		for (int i = 0; i < allCameras.Length; i++)
		{
			jsoninit.strings[i] = allCameras[i].gameObject.name;
		}
		return jsoninit;
	}

	public override List<GameObject> getDependencies()
	{
		List<GameObject> list = new List<GameObject>();
		foreach (AMKey amkey in this.keys)
		{
			AMCameraSwitcherKey amcameraSwitcherKey = (AMCameraSwitcherKey)amkey;
			if (amcameraSwitcherKey.type == 0 && amcameraSwitcherKey.camera)
			{
				list.Add(amcameraSwitcherKey.camera.gameObject);
			}
		}
		return list;
	}

	public override List<GameObject> updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences)
	{
		List<GameObject> list = new List<GameObject>();
		for (int i = 0; i < oldReferences.Count; i++)
		{
			foreach (AMKey amkey in this.keys)
			{
				AMCameraSwitcherKey amcameraSwitcherKey = (AMCameraSwitcherKey)amkey;
				if (amcameraSwitcherKey.type == 0 && amcameraSwitcherKey.camera && oldReferences[i] == amcameraSwitcherKey.camera.gameObject)
				{
					Camera camera = (Camera)newReferences[i].GetComponent(typeof(Camera));
					if (!camera)
					{
						Debug.LogWarning("Animator: Camera Switcher component 'Camera' not found on new reference for GameObject '" + amcameraSwitcherKey.camera.gameObject.name + "'. Duplicate not replaced.");
						list.Add(oldReferences[i]);
					}
					else
					{
						amcameraSwitcherKey.camera = camera;
					}
				}
			}
		}
		return list;
	}

	private Camera[] _cachedAllCameras;

	[CompilerGenerated]
	private static AMTween.EasingFunction <>f__mg$cache0;

	public struct cfTuple
	{
		public cfTuple(int _frame, int _type1, int _type2, Camera _camera1, Camera _camera2, bool _isReversed)
		{
			this.frame = _frame;
			this.type1 = _type1;
			this.type2 = _type2;
			this.camera1 = _camera1;
			this.camera2 = _camera2;
			this.isReversed = _isReversed;
		}

		public int frame;

		public int type1;

		public int type2;

		public Camera camera1;

		public Camera camera2;

		public bool isReversed;
	}
}
