using System;
using System.IO;
using System.Xml.Linq;
using UnityEngine;

public class PhotoModePoseSave : MonoBehaviour
{
	public static string folder_path
	{
		get
		{
			string text = PhotoWindowManager.path_photo_folder + "MyPose";
			if (!Directory.Exists(text))
			{
				Directory.CreateDirectory(text);
			}
			return text;
		}
	}

	public void Awake()
	{
		EventDelegate.Add(UTY.GetChildObject(base.gameObject, "Base/Ok", false).GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.OnBtnOk));
		EventDelegate.Add(UTY.GetChildObject(base.gameObject, "Base/Cancel", false).GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.OnBtnCancel));
	}

	public void Start()
	{
	}

	public void OpenFingerSaveMode(XDocument save_target_xml, string save_dir_name, Action success_call_back)
	{
		if (save_target_xml == null)
		{
			return;
		}
		this.mode_ = PhotoModePoseSave.Mode.Finger;
		this.save_target_binary_ = null;
		this.save_target_xml_ = save_target_xml;
		this.save_dir_name_ = save_dir_name;
		this.success_call_back_ = success_call_back;
		this.Open();
	}

	public void OpenPoseSaveMode(byte[] save_target_binary)
	{
		if (this.is_fade_processed || save_target_binary == null || save_target_binary.Length <= 0)
		{
			return;
		}
		this.mode_ = PhotoModePoseSave.Mode.Pose;
		this.save_target_xml_ = null;
		this.save_target_binary_ = save_target_binary;
		this.Open();
	}

	public void OnBtnOk()
	{
		this.InputPoseName.RemoveFocus();
		char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
		string file_name = this.InputPoseName.value;
		if (string.IsNullOrEmpty(file_name))
		{
			GameMain.Instance.SysDlg.Show("ポーズ名が記入されていません", SystemDialog.TYPE.OK, null, null);
			return;
		}
		if (0 <= file_name.IndexOfAny(invalidFileNameChars))
		{
			GameMain.Instance.SysDlg.Show("ポーズ名に使用できない文字が使われています", SystemDialog.TYPE.OK, null, null);
			return;
		}
		if (this.mode_ == PhotoModePoseSave.Mode.Pose)
		{
			file_name = this.mode_folder_path + "\\" + file_name + ".anm";
		}
		else
		{
			file_name = this.mode_folder_path + "\\" + file_name + ".xml";
		}
		if (250 < file_name.Length)
		{
			GameMain.Instance.SysDlg.Show("ポーズ名が長すぎます", SystemDialog.TYPE.OK, null, null);
			return;
		}
		if (File.Exists(file_name))
		{
			GameMain.Instance.SysDlg.Show("データが既に存在しています\n上書きしますか?", SystemDialog.TYPE.YES_NO, delegate
			{
				GameMain.Instance.SysDlg.Close();
				this.Save(file_name);
			}, null);
			return;
		}
		this.Save(file_name);
	}

	public void Save(string file_name)
	{
		if (this.mode_ == PhotoModePoseSave.Mode.Pose)
		{
			if (this.save_target_binary_ == null && this.save_target_binary_.Length <= 0)
			{
				return;
			}
		}
		else if (this.save_target_xml_ == null)
		{
			return;
		}
		bool flag = File.Exists(file_name);
		try
		{
			if (this.mode_ == PhotoModePoseSave.Mode.Pose)
			{
				File.WriteAllBytes(file_name, this.save_target_binary_);
			}
			else
			{
				this.save_target_xml_.Save(file_name);
			}
			MotionWindow motionWindow = this.PhotoMgr.GetWindow(PhotoWindowManager.WindowType.Motion) as MotionWindow;
			if (this.mode_ == PhotoModePoseSave.Mode.Pose)
			{
				if (!flag)
				{
					motionWindow.AddMyPose(file_name);
				}
				else
				{
					motionWindow.OnUpdateMyPose(file_name);
				}
			}
			if (this.success_call_back_ != null)
			{
				this.success_call_back_();
			}
		}
		catch
		{
			GameMain.Instance.SysDlg.Show("ポーズの保存に失敗しました", SystemDialog.TYPE.OK, null, null);
			return;
		}
		if (this.mode_ == PhotoModePoseSave.Mode.Pose)
		{
			GameMain.Instance.SysDlg.Show("ポーズを保存しました\nモーションウィンドウのマイポーズから選択できます", SystemDialog.TYPE.OK, delegate
			{
				GameMain.Instance.SysDlg.Close();
				this.OnBtnCancel();
			}, null);
		}
		else
		{
			GameMain.Instance.SysDlg.Show("ポーズを保存しました", SystemDialog.TYPE.OK, delegate
			{
				GameMain.Instance.SysDlg.Close();
				this.OnBtnCancel();
			}, null);
		}
	}

	public void OnBtnCancel()
	{
		this.InputPoseName.RemoveFocus();
		if (this.is_fade_processed)
		{
			return;
		}
		TweenAlpha tweenAlpha = base.gameObject.AddComponent<TweenAlpha>();
		tweenAlpha.from = 1f;
		tweenAlpha.to = 0f;
		tweenAlpha.duration = 0.3f;
		tweenAlpha.PlayForward();
		EventDelegate.Set(tweenAlpha.onFinished, delegate()
		{
			TweenAlpha component = base.gameObject.GetComponent<TweenAlpha>();
			if (component != null)
			{
				UnityEngine.Object.DestroyImmediate(component);
			}
			base.gameObject.SetActive(false);
		});
	}

	private void Open()
	{
		base.gameObject.SetActive(true);
		TweenAlpha tweenAlpha = base.gameObject.AddComponent<TweenAlpha>();
		tweenAlpha.from = 0f;
		tweenAlpha.to = 1f;
		tweenAlpha.duration = 0.3f;
		tweenAlpha.PlayForward();
		EventDelegate.Set(tweenAlpha.onFinished, delegate()
		{
			TweenAlpha component = base.gameObject.GetComponent<TweenAlpha>();
			if (component != null)
			{
				UnityEngine.Object.DestroyImmediate(component);
			}
		});
	}

	private void Update()
	{
	}

	private void OnChangeTitle(PhotSaveLoagDataUnit unit)
	{
	}

	public void OnClickBG()
	{
		if (this.is_fade_processed)
		{
			return;
		}
		if (UICamera.currentTouchID == -2)
		{
			this.OnBtnCancel();
		}
	}

	private bool is_fade_processed
	{
		get
		{
			TweenAlpha component = base.gameObject.GetComponent<TweenAlpha>();
			if (component != null && !component.enabled)
			{
				component.enabled = true;
			}
			return component != null;
		}
	}

	private string mode_folder_path
	{
		get
		{
			string text = string.Empty;
			if (this.mode_ == PhotoModePoseSave.Mode.Pose)
			{
				text = PhotoModePoseSave.folder_path;
			}
			else
			{
				if (!Directory.Exists(PhotoWindowManager.path_photo_folder + "FingerData"))
				{
					Directory.CreateDirectory(PhotoWindowManager.path_photo_folder + "FingerData");
				}
				text = PhotoWindowManager.path_photo_folder + "FingerData/" + this.save_dir_name_;
				if (!Directory.Exists(text))
				{
					Directory.CreateDirectory(text);
				}
			}
			return text;
		}
	}

	public PhotoWindowManager PhotoMgr;

	public UIInput InputPoseName;

	private PhotoModePoseSave.Mode mode_;

	private byte[] save_target_binary_;

	private string save_dir_name_;

	private XDocument save_target_xml_;

	private Action success_call_back_;

	private enum Mode
	{
		Pose,
		Finger
	}
}
