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

public class CasinoShopItem
{
	public CasinoShopItem(CsvParser csv, int cy)
	{
		for (int i = 0; i < csv.max_cell_x; i++)
		{
			switch (i)
			{
			case 0:
				this.ID = csv.GetCellAsInteger(i, cy);
				break;
			case 1:
				this.Name = csv.GetCellAsString(i, cy);
				break;
			case 2:
				this.Price = csv.GetCellAsInteger(i, cy);
				break;
			case 3:
				this.MyCategory = ((!(csv.GetCellAsString(i, cy) == "衣装")) ? CasinoShopItem.Category.FacilityItem : CasinoShopItem.Category.Costume);
				break;
			case 4:
			{
				string text = csv.GetCellAsString(i, cy);
				if (text.IndexOf(".tex") < 0)
				{
					text += ".tex";
				}
				if (!GameUty.FileSystem.IsExistentFile(text))
				{
					Debug.LogError("アイコンファイル[" + text + "]が見つかりませんでした");
				}
				Texture2D texture2D = ImportCM.CreateTexture(text);
				this.Icon = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), Vector2.one * 0.5f);
				break;
			}
			case 5:
				this.Document = csv.GetCellAsString(i, cy);
				break;
			}
		}
		if (this.IsCategoryCostume)
		{
			this.ReadMenuFile();
		}
		else
		{
			this.ReadFacilityID();
		}
	}

	public CasinoShopItem()
	{
	}

	public bool IsSoldOut
	{
		get
		{
			return this.m_IsSoldOut;
		}
	}

	public bool IsCategoryCostume
	{
		get
		{
			return this.MyCategory == CasinoShopItem.Category.Costume;
		}
	}

	public bool IsCanBuy
	{
		get
		{
			return (long)this.Price <= GameMain.Instance.CharacterMgr.status.casinoCoin;
		}
	}

	public void ItemBuy()
	{
		this.m_IsSoldOut = true;
		CasinoShopItem.Category myCategory = this.MyCategory;
		if (myCategory != CasinoShopItem.Category.Costume)
		{
			if (myCategory == CasinoShopItem.Category.FacilityItem)
			{
				FacilityManager facilityMgr = GameMain.Instance.FacilityMgr;
				facilityMgr.SetHavePowerUpMaterial(this.m_FacilytyItemID, true);
			}
		}
		else
		{
			Status status = GameMain.Instance.CharacterMgr.status;
			foreach (List<CasinoShopItem.MenuData> list in this.m_WearDataList)
			{
				foreach (CasinoShopItem.MenuData menuData in list)
				{
					status.AddHavePartsItem(menuData.FileName);
				}
			}
		}
	}

	public void Serialize(BinaryWriter bw)
	{
		bw.Write(this.m_IsSoldOut);
	}

	public void Deserialize(BinaryReader br)
	{
		this.m_IsSoldOut = br.ReadBoolean();
	}

	public void Recet()
	{
		this.m_IsSoldOut = false;
	}

	private void ReadMenuFile()
	{
		Action<string[], List<CasinoShopItem.MenuData>> action = delegate(string[] file_array, List<CasinoShopItem.MenuData> list)
		{
			foreach (string text in file_array)
			{
				if (text.IndexOf(".menu") < 0)
				{
					this.ReadMenuDetail(text + ".menu", list);
				}
				else
				{
					this.ReadMenuDetail(text, list);
				}
			}
		};
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen("casinoshop_costume_data.nei"))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, "casinoshop_costume_data.nei]");
				for (int i = 1; i < csvParser.max_cell_y; i++)
				{
					if (csvParser.IsCellToExistData(0, i) && csvParser.GetCellAsInteger(0, i) == this.ID)
					{
						for (int j = 0; j < csvParser.max_cell_x; j++)
						{
							if (csvParser.IsCellToExistData(j, i))
							{
								switch (j)
								{
								case 0:
								case 1:
									break;
								case 2:
									this.m_WearMask = (TBody.MaskMode)Enum.Parse(typeof(TBody.MaskMode), csvParser.GetCellAsString(j, i));
									break;
								case 3:
								{
									string[] arg = csvParser.GetCellAsString(j, i).Split(new char[]
									{
										'\n'
									});
									action(arg, this.m_TrywearData);
									break;
								}
								default:
								{
									this.m_WearDataList.Add(new List<CasinoShopItem.MenuData>());
									string[] arg2 = csvParser.GetCellAsString(j, i).Split(new char[]
									{
										'\n'
									});
									action(arg2, this.m_WearDataList.Last<List<CasinoShopItem.MenuData>>());
									break;
								}
								}
							}
						}
						break;
					}
				}
			}
		}
	}

	private void ReadMenuDetail(string menu_file, List<CasinoShopItem.MenuData> menu_list)
	{
		byte[] buffer = null;
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen(menu_file))
		{
			NDebug.Assert(afileBase.IsValid(), menu_file + "\nnot file open.");
			buffer = afileBase.ReadAll();
		}
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer), Encoding.UTF8);
		string text = binaryReader.ReadString();
		NDebug.Assert(text == "CM3D2_MENU", "ProcScriptBin 例外 : ヘッダーファイルが不正です。" + text);
		int num = binaryReader.ReadInt32();
		string text2 = binaryReader.ReadString();
		string text3 = binaryReader.ReadString();
		string mpn = binaryReader.ReadString();
		binaryReader.Close();
		buffer = null;
		menu_list.Add(new CasinoShopItem.MenuData(mpn, menu_file));
	}

	public void TryWear(Maid maid)
	{
		if (!this.IsCategoryCostume)
		{
			return;
		}
		foreach (CasinoShopItem.MenuData menuData in this.m_TrywearData)
		{
			maid.SetProp(menuData.Mpn, menuData.FileName, 0, false, false);
		}
		maid.body0.SetMaskMode(this.m_WearMask);
	}

	private void ReadFacilityID()
	{
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen("casinoshop_facilityitem_data.nei"))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, "casinoshop_facility_data.nei]");
				for (int i = 1; i < csvParser.max_cell_y; i++)
				{
					if (csvParser.IsCellToExistData(0, i) && csvParser.GetCellAsInteger(0, i) == this.ID)
					{
						this.m_FacilytyItemID = csvParser.GetCellAsInteger(2, i);
						break;
					}
				}
			}
		}
	}

	public readonly int ID;

	public readonly string Name;

	public readonly int Price;

	public readonly Sprite Icon;

	public readonly string Document;

	public readonly CasinoShopItem.Category MyCategory;

	private bool m_IsSoldOut;

	private List<CasinoShopItem.MenuData> m_TrywearData = new List<CasinoShopItem.MenuData>();

	private TBody.MaskMode m_WearMask;

	private List<List<CasinoShopItem.MenuData>> m_WearDataList = new List<List<CasinoShopItem.MenuData>>();

	private int m_FacilytyItemID = -1;

	public enum Category
	{
		Costume,
		FacilityItem
	}

	private class MenuData
	{
		public MenuData(string mpn, string file_name)
		{
			this.Mpn = mpn;
			this.FileName = file_name;
		}

		public readonly string Mpn;

		public readonly string FileName;
	}
}
