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

public class YotogiStageUnit : MonoBehaviour
{
	public void Awake()
	{
		GameObject childObject = UTY.GetChildObject(base.gameObject, "Parent", false);
		this.thumbnail_sprite_ = UTY.GetChildObject(childObject, "Icon", false).GetComponent<UI2DSprite>();
		EventDelegate.Add(childObject.GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.UpdateBG));
		this.button_ = childObject.GetComponent<UIWFTabButton>();
		this.name_label_ = UTY.GetChildObject(childObject, "Name", false).GetComponent<UILabel>();
		this.star_parent_ = new GameObject[2];
		this.star_parent_[0] = UTY.GetChildObject(childObject, "StarGroup", false);
		this.star_parent_[1] = UTY.GetChildObject(childObject, "StarMarkGroup", false);
		if (GameUty.supportMultiLanguage)
		{
			foreach (GameObject gameObject in this.star_parent_)
			{
				Vector3 localPosition = gameObject.transform.localPosition;
				gameObject.transform.localPosition = new Vector3(40f, localPosition.y, localPosition.z);
				UIGrid component = gameObject.GetComponent<UIGrid>();
				component.cellWidth = 22f;
				component.Reposition();
			}
		}
		Transform transform = UTY.GetChildObject(childObject, "StarGroup", false).transform;
		for (int j = 0; j < transform.childCount; j++)
		{
			this.star_sprite_list_.Add(transform.GetChild(j).GetComponent<UISprite>());
		}
		this.is_change_bg_ = true;
	}

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

	public void SetOnSelectEvent(YotogiStageUnit.OnSelectEvent call_back)
	{
		this.on_select_event_ = call_back;
	}

	public void SetStageData(YotogiStage.Data stage_data, bool enabled, bool isDaytime)
	{
		this.stage_data_ = stage_data;
		this.is_daytime_ = isDaytime;
		if (enabled)
		{
			this.name_label_.text = this.stage_data_.drawName;
		}
		else
		{
			this.name_label_.text = "??????";
		}
		foreach (GameObject gameObject in this.star_parent_)
		{
			gameObject.SetActive(true);
		}
		for (int j = 0; j < this.star_sprite_list_.Count; j++)
		{
			if (j < this.stage_data_.drawClubGrade)
			{
				this.star_sprite_list_[j].enabled = true;
			}
			else
			{
				this.star_sprite_list_[j].enabled = false;
			}
		}
		int num = (!isDaytime) ? 1 : 0;
		string text = this.stage_data_.thumbnailName[num];
		text = ScriptManager.ReplacePersonal(GameMain.Instance.CharacterMgr.GetMaid(0), text);
		if (!enabled)
		{
			text = "cm3d2_stageselectpreview_nowprinting.tex";
		}
		AFileSystemBase afileSystemBase = (!GameUty.FileSystemOld.IsExistentFile(text)) ? GameUty.FileSystem : GameUty.FileSystemOld;
		if (!afileSystemBase.IsExistentFile(text))
		{
			text = "cm3d2_stageselectpreview_dammy.tex";
		}
		if (afileSystemBase.IsExistentFile(text))
		{
			Texture2D texture2D = ImportCM.CreateTexture(afileSystemBase, text);
			Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
			sprite.name = stage_data.drawName;
			if (this.thumbnail_sprite_.sprite2D != null && this.thumbnail_sprite_.sprite2D.texture != null)
			{
				UnityEngine.Object.DestroyImmediate(this.thumbnail_sprite_.sprite2D.texture);
			}
			this.thumbnail_sprite_.sprite2D = sprite;
			this.thumbnail_sprite_.SetDimensions(texture2D.width, texture2D.height);
		}
		this.button_.isEnabled = enabled;
	}

	public void UpdateBG()
	{
		if (this.stage_data_ == null)
		{
			return;
		}
		if (this.is_change_bg)
		{
			int num = (!GameMain.Instance.CharacterMgr.status.isDaytime) ? 1 : 0;
			GameMain.Instance.BgMgr.ChangeBg(this.stage_data_.prefabName[num]);
		}
		if (this.on_select_event_ != null)
		{
			this.on_select_event_(this);
		}
	}

	public YotogiStage.Data stage_data
	{
		get
		{
			return this.stage_data_;
		}
	}

	public bool is_daytime
	{
		get
		{
			return this.is_daytime_;
		}
	}

	public UIWFTabButton button
	{
		get
		{
			return this.button_;
		}
	}

	public bool is_change_bg
	{
		get
		{
			return this.is_change_bg_;
		}
		set
		{
			this.is_change_bg_ = value;
		}
	}

	public UI2DSprite thumbnail_sprite
	{
		get
		{
			return this.thumbnail_sprite_;
		}
	}

	private YotogiStage.Data stage_data_;

	private bool is_daytime_;

	private YotogiStageUnit.OnSelectEvent on_select_event_;

	private UILabel name_label_;

	private UI2DSprite thumbnail_sprite_;

	private List<UISprite> star_sprite_list_ = new List<UISprite>();

	private GameObject[] star_parent_;

	private UIWFTabButton button_;

	private bool is_change_bg_;

	public delegate void OnSelectEvent(YotogiStageUnit click_unit);
}
