using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using UnityEngine;
using wf;

public class WindowPartsFingerPreset : MonoBehaviour
{
	public void Awake()
	{
		this.PresetLoad.CheckBoxPresetVisibleSwitch.check = false;
		this.PresetLoad.CheckBoxPresetVisibleSwitch.onClick.Add(new Action<WFCheckBox>(this.OnClickPresetSwitch));
		for (int i = 0; i < this.PresetSaveBtnDataArray.Length; i++)
		{
			EventDelegate.Add(this.PresetSaveBtnDataArray[i].btn.onClick, new EventDelegate.Callback(this.OnClickPresetSave));
		}
		if (this.RightCopyToLeft != null)
		{
			EventDelegate.Add(this.RightCopyToLeft.onClick, new EventDelegate.Callback(this.OnClickRightCopyToLeft));
			EventDelegate eventDelegate = new EventDelegate(this, "OnBtnMouseHoverIn");
			eventDelegate.parameters[0].value = "左へコピー";
			this.RightCopyToLeft.gameObject.GetComponent<UIEventTrigger>().onHoverOver.Add(eventDelegate);
			EventDelegate.Add(this.RightCopyToLeft.gameObject.GetComponent<UIEventTrigger>().onHoverOut, new EventDelegate.Callback(this.OnMouseHoverOut));
		}
		if (this.LeftCopyToRight != null)
		{
			EventDelegate.Add(this.LeftCopyToRight.onClick, new EventDelegate.Callback(this.OnClickLeftCopyToLeft));
			EventDelegate eventDelegate2 = new EventDelegate(this, "OnBtnMouseHoverIn");
			eventDelegate2.parameters[0].value = "右へコピー";
			this.LeftCopyToRight.gameObject.GetComponent<UIEventTrigger>().onHoverOver.Add(eventDelegate2);
			EventDelegate.Add(this.LeftCopyToRight.gameObject.GetComponent<UIEventTrigger>().onHoverOut, new EventDelegate.Callback(this.OnMouseHoverOut));
		}
		if (this.Swap != null)
		{
			EventDelegate.Add(this.Swap.onClick, new EventDelegate.Callback(this.OnClickSwap));
			EventDelegate eventDelegate3 = new EventDelegate(this, "OnBtnMouseHoverIn");
			eventDelegate3.parameters[0].value = "交換";
			this.Swap.gameObject.GetComponent<UIEventTrigger>().onHoverOver.Add(eventDelegate3);
			EventDelegate.Add(this.Swap.gameObject.GetComponent<UIEventTrigger>().onHoverOut, new EventDelegate.Callback(this.OnMouseHoverOut));
		}
	}

	public void Start()
	{
		this.OnClickPresetSwitch(this.PresetLoad.CheckBoxPresetVisibleSwitch);
	}

	public void OnClickPresetSwitch(WFCheckBox check_box)
	{
		if (check_box.check)
		{
			this.PresetLoad.PresetList.SetActive(true);
			foreach (GameObject gameObject in this.HideObjectArray)
			{
				gameObject.SetActive(false);
			}
			this.UpdatePresetList();
		}
		else
		{
			this.PresetLoad.PresetList.SetActive(false);
			foreach (GameObject gameObject2 in this.HideObjectArray)
			{
				gameObject2.SetActive(true);
			}
		}
	}

	private void UpdatePresetList()
	{
		if (this.PresetLoad.Grid == null)
		{
			return;
		}
		UIGrid grid = this.PresetLoad.Grid;
		List<Transform> childList = grid.GetChildList();
		for (int i = 0; i < childList.Count; i++)
		{
			UnityEngine.Object.DestroyImmediate(childList[i].gameObject);
		}
		Action<string, List<string>> action = delegate(string path, List<string> result_list)
		{
			string[] files = Directory.GetFiles(path);
			for (int j = 0; j < files.Length; j++)
			{
				if (Path.GetExtension(files[j]) == ".xml")
				{
					result_list.Add(files[j]);
				}
			}
		};
		List<string> list = new List<string>();
		action(this.GetFolderPath(this.PresetLoad.type), list);
		foreach (string path2 in list)
		{
			GameObject gameObject = Utility.CreatePrefab(grid.gameObject, "ScenePhotoMode/WindowListItemFingerPresetButton", true);
			gameObject.name = Path.GetFileNameWithoutExtension(path2);
			UTY.GetChildObject(gameObject, "Text", false).GetComponent<UILabel>().text = gameObject.name;
			EventDelegate.Add(UTY.GetChildObject(gameObject, "LeftBtn", false).GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.OnClickPresetBtnLeft));
			UTY.GetChildObject(gameObject, "LeftBtn", false).name = gameObject.name;
			EventDelegate.Add(UTY.GetChildObject(gameObject, "RightBtn", false).GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.OnClickPresetBtnRight));
			UTY.GetChildObject(gameObject, "RightBtn", false).name = gameObject.name;
		}
		grid.Reposition();
		NGUITools.FindInParents<UIScrollView>(grid.transform).ResetPosition();
	}

	public void OnClickPresetBtnRight()
	{
		this.OnClickPresetBtn(this.PresetLoad.right_blend);
	}

	public void OnClickPresetBtnLeft()
	{
		this.OnClickPresetBtn(this.PresetLoad.left_blend);
	}

	public void OnClickPresetBtn(WindowPartsFingerBlend target)
	{
		string name = UIButton.current.name;
		string text = this.GetFolderPath(this.PresetLoad.type) + "/" + name + ".xml";
		if (!File.Exists(text))
		{
			return;
		}
		XDocument xdocument = XDocument.Load(text);
		XElement xelement = xdocument.Element("FingerData");
		if (xelement.IsEmpty || xelement.Element("GameVersion").IsEmpty || xelement.Element("RightData").IsEmpty || xelement.Element("BinaryData").IsEmpty)
		{
			return;
		}
		bool flag = bool.Parse(xelement.Element("RightData").Value);
		string value = xelement.Element("BinaryData").Value;
		bool mirroring;
		if (flag)
		{
			mirroring = (target.BlendType != WindowPartsFingerBlend.Type.RightArm && target.BlendType != WindowPartsFingerBlend.Type.RightLeg);
		}
		else
		{
			mirroring = (target.BlendType != WindowPartsFingerBlend.Type.LeftArm && target.BlendType != WindowPartsFingerBlend.Type.LeftLeg);
		}
		target.ApplyPresetData(value, mirroring, true);
	}

	private void OnClickRightCopyToLeft()
	{
		FingerBlend.BaseFinger baseFingerClass = this.RightFingerBlend.GetBaseFingerClass(this.RightFingerBlend.mgr.select_maid, this.RightFingerBlend.BlendType);
		this.LeftFingerBlend.ApplyPresetData(Convert.ToBase64String(baseFingerClass.GetBinary()), true, true);
	}

	private void OnClickLeftCopyToLeft()
	{
		FingerBlend.BaseFinger baseFingerClass = this.LeftFingerBlend.GetBaseFingerClass(this.LeftFingerBlend.mgr.select_maid, this.LeftFingerBlend.BlendType);
		this.RightFingerBlend.ApplyPresetData(Convert.ToBase64String(baseFingerClass.GetBinary()), true, true);
	}

	private void OnClickSwap()
	{
		FingerBlend.BaseFinger baseFingerClass = this.RightFingerBlend.GetBaseFingerClass(this.RightFingerBlend.mgr.select_maid, this.RightFingerBlend.BlendType);
		string base64_text = Convert.ToBase64String(baseFingerClass.GetBinary());
		baseFingerClass = this.LeftFingerBlend.GetBaseFingerClass(this.LeftFingerBlend.mgr.select_maid, this.LeftFingerBlend.BlendType);
		string base64_text2 = Convert.ToBase64String(baseFingerClass.GetBinary());
		this.LeftFingerBlend.ApplyPresetData(base64_text, true, false);
		this.RightFingerBlend.ApplyPresetData(base64_text2, true, true);
	}

	private void OnClickPresetSave()
	{
		foreach (WindowPartsFingerPreset.PresetSaveData presetSaveData in this.PresetSaveBtnDataArray)
		{
			if (presetSaveData.btn == UIButton.current)
			{
				this.PresetSave(presetSaveData.window_finger_blend);
				break;
			}
		}
	}

	private void PresetSave(WindowPartsFingerBlend tareget_blend)
	{
		WindowPartsFingerBlend.Type blendType = tareget_blend.BlendType;
		FingerBlend.BaseFinger baseFingerClass = tareget_blend.GetBaseFingerClass(tareget_blend.mgr.select_maid, blendType);
		XElement xelement = new XElement("GameVersion", 1170);
		XElement xelement2 = new XElement("RightData", blendType == WindowPartsFingerBlend.Type.RightArm || blendType == WindowPartsFingerBlend.Type.RightLeg);
		XElement xelement3 = new XElement("BinaryData", Convert.ToBase64String(baseFingerClass.GetBinary()));
		XNode[] contents = new XNode[]
		{
			xelement,
			xelement2,
			xelement3
		};
		XElement xelement4 = new XElement("FingerData", contents);
		XDocument save_target_xml = new XDocument(new XDeclaration("1.0", "utf-8", "true"), new object[]
		{
			new XComment("CM3D2 FingerData"),
			xelement4
		});
		string save_dir_name = "arm";
		if (blendType == WindowPartsFingerBlend.Type.RightLeg || blendType == WindowPartsFingerBlend.Type.LeftLeg)
		{
			save_dir_name = "leg";
		}
		this.PoseEditWindow.PanelSave.OpenFingerSaveMode(save_target_xml, save_dir_name, null);
	}

	private void OnBtnMouseHoverIn(string text)
	{
		MouseExposition @object = MouseExposition.GetObject();
		@object.text = text;
	}

	private void OnMouseHoverOut()
	{
		MouseExposition @object = MouseExposition.GetObject();
		@object.text = string.Empty;
	}

	private string GetFolderPath(WindowPartsFingerBlend.Type type)
	{
		if (!Directory.Exists(PhotoWindowManager.path_photo_folder + "FingerData"))
		{
			Directory.CreateDirectory(PhotoWindowManager.path_photo_folder + "FingerData");
		}
		string str = "arm";
		if (type == WindowPartsFingerBlend.Type.RightLeg || type == WindowPartsFingerBlend.Type.LeftLeg)
		{
			str = "leg";
		}
		string text = PhotoWindowManager.path_photo_folder + "FingerData/" + str;
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		return text;
	}

	private string GetFolderPath(WindowPartsFingerPreset.PresetLoadData.Type type)
	{
		return this.GetFolderPath((type != WindowPartsFingerPreset.PresetLoadData.Type.Arm) ? WindowPartsFingerBlend.Type.RightLeg : WindowPartsFingerBlend.Type.RightArm);
	}

	public PoseEditWindow PoseEditWindow;

	public WindowPartsFingerPreset.PresetSaveData[] PresetSaveBtnDataArray;

	public WindowPartsFingerPreset.PresetLoadData PresetLoad;

	public GameObject[] HideObjectArray;

	public WindowPartsFingerBlend RightFingerBlend;

	public WindowPartsFingerBlend LeftFingerBlend;

	public UIButton RightCopyToLeft;

	public UIButton LeftCopyToRight;

	public UIButton Swap;

	[Serializable]
	public class PresetSaveData
	{
		public WindowPartsFingerBlend window_finger_blend;

		public UIButton btn;
	}

	[Serializable]
	public class PresetLoadData
	{
		public UIGrid Grid
		{
			get
			{
				if (this.grid_ == null)
				{
					this.grid_ = this.PresetList.GetComponentInChildren<UIGrid>();
				}
				return this.grid_;
			}
		}

		public WindowPartsFingerPreset.PresetLoadData.Type type;

		public WFCheckBox CheckBoxPresetVisibleSwitch;

		public WindowPartsFingerBlend right_blend;

		public WindowPartsFingerBlend left_blend;

		public GameObject PresetList;

		private UIGrid grid_;

		public enum Type
		{
			Arm,
			Leg
		}
	}
}
