using System;
using System.Collections.Generic;
using Schedule;
using UnityEngine;

public class ScheduleFacillityPanelCtrl : MonoBehaviour
{
	public ScheduleTaskViewer TaskViewr
	{
		set
		{
			this.viewer = value;
		}
	}

	public ScheduleCSVData.Work WorkData
	{
		set
		{
			this.current_workData = value;
		}
	}

	public Maid SelectMaid
	{
		set
		{
			this.current_maid = value;
		}
	}

	public ScheduleMgr.ScheduleTime ScheduleTime
	{
		set
		{
			this.current_time = value;
		}
	}

	public WorkTaskUnit TaskUnit
	{
		set
		{
			this.taskUnit = value;
		}
	}

	private void Awake()
	{
		this.faciltyManagement = base.gameObject.GetComponent<SceneFacilityManagement>();
		this.faciltyManagement.enableSetUpCameraAndBG = false;
		this.parentFacility = UTY.GetChildObject(base.gameObject, "Window Facility Details/Parent Facility Button", false);
	}

	private void Update()
	{
		if (!this.init)
		{
			this.Init();
		}
	}

	public void Active(bool active)
	{
		base.gameObject.SetActive(active);
		if (active)
		{
			GameMain.Instance.FacilityMgr.UpdateFacilityAssignedMaidData();
			this.UpdateFacilityInfo();
			this.UpdateFacilityInfoInteractable();
			this.faciltyManagement.ToggleEvent_VisibleFacilityInfo(true);
		}
	}

	private void Init()
	{
		this.init = true;
		uGUIListViewer component = GameObject.Find("Window Facility Details").GetComponent<uGUIListViewer>();
		GameObject[] itemArray = component.ItemArray;
		if (itemArray.Length > 0)
		{
			this.infoUiList = new List<FacilityInfoUI>();
			for (int i = 0; i < itemArray.Length; i++)
			{
				FacilityInfoUI component2 = itemArray[i].GetComponent<FacilityInfoUI>();
				GameObject childObject = UTY.GetChildObject(component2.gameObject, "InputField Facility Name/Button Change Name", false);
				childObject.SetActive(false);
				this.infoUiList.Add(component2);
			}
		}
		this.faciltyManagement.ToggleEvent_VisibleFacilityInfo(true);
		this.UpdateFacilityInfo();
		this.UpdateFacilityInfoInteractable();
	}

	public void AddMaid(FacilityInfoUI infoUI)
	{
		if (infoUI == null)
		{
			return;
		}
		if (infoUI.facility == null)
		{
			return;
		}
		if (infoUI.facility.NowMaidCount(this.current_time) >= infoUI.facility.maxMaidCount)
		{
			GameMain.Instance.SysDlg.Show("この施設にはこれ以上メイドを配置出来ません。", SystemDialog.TYPE.OK, null, null);
			return;
		}
		bool flag = infoUI.facility.AllocationMaid(this.current_maid, this.current_time);
		if (flag)
		{
			this.viewer.OnClickTaskUnit(this.taskUnit);
			this.UpdateFacilityInfo();
		}
	}

	private void UpdateFacilityInfoInteractable()
	{
		if (this.infoUiList == null)
		{
			return;
		}
		foreach (FacilityInfoUI facilityInfoUI in this.infoUiList)
		{
			bool isInteractable = false;
			if (facilityInfoUI.facility != null && this.current_workData.facility != null)
			{
				isInteractable = (facilityInfoUI.facility.defaultData.ID == this.current_workData.facility.ID);
			}
			foreach (KeyValuePair<int, Facility> keyValuePair in GameMain.Instance.FacilityMgr.GetNextDayFacilityArray())
			{
				Facility value = keyValuePair.Value;
				if (value != null && facilityInfoUI.facility != null && value == facilityInfoUI.facility)
				{
					isInteractable = false;
					break;
				}
			}
			facilityInfoUI.IsInteractable = isInteractable;
		}
	}

	private void UpdateFacilityInfo()
	{
		if (this.infoUiList == null)
		{
			return;
		}
		foreach (FacilityInfoUI facilityInfoUI in this.infoUiList)
		{
			bool maidIconShowFlag = false;
			if (facilityInfoUI.facility != null)
			{
				Maid[] allocationMaidArray = facilityInfoUI.facility.GetAllocationMaidArray(this.current_time);
				foreach (Maid y in allocationMaidArray)
				{
					if (this.current_maid == y)
					{
						facilityInfoUI.SetMaidIcon(this.current_maid, this.current_time);
						maidIconShowFlag = true;
						break;
					}
				}
			}
			facilityInfoUI.SetMaidIconShowFlag(maidIconShowFlag);
			facilityInfoUI.UpdateFacilityInfo(this.current_time, true);
		}
		this.UpdateFacilityInfoPanel();
	}

	private void UpdateFacilityInfoPanel()
	{
		Facility maidAssignedFacility = GameMain.Instance.FacilityMgr.GetMaidAssignedFacility(this.current_maid, this.current_time);
		if (maidAssignedFacility != null)
		{
			this.m_UIFacilityInfoConstruction.SetFacilityInfo(maidAssignedFacility, true);
			this.m_UIFacilityInfoConstruction.UpdateFacilityInfo(this.current_time, true);
			this.m_UIFacilityInfoConstruction.gameObject.SetActive(true);
		}
		else
		{
			this.m_UIFacilityInfoConstruction.gameObject.SetActive(false);
		}
	}

	private SceneFacilityManagement faciltyManagement;

	private GameObject parentFacility;

	private List<FacilityInfoUI> infoUiList;

	private WorkTaskUnit taskUnit;

	private ScheduleTaskViewer viewer;

	private ScheduleMgr.ScheduleTime current_time;

	private Maid current_maid;

	private ScheduleCSVData.Work current_workData;

	[SerializeField]
	private FacilityInfoUI m_UIFacilityInfoConstruction;

	private bool init;
}
