using System;
using MaidStatus;
using TextureBank;
using UnityEngine;

namespace Schedule
{
	public class ScheduleScene
	{
		public ScheduleScene()
		{
			this.textureBank = new TextureBank();
			this.slot = new Slot[40];
			for (int i = 0; i < this.slot.Length; i++)
			{
				this.slot[i] = new Slot(this, i);
			}
		}

		public void SetSlot_Safe(int slotId, Maid maid, bool slotUpdate = true, bool updateAll = true)
		{
			if (slotId < 0)
			{
				Debug.LogWarning("ScheduleScene.SetSlot_Safe:スロットIDに0未満を指定することはできません。");
				return;
			}
			if (slotId >= 40)
			{
				Debug.LogWarning("ScheduleScene.SetSlot_Safe:スロットIDにkScheduleSlotMax以上の値を指定することはできません。");
				return;
			}
			if (maid != null && slotUpdate)
			{
				if (this.slot[slotId].maid == maid)
				{
					Debug.LogWarning("ScheduleScene.SetSlot_Safe:そのスロットIDには既に指定したメイドがセットされています。");
					return;
				}
				for (int i = 0; i < 40; i++)
				{
					if (i != slotId)
					{
						if (this.slot[i].maid == maid)
						{
							this.SetSlot_Safe(i, this.slot[slotId].maid, false, true);
							break;
						}
					}
				}
			}
			this.slot[slotId].maid = maid;
			if (slotUpdate)
			{
				ScheduleAPI.MaidWorkIdErrorCheck(true);
				if (updateAll)
				{
					this.Update();
				}
				else
				{
					this.Update(slotId);
				}
			}
			if (!DailyMgr.IsLegacy)
			{
				GameMain.Instance.FacilityMgr.UpdateFacilityAssignedMaidData();
			}
		}

		public void SetNoonWorkSlot_Safe(ScheduleMgr.ScheduleTime workTime, int slotId, int workId)
		{
			if (slotId < 0)
			{
				Debug.LogWarning("ScheduleScene.SetNoonWorkSlot_Safe:スロットIDに0未満を指定することはできません。");
				return;
			}
			if (slotId >= 40)
			{
				Debug.LogWarning("ScheduleScene.SetNoonWorkSlot_Safe:スロットIDにkScheduleSlotMax以上の値を指定することはできません。");
				return;
			}
			if (this.slot[slotId].maid == null)
			{
				Debug.LogWarning("ScheduleScene.SetNoonWorkSlot_Safe:そのスロットIDにメイドは設定されていません。");
				return;
			}
			if (workId == 0)
			{
				Debug.LogWarning("ScheduleScene.SetNoonWorkSlot_Safe:noonWorkIdに0が指定されましたが、空にはできないので傾向選択します。");
				ScheduleAPI.NoonWorkRandom(this.slot[slotId].maid);
			}
			if (ScheduleCSVData.TrainingData.ContainsKey(workId))
			{
				ScheduleCSVData.TrainingType trainingType = ScheduleCSVData.TrainingData[workId].trainingType;
				if (trainingType != ScheduleCSVData.TrainingType.Trainee)
				{
					if (trainingType != ScheduleCSVData.TrainingType.Travel)
					{
						if (workTime == ScheduleMgr.ScheduleTime.DayTime)
						{
							this.slot[slotId].maid.status.noonWorkId = workId;
						}
						else if (workTime == ScheduleMgr.ScheduleTime.Night)
						{
							this.slot[slotId].maid.status.nightWorkId = workId;
						}
					}
					else
					{
						this.slot[slotId].maid.status.noonWorkId = workId;
						this.RepeatedNoonWorkDel(this.slot[slotId].maid);
						int nightEntertainWorkId = ScheduleAPI.GetNightEntertainWorkId();
						int nightRestWorkId = ScheduleAPI.GetNightRestWorkId();
						for (int i = 0; i < 40; i++)
						{
							if (this.slot[i].maid != null && ScheduleCSVData.AllData.ContainsKey(this.slot[i].nightWorkId) && !ScheduleAPI.CheckNoProblemTravel(this.slot[i].nightWorkId))
							{
								if (this.slot[i].maid.status.contract == Contract.Trainee)
								{
									this.slot[i].maid.status.nightWorkId = nightRestWorkId;
								}
								else
								{
									this.slot[i].maid.status.nightWorkId = nightEntertainWorkId;
								}
							}
						}
						Debug.Log("ScheduleScene.SetNoonWorkSlot_Safe:旅行がセットされたので夜のスロットも変更します。");
						this.slot[slotId].maid.status.nightWorkId = ScheduleAPI.GetNightTravelWorkId();
						this.RepeatedNightWorkDel(this.slot[slotId].maid);
					}
				}
				else
				{
					this.slot[slotId].maid.status.noonWorkId = workId;
					this.RepeatedNoonWorkDel(this.slot[slotId].maid);
					Debug.Log("ScheduleScene.SetNoonWorkSlot_Safe:メイド研修（研修生）がセットされたので夜のスロットも変更します。");
					this.slot[slotId].maid.status.nightWorkId = ScheduleAPI.GetNightTraineeWorkId();
					this.RepeatedNightWorkDel(this.slot[slotId].maid);
					Debug.Log("ScheduleScene.SetNoonWorkSlot_Safe:メイド研修（研修生）がセットされたのでメイド長のスロットを変更します。");
					for (int j = 0; j < 40; j++)
					{
						if (this.slot[j].maid != null && this.slot[j].maid.status.leader)
						{
							this.slot[j].maid.status.noonWorkId = ScheduleAPI.GetNoonTrainerWorkId();
							this.RepeatedNoonWorkDel(this.slot[j].maid);
							this.slot[j].maid.status.nightWorkId = ScheduleAPI.GetNightTrainerWorkId();
							this.RepeatedNightWorkDel(this.slot[j].maid);
							break;
						}
					}
				}
			}
			else if (workTime == ScheduleMgr.ScheduleTime.DayTime)
			{
				this.slot[slotId].maid.status.noonWorkId = workId;
			}
			else if (workTime == ScheduleMgr.ScheduleTime.Night)
			{
				this.slot[slotId].maid.status.nightWorkId = workId;
			}
		}

		public void SetCommuSlot_Safe(ScheduleMgr.ScheduleTime workTime, int slotId, bool value)
		{
			if (workTime == ScheduleMgr.ScheduleTime.DayTime)
			{
				this.slot[slotId].maid.status.noonCommu = value;
			}
			else if (workTime == ScheduleMgr.ScheduleTime.Night)
			{
				this.slot[slotId].maid.status.nightCommu = value;
			}
		}

		public void SetNightWorkSlot_Safe(ScheduleMgr.ScheduleTime workTime, int slotId, int workId)
		{
			if (slotId < 0)
			{
				Debug.LogWarning("ScheduleScene.SetNightnWorkSlot_Safe:スロットIDに0未満を指定することはできません。");
				return;
			}
			if (slotId >= 40)
			{
				Debug.LogWarning("ScheduleScene.SetNightnWorkSlot_Safe:スロットIDにkScheduleSlotMax以上の値を指定することはできません。");
				return;
			}
			if (this.slot[slotId].maid == null)
			{
				Debug.LogWarning("ScheduleScene.SetNightnWorkSlot_Safe:そのスロットIDにメイドは設定されていません。");
				return;
			}
			if (workId == 0)
			{
				Debug.LogWarning("ScheduleScene.SetNightnWorkSlot_Safe:nightWorkIdに0が指定されましたが、空にはできないので傾向選択します。");
				ScheduleAPI.NightWorkRandom(this.slot[slotId].maid);
			}
			ScheduleCSVData.ScheduleBase scheduleBase = ScheduleCSVData.AllData[workId];
			ScheduleCSVData.Yotogi yotogi = (ScheduleCSVData.Yotogi)scheduleBase;
			if (workTime == ScheduleMgr.ScheduleTime.DayTime)
			{
				this.slot[slotId].maid.status.noonWorkId = workId;
			}
			else if (workTime == ScheduleMgr.ScheduleTime.Night)
			{
				this.slot[slotId].maid.status.nightWorkId = workId;
			}
			ScheduleCSVData.YotogiType yotogiType = yotogi.yotogiType;
			if (yotogiType == ScheduleCSVData.YotogiType.Vip || yotogiType == ScheduleCSVData.YotogiType.VipCall)
			{
				this.RepeatedWorkDel(workTime, this.slot[slotId].maid, workId);
			}
		}

		private void SetNoonWorkId(Maid maid, int setId)
		{
			maid.status.SetFlag("Schedule.ScheduleScene.NoonWorkId", setId);
		}

		private void SetNightWorkId(Maid maid, int setId)
		{
			maid.status.SetFlag("Schedule.ScheduleScene.NightWorkId", setId);
		}

		private void RepeatedWorkDel(ScheduleMgr.ScheduleTime workTime, Maid baseMaid, int workId)
		{
			int stockMaidCount = GameMain.Instance.CharacterMgr.GetStockMaidCount();
			for (int i = 0; i < stockMaidCount; i++)
			{
				Maid stockMaid = GameMain.Instance.CharacterMgr.GetStockMaid(i);
				if (stockMaid == baseMaid)
				{
					if (workTime == ScheduleMgr.ScheduleTime.DayTime)
					{
						if (stockMaid.status.nightWorkId == workId)
						{
							stockMaid.status.nightWorkId = 0;
						}
					}
					else if (workTime == ScheduleMgr.ScheduleTime.Night && stockMaid.status.noonWorkId == workId)
					{
						stockMaid.status.noonWorkId = 0;
					}
				}
				else if (stockMaid.status.noonWorkId == workId)
				{
					stockMaid.status.noonWorkId = 0;
				}
				else if (stockMaid.status.nightWorkId == workId)
				{
					stockMaid.status.nightWorkId = 0;
				}
			}
		}

		private void RepeatedNoonWorkDel(Maid baseMaid)
		{
			int stockMaidCount = GameMain.Instance.CharacterMgr.GetStockMaidCount();
			for (int i = 0; i < stockMaidCount; i++)
			{
				Maid stockMaid = GameMain.Instance.CharacterMgr.GetStockMaid(i);
				if (!(stockMaid == baseMaid))
				{
					if (stockMaid.status.noonWorkId == baseMaid.status.noonWorkId)
					{
						stockMaid.status.noonWorkId = 0;
					}
				}
			}
		}

		private void RepeatedNightWorkDel(Maid baseMaid)
		{
			int stockMaidCount = GameMain.Instance.CharacterMgr.GetStockMaidCount();
			for (int i = 0; i < stockMaidCount; i++)
			{
				Maid stockMaid = GameMain.Instance.CharacterMgr.GetStockMaid(i);
				if (!(stockMaid == baseMaid))
				{
					if (stockMaid.status.nightWorkId == baseMaid.status.nightWorkId)
					{
						stockMaid.status.nightWorkId = 0;
					}
				}
			}
		}

		public void Update()
		{
			for (int i = 0; i < this.slot.Length; i++)
			{
				this.slot[i].Update();
			}
		}

		public void Update(int index)
		{
			this.slot[index].Update();
		}

		public void Dispace()
		{
			if (this.textureBank != null)
			{
				this.textureBank.Dispace();
			}
		}

		public Slot[] slot;

		public TextureBank textureBank;
	}
}
