using System;
using System.Collections.Generic;
using System.IO;
using wf;
using Yotogis;

namespace MaidStatus
{
	public class YotogiSkillSystem
	{
		public YotogiSkillSystem(Status status)
		{
			this.status = status;
			this.skillDatas_ = new SortedDictionary<int, YotogiSkillData>();
			this.datas = new ReadOnlySortedDictionary<int, YotogiSkillData>(this.skillDatas_);
			this.oldSkillDatas_ = new SortedDictionary<int, YotogiSkillData>();
			this.oldDatas = new ReadOnlySortedDictionary<int, YotogiSkillData>(this.oldSkillDatas_);
		}

		public ReadOnlySortedDictionary<int, YotogiSkillData> datas { get; protected set; }

		public ReadOnlySortedDictionary<int, YotogiSkillData> oldDatas { get; protected set; }

		public Status status { get; protected set; }

		public YotogiSkillData Add(int skillId)
		{
			if (Skill.Contains(skillId))
			{
				return this.Add(Skill.Get(skillId));
			}
			if (Skill.Old.Contains(skillId))
			{
				return this.Add(Skill.Old.Get(skillId));
			}
			return null;
		}

		public YotogiSkillData Add(Skill.Old.Data data)
		{
			if (data == null)
			{
				return null;
			}
			YotogiSkillData yotogiSkillData = null;
			if (!this.oldSkillDatas_.ContainsKey(data.id))
			{
				if (data.newData == null)
				{
					yotogiSkillData = new YotogiSkillData();
					yotogiSkillData.oldData = data;
					yotogiSkillData.expSystem.SetExreienceList(new List<int>(data.skill_exp_table));
					this.oldSkillDatas_.Add(data.id, yotogiSkillData);
				}
				else
				{
					yotogiSkillData = this.Add(data.newData);
				}
			}
			return yotogiSkillData;
		}

		public YotogiSkillData Add(Skill.Data data)
		{
			if (data == null)
			{
				return null;
			}
			YotogiSkillData yotogiSkillData = null;
			if (!this.skillDatas_.ContainsKey(data.id))
			{
				yotogiSkillData = new YotogiSkillData();
				yotogiSkillData.data = data;
				yotogiSkillData.oldData = data.oldData;
				yotogiSkillData.expSystem.SetExreienceList(new List<int>(data.skill_exp_table));
				this.skillDatas_.Add(data.id, yotogiSkillData);
				if (yotogiSkillData.oldData != null)
				{
					this.oldSkillDatas_.Add(data.id, yotogiSkillData);
				}
			}
			return yotogiSkillData;
		}

		public void Remove(Skill.Data data)
		{
			if (data != null)
			{
				this.Remove(data.id);
			}
		}

		public void Remove(Skill.Old.Data data)
		{
			if (data != null)
			{
				this.Remove(data.id);
			}
		}

		public void Remove(int skillId)
		{
			if (this.skillDatas_.ContainsKey(skillId))
			{
				this.skillDatas_.Remove(skillId);
			}
			if (this.oldSkillDatas_.ContainsKey(skillId))
			{
				this.oldSkillDatas_.Remove(skillId);
			}
		}

		public void Clear()
		{
			this.skillDatas_.Clear();
			this.oldSkillDatas_.Clear();
		}

		public bool Contains(int skillId)
		{
			return this.datas.ContainsKey(skillId) || (this.oldDatas != null && this.oldDatas.ContainsKey(skillId));
		}

		public YotogiSkillData Get(int skillId)
		{
			return (!this.datas.ContainsKey(skillId)) ? ((this.oldDatas == null || !this.oldDatas.ContainsKey(skillId)) ? null : this.oldDatas.Get(skillId)) : this.datas.Get(skillId);
		}

		public void Serialize(BinaryWriter binary)
		{
			binary.Write((short)this.skillDatas_.Count);
			foreach (KeyValuePair<int, YotogiSkillData> keyValuePair in this.skillDatas_)
			{
				binary.Write(keyValuePair.Key);
				keyValuePair.Value.Serialize(binary);
			}
			binary.Write((short)this.oldSkillDatas_.Count);
			foreach (KeyValuePair<int, YotogiSkillData> keyValuePair2 in this.oldSkillDatas_)
			{
				binary.Write(keyValuePair2.Key);
				bool flag = !this.skillDatas_.ContainsKey(keyValuePair2.Key);
				binary.Write(flag);
				if (flag)
				{
					keyValuePair2.Value.Serialize(binary);
				}
			}
		}

		public void Deserialize(BinaryReader binary, int version)
		{
			Func<int, YotogiSkillData> func = delegate(int skillId)
			{
				YotogiSkillData yotogiSkillData3 = new YotogiSkillData();
				yotogiSkillData3.data = Skill.Get(skillId);
				yotogiSkillData3.oldData = Skill.Old.Get(skillId);
				if (yotogiSkillData3.data != null)
				{
					yotogiSkillData3.expSystem.SetExreienceList(new List<int>(yotogiSkillData3.data.skill_exp_table));
				}
				else if (yotogiSkillData3.oldData != null)
				{
					yotogiSkillData3.expSystem.SetExreienceList(new List<int>(yotogiSkillData3.oldData.skill_exp_table));
				}
				return yotogiSkillData3;
			};
			this.skillDatas_.Clear();
			int num = (int)binary.ReadInt16();
			for (int i = 0; i < num; i++)
			{
				int num2 = binary.ReadInt32();
				YotogiSkillData yotogiSkillData = func(num2);
				yotogiSkillData.Deserialize(binary, version);
				if (yotogiSkillData.data != null || yotogiSkillData.oldData != null)
				{
					this.skillDatas_.Add(num2, yotogiSkillData);
				}
			}
			this.oldSkillDatas_.Clear();
			num = (int)binary.ReadInt16();
			for (int j = 0; j < num; j++)
			{
				int num3 = binary.ReadInt32();
				bool flag = binary.ReadBoolean();
				if (flag)
				{
					YotogiSkillData yotogiSkillData2 = func(num3);
					yotogiSkillData2.Deserialize(binary, version);
					if (yotogiSkillData2.data != null || yotogiSkillData2.oldData != null)
					{
						this.oldSkillDatas_.Add(num3, yotogiSkillData2);
					}
				}
				else if (this.skillDatas_.ContainsKey(num3) && (Skill.Contains(num3) || Skill.Old.Contains(num3)))
				{
					this.oldSkillDatas_.Add(num3, this.skillDatas_[num3]);
				}
			}
			Dictionary<int, YotogiSkillData> dictionary = new Dictionary<int, YotogiSkillData>();
			Dictionary<int, YotogiSkillData> dictionary2 = new Dictionary<int, YotogiSkillData>();
			Dictionary<int, YotogiSkillData> dictionary3 = new Dictionary<int, YotogiSkillData>();
			for (int k = 0; k < 2; k++)
			{
				SortedDictionary<int, YotogiSkillData> sortedDictionary = (k != 0) ? this.oldSkillDatas_ : this.skillDatas_;
				foreach (KeyValuePair<int, YotogiSkillData> keyValuePair in sortedDictionary)
				{
					YotogiSkillData value = keyValuePair.Value;
					int id = value.id;
					if (value.data != null && value.oldData == null && !dictionary2.ContainsKey(id))
					{
						dictionary2.Add(id, keyValuePair.Value);
					}
					else if (value.data == null && value.oldData != null && !dictionary3.ContainsKey(id))
					{
						dictionary3.Add(id, keyValuePair.Value);
					}
					else if (value.data != null && value.oldData != null && !dictionary.ContainsKey(id))
					{
						dictionary.Add(id, keyValuePair.Value);
					}
				}
			}
			this.skillDatas_.Clear();
			this.oldSkillDatas_.Clear();
			foreach (KeyValuePair<int, YotogiSkillData> keyValuePair2 in dictionary)
			{
				this.skillDatas_.Add(keyValuePair2.Key, keyValuePair2.Value);
				this.oldSkillDatas_.Add(keyValuePair2.Key, keyValuePair2.Value);
			}
			foreach (KeyValuePair<int, YotogiSkillData> keyValuePair3 in dictionary2)
			{
				this.skillDatas_.Add(keyValuePair3.Key, keyValuePair3.Value);
			}
			foreach (KeyValuePair<int, YotogiSkillData> keyValuePair4 in dictionary3)
			{
				this.oldSkillDatas_.Add(keyValuePair4.Key, keyValuePair4.Value);
			}
		}

		protected SortedDictionary<int, YotogiSkillData> skillDatas_;

		protected SortedDictionary<int, YotogiSkillData> oldSkillDatas_;
	}
}
