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

public class WindowPartsFaceMorph : MonoBehaviour
{
	public void Awake()
	{
		for (int i = 0; i < this.propValInput.Length; i++)
		{
			this.propValInput[i].onChangeValue.Add(new Action<WindowPartsInputSliderSet.SliderAndInputSet, float>(this.OnChangetPropInputValue));
		}
	}

	public void SetFaceWindow(FaceWindow faceWindow)
	{
		this.faceWindow = faceWindow;
	}

	public void OnDestroy()
	{
	}

	public void OnMaidAddEvent(Maid maid, bool isDeserializeLoad, Dictionary<string, string> storeData)
	{
		if (maid == null || maid.boMAN)
		{
			return;
		}
		this.SetPhotoFaceData(PhotoFaceData.Get(long.Parse(storeData["id"])));
		this.CreateBackupData(maid);
		if (!isDeserializeLoad)
		{
			for (int i = 0; i < this.propValInput.Length; i++)
			{
				for (int j = 0; j < this.propValInput[i].SliderAndInput.Length; j++)
				{
					WindowPartsInputSliderSet.SliderAndInputSet sliderAndInputSet = this.propValInput[i].SliderAndInput[j];
					storeData.Add("morph_val_" + sliderAndInputSet.Name, this.GetValue(maid, sliderAndInputSet.Name).ToString("G9"));
				}
			}
		}
		else
		{
			for (int k = 0; k < this.propValInput.Length; k++)
			{
				for (int l = 0; l < this.propValInput[k].SliderAndInput.Length; l++)
				{
					WindowPartsInputSliderSet.SliderAndInputSet sliderAndInputSet2 = this.propValInput[k].SliderAndInput[l];
					this.SetValue(maid, sliderAndInputSet2.Name, float.Parse(storeData["morph_val_" + sliderAndInputSet2.Name]));
				}
			}
		}
	}

	public void CreateBackupData(Maid maid)
	{
		if (maid == null || maid.boMAN)
		{
			return;
		}
		string guid = maid.status.guid;
		this.RemoveBackupData(maid);
		Dictionary<string, float[]> dictionary = new Dictionary<string, float[]>();
		foreach (KeyValuePair<string, float[]> keyValuePair in maid.body0.Face.morph.dicBlendSet)
		{
			float[] array = new float[keyValuePair.Value.Length];
			keyValuePair.Value.CopyTo(array, 0);
			dictionary.Add(keyValuePair.Key, array);
		}
		this.backupBlendSets.Add(guid, dictionary);
	}

	public void ApplyBackupData(Maid maid)
	{
		if (maid == null)
		{
			return;
		}
		string guid = maid.status.guid;
		if (this.backupBlendSets.ContainsKey(guid))
		{
			Dictionary<string, float[]> dictionary = this.backupBlendSets[guid];
			foreach (KeyValuePair<string, float[]> keyValuePair in maid.body0.Face.morph.dicBlendSet)
			{
				dictionary[keyValuePair.Key].CopyTo(maid.body0.Face.morph.dicBlendSet[keyValuePair.Key], 0);
			}
		}
		else
		{
			Debug.Log("FaceMorphのバックアップデータが見つかりませんでした");
		}
	}

	public bool RemoveBackupData(Maid maid)
	{
		if (maid == null)
		{
			return false;
		}
		bool result = false;
		string guid = maid.status.guid;
		if (this.backupBlendSets.ContainsKey(guid))
		{
			this.backupBlendSets.Remove(guid);
			result = true;
		}
		return result;
	}

	public void Apply(Maid maid)
	{
		if (maid == null)
		{
			return;
		}
		this.UpdateGui(maid);
	}

	public void OnChangetPropInputValue(WindowPartsInputSliderSet.SliderAndInputSet input_object, float val)
	{
		if (this.faceWindow == null || this.faceWindow.mgr.select_maid == null)
		{
			return;
		}
		string name = input_object.Name;
		this.SetValue(this.faceWindow.mgr.select_maid, name, val);
		this.faceWindow.GetMaidStoreData(this.faceWindow.mgr.select_maid)["morph_val_" + name] = val.ToString("G9");
	}

	public void UpdateGui(Maid maid)
	{
		if (maid == null)
		{
			return;
		}
		for (int i = 0; i < this.propValInput.Length; i++)
		{
			for (int j = 0; j < this.propValInput[i].SliderAndInput.Length; j++)
			{
				string name = this.propValInput[i].SliderAndInput[j].Name;
				PhotoSliderAndInput sliderAndInput = this.propValInput[i].GetSliderAndInput(name);
				sliderAndInput.ResetNum = this.backupBlendSets[maid.status.guid][this.selectData.setting_name][this.blendIdDictionary[name]];
				sliderAndInput.value = this.GetValue(maid, name);
				this.faceWindow.GetMaidStoreData(maid)["morph_val_" + name] = sliderAndInput.value.ToString("G9");
			}
		}
	}

	public void SetPhotoFaceData(PhotoFaceData data)
	{
		this.selectData = data;
	}

	private void SetValue(Maid maid, string name, float val)
	{
		if (maid == null || maid.body0 == null || maid.body0.Face == null)
		{
			return;
		}
		if (this.blendIdDictionary.Count <= 0)
		{
			List<BlendData> blendDatas = maid.body0.Face.morph.BlendDatas;
			for (int i = 0; i < blendDatas.Count; i++)
			{
				if (blendDatas[i] != null)
				{
					if (!this.blendIdDictionary.ContainsKey(blendDatas[i].name))
					{
						this.blendIdDictionary.Add(blendDatas[i].name, i);
					}
				}
			}
		}
		this.selectData.BlendArray(maid)[this.blendIdDictionary[name]] = val;
	}

	private float GetValue(Maid maid, string name)
	{
		if (maid == null || maid.body0 == null || maid.body0.Face == null)
		{
			return 0f;
		}
		if (this.blendIdDictionary.Count <= 0)
		{
			List<BlendData> blendDatas = maid.body0.Face.morph.BlendDatas;
			for (int i = 0; i < blendDatas.Count; i++)
			{
				if (blendDatas[i] != null)
				{
					if (!this.blendIdDictionary.ContainsKey(blendDatas[i].name))
					{
						this.blendIdDictionary.Add(blendDatas[i].name, i);
					}
				}
			}
		}
		return this.selectData.BlendArray(maid)[this.blendIdDictionary[name]];
	}

	public bool visible
	{
		get
		{
			return base.gameObject.activeSelf;
		}
		set
		{
			base.gameObject.SetActive(value);
		}
	}

	[SerializeField]
	private WindowPartsInputSliderSet[] propValInput;

	[HideInInspector]
	public bool isFacialUpdate;

	private FaceWindow faceWindow;

	private Dictionary<string, int> blendIdDictionary = new Dictionary<string, int>();

	private Dictionary<string, Dictionary<string, float[]>> backupBlendSets = new Dictionary<string, Dictionary<string, float[]>>();

	private PhotoFaceData selectData;
}
