using System;
using System.IO;
using UnityEngine;

namespace Kasizuki
{
	public class BigThumbnailKasizuki : MonoBehaviour
	{
		public void Awake()
		{
			this.isEnabled = true;
			this.Init();
		}

		private void Init()
		{
			if (this.m_ThumbnailSprite == null)
			{
				this.CheckCreateFrame();
				this.m_ThumbnailSprite = UTY.GetChildObject(base.gameObject, "Image", false).GetComponent<UI2DSprite>();
				this.m_BGSprite = base.gameObject.GetComponent<UI2DSprite>();
				this.m_FrameSprite = UTY.GetChildObject(base.gameObject, "Frame", false).GetComponent<UI2DSprite>();
				this.alpha = 0f;
			}
		}

		public void OnDestroy()
		{
			if (this.m_ThumbnailSprite.sprite2D != null && this.m_ThumbnailSprite.sprite2D.texture != null)
			{
				UnityEngine.Object.DestroyImmediate(this.m_ThumbnailSprite.sprite2D.texture);
			}
			if (this.m_BGSprite.sprite2D != null && this.m_BGSprite.sprite2D.texture != null)
			{
				UnityEngine.Object.DestroyImmediate(this.m_BGSprite.sprite2D.texture);
			}
			if (this.m_FrameSprite.sprite2D != null && this.m_FrameSprite.sprite2D.texture != null)
			{
				UnityEngine.Object.DestroyImmediate(this.m_FrameSprite.sprite2D.texture);
			}
			this.m_ThumbnailSprite.sprite2D = null;
			this.m_BGSprite.sprite2D = null;
			this.m_FrameSprite.sprite2D = null;
			this.m_DrawTexture = null;
		}

		public void SetMaid(Maid maid)
		{
			if (!this.isEnabled)
			{
				return;
			}
			this.Init();
			this.alpha = 0f;
			if (this.m_ThumbnailSprite.sprite2D != null && this.m_ThumbnailSprite.sprite2D.texture != null)
			{
				UnityEngine.Object.DestroyImmediate(this.m_ThumbnailSprite.sprite2D.texture);
			}
			this.m_ThumbnailSprite.sprite2D = null;
			this.m_DrawTexture = null;
			if (maid == null)
			{
				return;
			}
			this.m_DrawTexture = KasizukiManager.GetMaidThumbnail(maid);
			if (this.m_DrawTexture == null)
			{
				return;
			}
			this.LoadFrameAndBG();
			Sprite sprite = Sprite.Create(this.m_DrawTexture, new Rect(0f, 0f, (float)this.m_DrawTexture.width, (float)this.m_DrawTexture.height), default(Vector2));
			sprite.name = maid.status.lastName + maid.status.firstName;
			this.m_ThumbnailSprite.sprite2D = sprite;
			this.m_ThumbnailSprite.SetDimensions(this.m_DrawTexture.width, this.m_DrawTexture.height);
			this.alpha = 1f;
		}

		public float alpha
		{
			get
			{
				this.Init();
				return this.m_BGSprite.alpha;
			}
			set
			{
				this.Init();
				this.m_BGSprite.alpha = value;
			}
		}

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

		public bool isEnabled { get; set; }

		private void CheckCreateFrame()
		{
			string fullPath = Path.GetFullPath(".\\");
			string text = fullPath + "Thumb";
			if (!Directory.Exists(text))
			{
				Directory.CreateDirectory(text);
			}
			string path = text + "/frame_single.png";
			if (!File.Exists(path))
			{
				Texture2D texture2D = Resources.Load<Texture2D>("CharacterSelect/Atlas/DefaultFrame");
				File.WriteAllBytes(path, texture2D.EncodeToPNG());
				Resources.UnloadAsset(texture2D);
			}
			path = text + "/frame_back.png";
			if (!File.Exists(path))
			{
				Texture2D texture2D2 = Resources.Load<Texture2D>("CharacterSelect/Atlas/DefaultFrameBack");
				File.WriteAllBytes(path, texture2D2.EncodeToPNG());
				Resources.UnloadAsset(texture2D2);
			}
		}

		private void LoadFrameAndBG()
		{
			this.CheckCreateFrame();
			string str = Path.GetFullPath(".\\") + "Thumb";
			string f_strFileName = str + "/frame_back.png";
			if (this.m_BGSprite.sprite2D == null)
			{
				Texture2D texture2D = UTY.LoadTexture(f_strFileName);
				if (texture2D != null)
				{
					int width = this.m_BGSprite.width;
					int height = this.m_BGSprite.height;
					Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)width, (float)height), default(Vector2));
					sprite.name = "frame_back";
					this.m_BGSprite.sprite2D = sprite;
					this.m_BGSprite.SetDimensions(width, height);
				}
			}
			if (this.m_FrameSprite.sprite2D == null)
			{
				f_strFileName = str + "/frame_single.png";
				Texture2D texture2D2 = UTY.LoadTexture(f_strFileName);
				if (texture2D2 != null)
				{
					int width2 = this.m_FrameSprite.width;
					int height2 = this.m_FrameSprite.height;
					Sprite sprite2 = Sprite.Create(texture2D2, new Rect(0f, 0f, (float)width2, (float)height2), default(Vector2));
					sprite2.name = "frame_single";
					this.m_FrameSprite.sprite2D = sprite2;
					this.m_FrameSprite.SetDimensions(width2, height2);
				}
			}
		}

		[SerializeField]
		private UI2DSprite m_ThumbnailSprite;

		[SerializeField]
		private UI2DSprite m_BGSprite;

		[SerializeField]
		private UI2DSprite m_FrameSprite;

		private Texture2D m_DrawTexture;
	}
}
