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

public class RandomPresetContent
{
	public static Dictionary<string, RandomPresetContent.CSVData> dicCsvData { get; set; }

	public static Dictionary<MPN, RandomPresetContent.MPNMapping> m_dicMPNMapping
	{
		get
		{
			if (RandomPresetContent.m_dicMPNMapping_ == null)
			{
				Dictionary<MPN, SceneEditInfo.CCateNameType> dicPartsTypePair = SceneEditInfo.m_dicPartsTypePair;
				string text = "random_preset_mpn_map.nei";
				RandomPresetContent.m_dicMPNMapping_ = new Dictionary<MPN, RandomPresetContent.MPNMapping>();
				using (AFileBase afileBase = GameUty.FileSystem.FileOpen(text))
				{
					using (CsvParser csvParser = new CsvParser())
					{
						bool condition = csvParser.Open(afileBase);
						NDebug.Assert(condition, text + "\nopen failed.");
						for (int i = 1; i < csvParser.max_cell_y; i++)
						{
							if (csvParser.IsCellToExistData(0, i))
							{
								RandomPresetContent.MPNMapping mpnmapping = new RandomPresetContent.MPNMapping();
								int num = 0;
								MPN key = MPN.null_mpn;
								try
								{
									key = (MPN)Enum.Parse(typeof(MPN), csvParser.GetCellAsString(num++, i));
								}
								catch (Exception e)
								{
									NDebug.AssertParseError("MPN", e);
								}
								mpnmapping.m_buttonName = csvParser.GetCellAsString(num++, i);
								SceneEditInfo.CCateNameType ccateNameType;
								if (dicPartsTypePair.TryGetValue(key, out ccateNameType))
								{
									mpnmapping.m_buttonTitle = ccateNameType.m_strBtnPartsTypeName;
									if (RandomPresetContent.m_dicMPNMapping_.ContainsKey(key))
									{
										NDebug.Assert(text + "\n" + key.ToString() + "が重複しています", false);
									}
									RandomPresetContent.m_dicMPNMapping_.Add(key, mpnmapping);
								}
							}
						}
					}
				}
			}
			return RandomPresetContent.m_dicMPNMapping_;
		}
	}

	public void InitRandomPresetParamter()
	{
		if (!this.LoadParameterSheet())
		{
			return;
		}
		this.InitExecutionOrder();
		this.InitRandomBaseGroup();
		this.InitBodyBase();
		this.InitRandomMapping();
		this.InitExceptionPartMapping();
		this.InitPriorityPartMapping();
		this.InitRatioToPutOnItem();
	}

	private bool LoadParameterSheet()
	{
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen("random_preset_parameter_define.nei"))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				if (!csvParser.Open(afileBase))
				{
					Debug.LogWarning("csv open failed.\nrandom_preset_parameter_define.nei");
					return false;
				}
				RandomPresetContent.dicCsvData = new Dictionary<string, RandomPresetContent.CSVData>();
				this.CreateObjectFromCsv(csvParser);
			}
		}
		return true;
	}

	private void CreateObjectFromCsv(CsvParser csv)
	{
		int max_cell_y = csv.max_cell_y;
		for (int i = 0; i < max_cell_y; i++)
		{
			string cellAsString = csv.GetCellAsString(0, i);
			if (cellAsString == "*")
			{
				this.csvData = new RandomPresetContent.CSVData();
				string cellAsString2 = csv.GetCellAsString(1, i);
				this.csvData.ClassName = cellAsString2;
				List<string> list = new List<string>();
				List<int> list2 = new List<int>();
				int j;
				for (j = 0; j < csv.max_cell_x; j++)
				{
					string cellAsString3 = csv.GetCellAsString(1 + j, i + 2);
					if (cellAsString3 == "-")
					{
						list2.Add(j);
					}
					else
					{
						if (string.IsNullOrEmpty(cellAsString3))
						{
							break;
						}
						list.Add(cellAsString3);
					}
				}
				this.csvData.Header = list;
				Dictionary<int, List<string>> dictionary = new Dictionary<int, List<string>>();
				bool flag = false;
				for (int k = 1; k < csv.max_cell_y; k++)
				{
					List<string> list3 = new List<string>();
					for (int l = 0; l < j; l++)
					{
						if (!list2.Contains(l))
						{
							string cellAsString4 = csv.GetCellAsString(1 + l, i + 2 + k);
							if (l == 0 && string.IsNullOrEmpty(cellAsString4))
							{
								flag = true;
								l = list.Count;
								break;
							}
							list3.Add(cellAsString4);
						}
					}
					if (flag)
					{
						break;
					}
					dictionary.Add(k, list3);
				}
				this.csvData.Rows = dictionary;
				RandomPresetContent.dicCsvData.Add(this.csvData.ClassName, this.csvData);
			}
		}
	}

	private void InitExecutionOrder()
	{
		RandomPresetContent.ExecutionOrderMasterTable executionOrderMasterTable = new RandomPresetContent.ExecutionOrderMasterTable();
		executionOrderMasterTable.Load();
		foreach (RandomPresetContent.ExecutionOrder executionOrder in executionOrderMasterTable.All)
		{
			if (RandomPresetContent.m_dicMPNMapping.ContainsKey(executionOrder.category))
			{
				RandomPresetContent.m_dicMPNMapping[executionOrder.category].m_executionOrder = executionOrder.order;
			}
		}
	}

	private void InitRandomBaseGroup()
	{
		RandomPresetContent.RandomBaseGroupMasterTable randomBaseGroupMasterTable = new RandomPresetContent.RandomBaseGroupMasterTable();
		randomBaseGroupMasterTable.Load();
		RandomPresetContent.m_dicRandomBaseGroup = new Dictionary<int, RandomPresetContent.RandomBaseGroup>();
		foreach (RandomPresetContent.RandomBaseGroup randomBaseGroup in randomBaseGroupMasterTable.All)
		{
			RandomPresetContent.m_dicRandomBaseGroup.Add(randomBaseGroup.randomBaseGroupId, randomBaseGroup);
		}
	}

	private void InitBodyBase()
	{
		RandomPresetContent.BodyBaseMasterTable bodyBaseMasterTable = new RandomPresetContent.BodyBaseMasterTable();
		bodyBaseMasterTable.Load();
		RandomPresetContent.m_dicBodyBase = new Dictionary<int, RandomPresetContent.BodyBase>();
		foreach (RandomPresetContent.BodyBase bodyBase in bodyBaseMasterTable.All)
		{
			RandomPresetContent.m_dicBodyBase.Add(bodyBase.bodyBaseId, bodyBase);
		}
	}

	private void InitRandomMapping()
	{
		RandomPresetContent.RandomMappingMasterTable randomMappingMasterTable = new RandomPresetContent.RandomMappingMasterTable();
		randomMappingMasterTable.Load();
		RandomPresetContent.m_dicRandomMapping = new Dictionary<int, RandomPresetContent.RandomMapping>();
		foreach (RandomPresetContent.RandomMapping randomMapping in randomMappingMasterTable.All)
		{
			RandomPresetContent.m_dicRandomMapping.Add(randomMapping.randomMappingId, randomMapping);
		}
		foreach (KeyValuePair<int, RandomPresetContent.RandomMapping> keyValuePair in RandomPresetContent.m_dicRandomMapping)
		{
			RandomPresetContent.RandomBaseGroup randomBaseGroup;
			if (RandomPresetContent.m_dicRandomBaseGroup.TryGetValue(keyValuePair.Value.randomBaseGroupId, out randomBaseGroup))
			{
				RandomPresetContent.BodyBase bodyBase;
				if (RandomPresetContent.m_dicBodyBase.TryGetValue(keyValuePair.Value.bodyBaseId, out bodyBase))
				{
					keyValuePair.Value.randomBaseGroup = randomBaseGroup;
					keyValuePair.Value.bodyBase = bodyBase;
				}
			}
		}
	}

	private void InitExceptionPartMapping()
	{
		RandomPresetContent.ExceptionPartMappingMasterTable exceptionPartMappingMasterTable = new RandomPresetContent.ExceptionPartMappingMasterTable();
		exceptionPartMappingMasterTable.Load();
		RandomPresetContent.m_dicExceptionPartMapping = new Dictionary<int, RandomPresetContent.PartMapping>();
		foreach (RandomPresetContent.PartMapping partMapping in exceptionPartMappingMasterTable.All)
		{
			RandomPresetContent.m_dicExceptionPartMapping.Add(partMapping.partMappingId, partMapping);
		}
	}

	private void InitPriorityPartMapping()
	{
		RandomPresetContent.PriorityPartMappingMasterTable priorityPartMappingMasterTable = new RandomPresetContent.PriorityPartMappingMasterTable();
		priorityPartMappingMasterTable.Load();
		RandomPresetContent.m_dicPriorityPartMapping = new Dictionary<int, RandomPresetContent.PartMapping>();
		foreach (RandomPresetContent.PartMapping partMapping in priorityPartMappingMasterTable.All)
		{
			RandomPresetContent.m_dicPriorityPartMapping.Add(partMapping.partMappingId, partMapping);
		}
	}

	private void InitRatioToPutOnItem()
	{
		RandomPresetContent.RatioToPutOnItemMasterTable ratioToPutOnItemMasterTable = new RandomPresetContent.RatioToPutOnItemMasterTable();
		ratioToPutOnItemMasterTable.Load();
		foreach (RandomPresetContent.RatioToPutOnItem ratioToPutOnItem in ratioToPutOnItemMasterTable.All)
		{
			if (RandomPresetContent.m_dicMPNMapping.ContainsKey(ratioToPutOnItem.category))
			{
				RandomPresetContent.m_dicMPNMapping[ratioToPutOnItem.category].m_ratioToPutOn = ratioToPutOnItem.ratioToPutOn;
			}
		}
	}

	public static int GetRandomBaseGrpId(int maidPropValue)
	{
		bool flag = false;
		bool flag2 = false;
		int num = 0;
		if (maidPropValue < RandomPresetContent.m_dicRandomBaseGroup[1].lowerLimit)
		{
			return RandomPresetContent.m_dicRandomBaseGroup[1].randomBaseGroupId;
		}
		int count = RandomPresetContent.m_dicRandomBaseGroup.Count;
		if (maidPropValue > RandomPresetContent.m_dicRandomBaseGroup[count].upperLimit)
		{
			return RandomPresetContent.m_dicRandomBaseGroup[count].randomBaseGroupId;
		}
		foreach (KeyValuePair<int, RandomPresetContent.RandomBaseGroup> keyValuePair in RandomPresetContent.m_dicRandomBaseGroup)
		{
			if (maidPropValue >= keyValuePair.Value.lowerLimit)
			{
				flag = true;
			}
			if (maidPropValue <= keyValuePair.Value.upperLimit)
			{
				flag2 = true;
			}
			if (flag && flag2)
			{
				return keyValuePair.Value.randomBaseGroupId;
			}
			if (flag2)
			{
				int num2 = Math.Abs(num - maidPropValue);
				int num3 = Math.Abs(keyValuePair.Value.lowerLimit - maidPropValue);
				return (num2 <= num3) ? (keyValuePair.Value.randomBaseGroupId - 1) : keyValuePair.Value.randomBaseGroupId;
			}
			flag = false;
			flag2 = false;
			num = keyValuePair.Value.upperLimit;
		}
		return 1;
	}

	public static string GetButtonNameByMPN(MPN mpn)
	{
		RandomPresetContent.MPNMapping mpnmapping;
		return (!RandomPresetContent.m_dicMPNMapping.TryGetValue(mpn, out mpnmapping)) ? null : mpnmapping.m_buttonName;
	}

	private void MakeDummyData()
	{
		SceneEdit component = GameObject.Find("__SceneEdit__").GetComponent<SceneEdit>();
		List<SceneEdit.SCategory> categoryList = component.CategoryList;
		Texture2D texIcon = categoryList[1].m_listPartsType[0].m_listMenu[0].m_texIcon;
	}

	public static Dictionary<int, RandomPresetContent.RandomBaseGroup> m_dicRandomBaseGroup;

	public static Dictionary<int, RandomPresetContent.BodyBase> m_dicBodyBase;

	public static Dictionary<int, RandomPresetContent.RandomMapping> m_dicRandomMapping;

	public static Dictionary<int, RandomPresetContent.PartMapping> m_dicExceptionPartMapping;

	public static Dictionary<int, RandomPresetContent.PartMapping> m_dicPriorityPartMapping;

	private const string RANDOMPRESET_PARAMETE_CSV_NAME = "random_preset_parameter_define.nei";

	private const string RANDOMPRESET_MPNMAP_CSV_NAME = "random_preset_mpn_map.nei";

	private const string CLASS_MARKER = "*";

	private const string SKIP_COLUMN_MARKER = "-";

	private const int OFFSET_COLUMN = 1;

	private const int OFFSET_ROW = 2;

	private const int LOWEST_NUM = 1;

	private RandomPresetContent.CSVData csvData;

	public static List<MPN> m_listUnderwearsItemGroup = new List<MPN>
	{
		MPN.panz,
		MPN.bra
	};

	public static List<MPN> m_listClothesItemGroupn = new List<MPN>
	{
		MPN.wear,
		MPN.skirt,
		MPN.mizugi,
		MPN.stkg,
		MPN.shoes,
		MPN.headset,
		MPN.acchat,
		MPN.onepiece
	};

	public static Dictionary<SceneEditInfo.EMenuCategory, string> m_dicCategoryMapping = new Dictionary<SceneEditInfo.EMenuCategory, string>
	{
		{
			SceneEditInfo.EMenuCategory.頭,
			"Head"
		},
		{
			SceneEditInfo.EMenuCategory.髪,
			"Hair"
		},
		{
			SceneEditInfo.EMenuCategory.服装,
			"Cloth"
		},
		{
			SceneEditInfo.EMenuCategory.身体,
			"Body"
		},
		{
			SceneEditInfo.EMenuCategory.セット,
			"Package"
		},
		{
			SceneEditInfo.EMenuCategory.アクセサリ,
			"Accessory"
		}
	};

	public static Dictionary<MPN, MPN> m_dicColorFolderMapping = new Dictionary<MPN, MPN>
	{
		{
			MPN.mayu,
			MPN.folder_mayu
		},
		{
			MPN.eye,
			MPN.folder_eye
		},
		{
			MPN.haircolor,
			MPN.hairf
		},
		{
			MPN.skin,
			MPN.folder_skin
		},
		{
			MPN.underhair,
			MPN.folder_underhair
		},
		{
			MPN.chikubicolor,
			MPN.chikubi
		}
	};

	private static Dictionary<MPN, RandomPresetContent.MPNMapping> m_dicMPNMapping_;

	public class CSVData
	{
		public string ClassName { get; set; }

		public List<string> Header { get; set; }

		public Dictionary<int, List<string>> Rows { get; set; }
	}

	public class RandomBaseGroupMasterTable : RandomPresetContent.MasterTableBase<RandomPresetContent.RandomBaseGroup>
	{
		public void Load()
		{
			base.Load(RandomPresetContent.dicCsvData["RandomBaseGroup"]);
		}
	}

	public class BodyBaseMasterTable : RandomPresetContent.MasterTableBase<RandomPresetContent.BodyBase>
	{
		public void Load()
		{
			base.Load(RandomPresetContent.dicCsvData["BodyBase"]);
		}
	}

	public class RandomMappingMasterTable : RandomPresetContent.MasterTableBase<RandomPresetContent.RandomMapping>
	{
		public void Load()
		{
			base.Load(RandomPresetContent.dicCsvData["RandomMapping"]);
		}
	}

	public class ExceptionPartMappingMasterTable : RandomPresetContent.MasterTableBase<RandomPresetContent.PartMapping>
	{
		public void Load()
		{
			base.Load(RandomPresetContent.dicCsvData["ExceptionPartMapping"]);
		}
	}

	public class PriorityPartMappingMasterTable : RandomPresetContent.MasterTableBase<RandomPresetContent.PartMapping>
	{
		public void Load()
		{
			base.Load(RandomPresetContent.dicCsvData["PriorityPartMapping"]);
		}
	}

	public class ExecutionOrderMasterTable : RandomPresetContent.MasterTableBase<RandomPresetContent.ExecutionOrder>
	{
		public void Load()
		{
			base.Load(RandomPresetContent.dicCsvData["ExecutionOrder"]);
		}
	}

	public class RatioToPutOnItemMasterTable : RandomPresetContent.MasterTableBase<RandomPresetContent.RatioToPutOnItem>
	{
		public void Load()
		{
			base.Load(RandomPresetContent.dicCsvData["RatioToPutOnItem"]);
		}
	}

	public class MasterTableBase<T> where T : RandomPresetContent.MasterBase, new()
	{
		public List<T> All
		{
			get
			{
				return this.masters;
			}
		}

		public void Load(RandomPresetContent.CSVData csvData)
		{
			List<string> header = csvData.Header;
			this.masters = new List<T>();
			foreach (int key in csvData.Rows.Keys)
			{
				this.ParseLine(csvData.Rows[key], header);
			}
		}

		private void ParseLine(List<string> row, List<string> headerElements)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			for (int i = 0; i < headerElements.Count; i++)
			{
				dictionary.Add(headerElements[i], row[i]);
			}
			T item = Activator.CreateInstance<T>();
			item.Load(dictionary);
			this.masters.Add(item);
		}

		protected List<T> masters;
	}

	public class MasterBase
	{
		public void Load(Dictionary<string, string> param)
		{
			foreach (string key in param.Keys)
			{
				this.SetField(key, param[key]);
			}
		}

		private void SetField(string key, string value)
		{
			PropertyInfo property = base.GetType().GetProperty(key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property.PropertyType == typeof(int))
			{
				property.SetValue(this, int.Parse(value), null);
			}
			else if (property.PropertyType == typeof(string))
			{
				property.SetValue(this, value, null);
			}
			else if (property.PropertyType == typeof(float))
			{
				property.SetValue(this, float.Parse(value), null);
			}
			else if (property.PropertyType == typeof(MPN))
			{
				property.SetValue(this, (MPN)Enum.Parse(typeof(MPN), value), null);
			}
		}
	}

	public class SliderCommon : RandomPresetContent.MasterBase
	{
		public int lowerLimit { get; set; }

		public int upperLimit { get; set; }
	}

	public class RandomBaseGroup : RandomPresetContent.SliderCommon
	{
		public int randomBaseGroupId { get; set; }

		public override string ToString()
		{
			return string.Format("RandomBaseGroup [randomBaseGrpId={0}, lowerLimit={1}, upperLimit={2}]", this.randomBaseGroupId, base.lowerLimit, base.upperLimit);
		}
	}

	public class BodyBase : RandomPresetContent.SliderCommon
	{
		public int bodyBaseId { get; set; }

		public MPN bodyBaseCategory { get; set; }

		public override string ToString()
		{
			return string.Format("BodyBase [bodyBaseId={0}, bodyBaseCategory={1}, lowerLimit={2}, upperLimit={3}]", new object[]
			{
				this.bodyBaseId,
				this.bodyBaseCategory,
				base.lowerLimit,
				base.upperLimit
			});
		}
	}

	public class RandomMapping : RandomPresetContent.MasterBase
	{
		public int randomMappingId { get; set; }

		public int randomBaseGroupId { get; set; }

		public RandomPresetContent.RandomBaseGroup randomBaseGroup { get; set; }

		public int bodyBaseId { get; set; }

		public RandomPresetContent.BodyBase bodyBase { get; set; }

		public override string ToString()
		{
			return string.Format("RandomMapping [randomMappingId={0}, randomBaseGroupId={1}, randomBaseGroup={2}, bodyBaseId={3}, bodyBase={4}]", new object[]
			{
				this.randomMappingId,
				this.randomBaseGroupId,
				this.randomBaseGroup,
				this.bodyBaseId,
				this.bodyBase
			});
		}
	}

	public class PartMapping : RandomPresetContent.MasterBase
	{
		public int partMappingId { get; set; }

		public MPN category { get; set; }

		public string partId { get; set; }

		public MPN targetCategory { get; set; }

		public string targetPartId { get; set; }
	}

	public class ExecutionOrder : RandomPresetContent.MasterBase
	{
		public int executionOrderId { get; set; }

		public MPN category { get; set; }

		public int order { get; set; }
	}

	public class RatioToPutOnItem : RandomPresetContent.MasterBase
	{
		public int ratioToPutOnItemId { get; set; }

		public MPN category { get; set; }

		public int ratioToPutOn { get; set; }
	}

	public class MPNMapping
	{
		public string m_buttonName;

		public string m_buttonTitle;

		public int m_executionOrder;

		public int m_ratioToPutOn;
	}
}
