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

public class CasinoDataMgr
{
	public static CasinoDataMgr Instance
	{
		get
		{
			return CasinoDataMgr.m_Instance;
		}
	}

	public Facility CurrentCasino
	{
		get
		{
			return this.m_SelectCasino;
		}
	}

	public Maid DealerMaid
	{
		get
		{
			return (!this.m_SelectCasino || !this.m_FacilityDealerPair.ContainsKey(this.m_SelectCasino)) ? null : this.m_FacilityDealerPair[this.m_SelectCasino].Maid;
		}
	}

	public int CasinoID
	{
		get
		{
			return FacilityDataTable.GetFacilityTypeID("カジノ");
		}
	}

	public int UpwardCasinoID
	{
		get
		{
			return FacilityDataTable.GetFacilityTypeID("高級カジノ");
		}
	}

	private void WriteDealerData(BinaryWriter bw, bool is_upward)
	{
		Facility casinoFacility = this.GetCasinoFacility(is_upward);
		bool flag = casinoFacility != null;
		string value = (!is_upward) ? "Low" : "High";
		bw.Write(value);
		if (flag)
		{
			if (!this.m_FacilityDealerPair.ContainsKey(casinoFacility))
			{
				this.m_FacilityDealerPair.Add(casinoFacility, new DealerMaid());
			}
			this.m_FacilityDealerPair[casinoFacility].Serialize(bw);
		}
	}

	private void ReadDealerData(BinaryReader br, bool is_upward)
	{
		string text = br.ReadString();
		bool condition = (!is_upward) ? (text == "Low") : (text == "High");
		string message = string.Format((!is_upward) ? "下位施設の情報を読み込もうとしましたが、文字「{0}」が検出されました" : "上位", text[0]);
		NDebug.Assert(condition, message);
		Facility casinoFacility = this.GetCasinoFacility(is_upward);
		bool flag = casinoFacility != null;
		if (flag)
		{
			this.m_FacilityDealerPair.Add(casinoFacility, new DealerMaid());
			this.m_FacilityDealerPair[casinoFacility].Deserialize(br);
		}
	}

	public Facility GetCasinoFacility(bool is_upward = false)
	{
		Facility result = null;
		Facility[] array;
		bool flag = (!is_upward) ? GameMain.Instance.FacilityMgr.IsExistTypeFacility(this.CasinoID, out array) : GameMain.Instance.FacilityMgr.IsExistTypeFacility(this.UpwardCasinoID, out array);
		if (flag)
		{
			result = array[0];
		}
		return result;
	}

	public void InitDealerMaid()
	{
		this.m_FacilityDealerPair[this.m_SelectCasino].Init();
	}

	public void SetDealerMaid(Maid maid)
	{
		this.m_FacilityDealerPair[this.m_SelectCasino].SetDealerMaid(maid);
	}

	public void Serialize(BinaryWriter bw)
	{
		bw.Write("COM3D2_CASINO");
		bw.Write("Ver:" + 1.1f);
		this.WriteDealerData(bw, false);
		this.WriteDealerData(bw, true);
		bw.Write("COM3D2_CASINOSHOP_ID");
		foreach (CasinoShopItem casinoShopItem in this.m_CasinoShopData.Values)
		{
			bw.Write(casinoShopItem.ID.ToString());
		}
		bw.Write("COM3D2_CASINOSHOP_ID_END");
		foreach (CasinoShopItem casinoShopItem2 in this.m_CasinoShopData.Values)
		{
			casinoShopItem2.Serialize(bw);
		}
	}

	public void Deserialize(BinaryReader br, int save_version)
	{
		this.m_FacilityDealerPair.Clear();
		foreach (CasinoShopItem casinoShopItem in this.m_CasinoShopData.Values)
		{
			casinoShopItem.Recet();
		}
		if (save_version < 210)
		{
			return;
		}
		string text = br.ReadString();
		float num = float.Parse(br.ReadString().Split(new char[]
		{
			':'
		})[1]);
		this.ReadDealerData(br, false);
		this.ReadDealerData(br, true);
		if (num >= 1.1f)
		{
			List<int> list = new List<int>();
			string text2 = br.ReadString();
			string text3 = br.ReadString();
			while (text3 != "COM3D2_CASINOSHOP_ID_END")
			{
				list.Add(int.Parse(text3));
				text3 = br.ReadString();
			}
			foreach (int key in list)
			{
				if (this.m_CasinoShopData.ContainsKey(key))
				{
					this.m_CasinoShopData[key].Deserialize(br);
				}
				else
				{
					CasinoShopItem casinoShopItem2 = new CasinoShopItem();
					casinoShopItem2.Deserialize(br);
				}
			}
		}
		else
		{
			foreach (CasinoShopItem casinoShopItem3 in this.m_CasinoShopData.Values)
			{
				casinoShopItem3.Deserialize(br);
			}
		}
	}

	public void CasinoSelect(bool is_upward)
	{
		this.m_SelectCasino = this.GetCasinoFacility(is_upward);
		if (this.m_SelectCasino == null)
		{
			Debug.LogError("該当するカジノ施設が見つかりませんでした");
		}
		else if (!this.m_FacilityDealerPair.ContainsKey(this.m_SelectCasino))
		{
			this.m_FacilityDealerPair.Add(this.m_SelectCasino, new DealerMaid());
		}
	}

	public void InitShopData()
	{
		if (this.m_CasinoShopData.Count > 0)
		{
			return;
		}
		HashSet<int> enabled_id_list = new HashSet<int>();
		Action<string> action = delegate(string file_name)
		{
			file_name += ".nei";
			if (!GameUty.FileSystem.IsExistentFile(file_name))
			{
				return;
			}
			using (AFileBase afileBase2 = GameUty.FileSystem.FileOpen(file_name))
			{
				using (CsvParser csvParser2 = new CsvParser())
				{
					bool condition2 = csvParser2.Open(afileBase2);
					NDebug.Assert(condition2, file_name + "\nopen failed.");
					for (int k = 1; k < csvParser2.max_cell_y; k++)
					{
						if (csvParser2.IsCellToExistData(0, k))
						{
							int cellAsInteger2 = csvParser2.GetCellAsInteger(0, k);
							if (!enabled_id_list.Contains(cellAsInteger2))
							{
								enabled_id_list.Add(cellAsInteger2);
							}
						}
					}
				}
			}
		};
		action("casinoshop_enable_id");
		for (int i = 0; i < GameUty.PathList.Count; i++)
		{
			action("casinoshop_enable_id_" + GameUty.PathList[i]);
		}
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen("casinoshop_data.nei"))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, "casinoshop_data.nei]");
				for (int j = 1; j < csvParser.max_cell_y; j++)
				{
					if (csvParser.IsCellToExistData(0, j))
					{
						int cellAsInteger = csvParser.GetCellAsInteger(0, j);
						if (enabled_id_list.Contains(cellAsInteger))
						{
							if (!this.m_CasinoShopData.ContainsKey(cellAsInteger))
							{
								this.m_CasinoShopData.Add(cellAsInteger, new CasinoShopItem(csvParser, j));
							}
							CasinoShopItem casinoShopItem = this.m_CasinoShopData[cellAsInteger];
							if (!this.m_CategoryItemData.ContainsKey(casinoShopItem.MyCategory))
							{
								this.m_CategoryItemData.Add(casinoShopItem.MyCategory, new List<CasinoShopItem>());
							}
							if (!this.m_CategoryItemData[casinoShopItem.MyCategory].Contains(casinoShopItem))
							{
								this.m_CategoryItemData[casinoShopItem.MyCategory].Add(casinoShopItem);
							}
						}
					}
				}
			}
		}
	}

	public List<CasinoShopItem> GetShopItem()
	{
		return this.m_CasinoShopData.Values.ToList<CasinoShopItem>();
	}

	public List<CasinoShopItem> GetShopItem(CasinoShopItem.Category category)
	{
		return this.m_CategoryItemData[category];
	}

	public void AddTotalCoin(int add_value)
	{
		if (add_value < 0)
		{
			return;
		}
		GameMain.Instance.CharacterMgr.status.AddFlag("コイン入手総額", add_value);
		if (GameMain.Instance.CharacterMgr.status.GetFlag("コイン入手総額") >= 10000)
		{
			GameMain.Instance.CharacterMgr.status.AddHaveTrophy(1120);
		}
	}

	private const int m_CheckMiscVer = 210;

	private const string m_SaveHeader = "COM3D2_CASINO";

	private const float m_SaveVer = 1.1f;

	private const string m_FacilityName = "カジノ";

	private const string m_UpwardFacilityName = "高級カジノ";

	public const int GetTrophyCoin = 10000;

	private static CasinoDataMgr m_Instance = new CasinoDataMgr();

	private Dictionary<Facility, DealerMaid> m_FacilityDealerPair = new Dictionary<Facility, DealerMaid>();

	private Facility m_SelectCasino;

	public bool IsChangeCostume;

	private Dictionary<int, CasinoShopItem> m_CasinoShopData = new Dictionary<int, CasinoShopItem>();

	private Dictionary<CasinoShopItem.Category, List<CasinoShopItem>> m_CategoryItemData = new Dictionary<CasinoShopItem.Category, List<CasinoShopItem>>();
}
