using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class MotionWindow : BaseMaidPhotoWindow
{
	public override void Awake()
	{
		base.Awake();
		PhotoMotionData.Create();
		this.noCharaActive = base.GetComponentInChildren<PhotoNoCharaActive>();
		Dictionary<string, List<KeyValuePair<string, object>>> dictionary = new Dictionary<string, List<KeyValuePair<string, object>>>();
		foreach (KeyValuePair<string, List<PhotoMotionData>> keyValuePair in PhotoMotionData.category_list)
		{
			if (!dictionary.ContainsKey(keyValuePair.Key))
			{
				dictionary.Add(keyValuePair.Key, new List<KeyValuePair<string, object>>());
			}
			for (int i = 0; i < keyValuePair.Value.Count; i++)
			{
				dictionary[keyValuePair.Key].Add(new KeyValuePair<string, object>(keyValuePair.Value[i].name, keyValuePair.Value[i]));
			}
		}
		this.PopupAndTabList.SetData(dictionary, true);
		this.PopupAndTabList.onChangePopUpListValue.Add(new Action<KeyValuePair<string, UnityEngine.Object>>(this.OnChangePopUpList));
		this.PopupAndTabList.onSelect.Add(new Action<object>(this.OnSelectItem));
		UTY.GetChildObject(this.content_game_object, "ListParent/DragMat", false).GetComponent<BoxCollider>().enabled = false;
	}

	public override void Start()
	{
		base.Start();
		this.noCharaActive.ChangeMode(true, true);
		this.pose_edit_window_ = (base.mgr.GetWindow(PhotoWindowManager.WindowType.PoseEdit) as PoseEditWindow);
		this.UpdateChildren();
		this.Slider.onChangeValue.Add(new Action<float>(this.OnChangeMotionSlider));
		this.CheckbtnStop.onClick.Add(new Action<WFCheckBox>(this.OnClickStopCheck));
		this.UpdateAnimationData(null);
		this.CopyAndPasteBtn.onCopyEvent.Add(delegate(StringWriter writer)
		{
			if (this.anime_state_ == null)
			{
				return;
			}
			writer.WriteLine(this.CheckbtnStop.check);
			writer.WriteLine(this.anime_state_.time);
		});
		this.CopyAndPasteBtn.onPasteEvent.Add(delegate(StringReader reader)
		{
			if (this.anime_state_ == null)
			{
				return;
			}
			this.CheckbtnStop.check = bool.Parse(reader.ReadLine());
			float num = float.Parse(reader.ReadLine());
			this.OnClickStopCheck(this.CheckbtnStop);
			if (this.CheckbtnStop.check)
			{
				if (this.Slider.MaxNum < num)
				{
					num = this.Slider.MaxNum;
				}
				this.Slider.value = num;
				this.anime_state_.enabled = true;
				this.animation_.Sample();
				this.anime_state_.enabled = false;
			}
		});
	}

	public override void OnMaidAddEvent(Maid maid, bool is_deserialize_load)
	{
		base.OnMaidAddEvent(maid, is_deserialize_load);
		PhotoMotionData photoMotionData = maid.boMAN ? PhotoMotionData.init_data_for_man : PhotoMotionData.init_data;
		Dictionary<string, string> maidStoreData = base.GetMaidStoreData(maid);
		if (!is_deserialize_load)
		{
			maidStoreData.Clear();
			maidStoreData.Add("id", photoMotionData.id.ToString());
		}
		if (!maidStoreData.ContainsKey("is_stop"))
		{
			maidStoreData.Add("is_stop", false.ToString());
		}
		if (!maidStoreData.ContainsKey("time"))
		{
			maidStoreData.Add("time", 0f.ToString());
		}
		photoMotionData = PhotoMotionData.Get(long.Parse(maidStoreData["id"]));
		this.Apply(photoMotionData, maid);
	}

	public new void Update()
	{
		if (this.anime_state_ == null || this.animation_ == null)
		{
			return;
		}
		float value = this.anime_state_.time;
		if (this.anime_state_.length < this.anime_state_.time)
		{
			if (this.anime_state_.wrapMode == WrapMode.ClampForever)
			{
				value = this.anime_state_.length;
			}
			else
			{
				value = this.anime_state_.time - this.anime_state_.length * (float)((int)(this.anime_state_.time / this.anime_state_.length));
			}
		}
		this.is_update_ = true;
		this.Slider.value = value;
		this.is_update_ = false;
	}

	public void OnChangeMotionSlider(float val)
	{
		if (this.is_update_ || this.anime_state_ == null)
		{
			return;
		}
		float time = this.anime_state_.time;
		this.anime_state_.time = val;
		this.anime_state_.enabled = true;
		if (base.mgr.select_maid != null)
		{
			Dictionary<string, string> maidStoreData = base.GetMaidStoreData(base.mgr.select_maid);
			maidStoreData["time"] = val.ToString("G9");
		}
		this.animation_.Sample();
		if (this.CheckbtnStop.check)
		{
			this.anime_state_.enabled = false;
		}
		if (time != val)
		{
			this.pose_edit_window_.OnMotionUpdate(base.mgr.select_maid);
		}
	}

	public void OnClickStopCheck(WFCheckBox check_box)
	{
		if (base.mgr.select_maid == null || this.anime_state_ == null)
		{
			return;
		}
		if (!check_box.check && this.pose_edit_window_.CheckbtnUse.check)
		{
			GameMain.Instance.SysDlg.Show("ポーズエディットのセーブしていないデータは失われます。\nOFFにしますか？", SystemDialog.TYPE.YES_NO, delegate
			{
				this.pose_edit_window_.CheckbtnUse.check = false;
				this.pose_edit_window_.OnClickUseCheckRun(false);
				GameMain.Instance.SysDlg.Close();
				check_box.check = false;
				this.OnClickStopCheckRun(check_box.check);
			}, null);
			check_box.check = true;
		}
		else
		{
			this.OnClickStopCheckRun(check_box.check);
		}
	}

	public void OnClickStopCheckRun(bool check)
	{
		if (!check)
		{
			this.anime_state_.enabled = true;
		}
		else
		{
			this.anime_state_.enabled = false;
		}
		if (base.mgr.select_maid != null)
		{
			Dictionary<string, string> maidStoreData = base.GetMaidStoreData(base.mgr.select_maid);
			maidStoreData["is_stop"] = check.ToString();
			float num = this.anime_state_.time;
			if (this.anime_state_.length < this.anime_state_.time)
			{
				if (this.anime_state_.wrapMode == WrapMode.ClampForever)
				{
					num = this.anime_state_.length;
				}
				else
				{
					num = this.anime_state_.time - this.anime_state_.length * (float)((int)(this.anime_state_.time / this.anime_state_.length));
				}
			}
			maidStoreData["time"] = num.ToString("G9");
		}
	}

	public override void OnMaidChangeEvent(Maid maid)
	{
		base.OnMaidChangeEvent(maid);
		this.PopupAndTabList.ResetSelect();
		this.noCharaActive.ChangeMode(maid == null, true);
		if (maid == null)
		{
			UTY.GetChildObject(this.content_game_object, "ListParent/DragMat", false).GetComponent<BoxCollider>().enabled = false;
			this.PopupAndTabList.popup_value_list = null;
			this.PopupAndTabList.SetPopupValue(this.PopupAndTabList.popup_value_list[0].Key);
		}
		else
		{
			UTY.GetChildObject(this.content_game_object, "ListParent/DragMat", false).GetComponent<BoxCollider>().enabled = true;
			Dictionary<string, string> maidStoreData = base.GetMaidStoreData(maid);
			PhotoMotionData photoMotionData = PhotoMotionData.Get(long.Parse(maidStoreData["id"]));
			this.PopupAndTabList.popup_value_list = (maid.boMAN ? PhotoMotionData.popup_category_list_for_man : PhotoMotionData.popup_category_list);
			this.PopupAndTabList.SetPopupValue(photoMotionData.category);
			this.PopupAndTabList.SetSelectButton(photoMotionData, false);
		}
		this.UpdateAnimationData(maid);
	}

	protected void OnChangePopUpList(KeyValuePair<string, UnityEngine.Object> popup_val)
	{
	}

	public override void OnDeserializeEvent()
	{
		CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
		List<string> maidStoreGuidList = base.mgr.GetMaidStoreGuidList();
		for (int i = 0; i < maidStoreGuidList.Count; i++)
		{
			Dictionary<string, string> maidStoreData = base.GetMaidStoreData(maidStoreGuidList[i]);
			if (maidStoreData.Count != 0)
			{
				bool flag = false;
				int num = 0;
				while (num < characterMgr.GetManCount() && !flag)
				{
					if (characterMgr.GetMan(num) != null && characterMgr.GetMan(num).status.guid == maidStoreGuidList[i])
					{
						flag = true;
					}
					num++;
				}
				PhotoMotionData photoMotionData = PhotoMotionData.Get(long.Parse(maidStoreData["id"]));
				if (photoMotionData == null)
				{
					photoMotionData = (flag ? PhotoMotionData.init_data_for_man : PhotoMotionData.init_data);
				}
				maidStoreData["id"] = photoMotionData.id.ToString();
			}
		}
	}

	private void OnSelectItem(object select_data_obj)
	{
		if (base.mgr.select_maid == null || select_data_obj == null)
		{
			return;
		}
		PhotoMotionData photoMotionData = select_data_obj as PhotoMotionData;
		Dictionary<string, string> maidStoreData = base.GetMaidStoreData(base.mgr.select_maid);
		maidStoreData["id"] = photoMotionData.id.ToString();
		maidStoreData["time"] = 0f.ToString();
		this.Apply(photoMotionData, base.mgr.select_maid);
	}

	public void Apply(PhotoMotionData motion_data, Maid target_maid)
	{
		motion_data.Apply(target_maid);
		this.UpdateAnimationData(target_maid);
		this.pose_edit_window_.OnMotionUpdate(target_maid);
	}

	public bool AddMyPose(string full_path)
	{
		PhotoMotionData photoMotionData = PhotoMotionData.AddMyPose(full_path);
		if (photoMotionData != null)
		{
			this.PopupAndTabList.AddData("マイポーズ", new KeyValuePair<string, object>(photoMotionData.name, photoMotionData));
		}
		return photoMotionData != null;
	}

	public void OnUpdateMyPose(string full_path)
	{
		List<PhotoMotionData> list = PhotoMotionData.category_list["マイポーズ"];
		PhotoMotionData photoMotionData = null;
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i].direct_file == full_path)
			{
				photoMotionData = list[i];
				break;
			}
		}
		if (photoMotionData == null)
		{
			Debug.LogError("モーションデータがnullです");
			return;
		}
		Dictionary<string, PhotoMotionData> dictionary = new Dictionary<string, PhotoMotionData>();
		List<string> maidStoreGuidList = base.mgr.GetMaidStoreGuidList();
		for (int j = 0; j < maidStoreGuidList.Count; j++)
		{
			Dictionary<string, string> maidStoreData = base.mgr.GetMaidStoreData(maidStoreGuidList[j], this);
			Dictionary<string, string> maidStoreData2 = base.mgr.GetMaidStoreData(maidStoreGuidList[j], base.mgr.GetWindow(PhotoWindowManager.WindowType.PoseEdit));
			if (maidStoreData.Count > 0 && maidStoreData2.Count > 0)
			{
				if (!bool.Parse(maidStoreData2["use"]) && long.Parse(maidStoreData["id"]) == photoMotionData.id)
				{
					photoMotionData.Apply(GameMain.Instance.CharacterMgr.GetMaid(maidStoreGuidList[j]));
				}
			}
		}
	}

	public void UpdateAnimationData(Maid target_maid)
	{
		if (target_maid != null && target_maid.body0 != null && target_maid.body0 != null && target_maid.body0.m_Bones.GetComponent<Animation>() != null)
		{
			this.animation_ = target_maid.body0.m_Bones.GetComponent<Animation>();
			this.anime_state_ = this.animation_[target_maid.body0.LastAnimeFN.ToLower()];
			if (this.anime_state_.length == 0f)
			{
				this.CopyAndPasteBtn.enabled = false;
				WFCheckBox checkbtnStop = this.CheckbtnStop;
				bool flag = false;
				this.CheckbtnStop.check = flag;
				checkbtnStop.enabled = flag;
				this.Slider.enabled = false;
				this.animation_ = null;
				this.anime_state_ = null;
				this.Slider.MaxNum = 1f;
				this.Slider.value = 0f;
			}
			else
			{
				Dictionary<string, string> maidStoreData = base.GetMaidStoreData(target_maid);
				this.Slider.enabled = true;
				this.Slider.MaxNum = this.anime_state_.length;
				if (this.anime_state_.wrapMode == WrapMode.Once)
				{
					this.anime_state_.wrapMode = WrapMode.ClampForever;
				}
				this.CopyAndPasteBtn.enabled = true;
				this.CheckbtnStop.enabled = true;
				this.CheckbtnStop.check = bool.Parse(maidStoreData["is_stop"]);
				if (this.CheckbtnStop.check)
				{
					this.OnClickStopCheck(this.CheckbtnStop);
					this.Slider.value = float.Parse(maidStoreData["time"]);
					this.anime_state_.enabled = true;
					this.animation_.Sample();
					this.anime_state_.enabled = false;
				}
			}
		}
		else
		{
			this.CopyAndPasteBtn.enabled = false;
			WFCheckBox checkbtnStop2 = this.CheckbtnStop;
			bool flag = false;
			this.CheckbtnStop.check = flag;
			checkbtnStop2.enabled = flag;
			this.Slider.enabled = false;
			this.animation_ = null;
			this.anime_state_ = null;
			this.Slider.MaxNum = 1f;
			this.Slider.value = 0f;
		}
	}

	public PopupAndTabList PopupAndTabList;

	public WFCheckBox CheckbtnStop;

	public PhotoSliderAndInput Slider;

	public WindowPartsInputSliderSet CopyAndPasteBtn;

	private PhotoNoCharaActive noCharaActive;

	private PoseEditWindow pose_edit_window_;

	private Animation animation_;

	private AnimationState anime_state_;

	private bool is_update_;
}
