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

public abstract class BaseCreateViewerCtrl : MonoBehaviour
{
	protected void CreateViewer<K, V>(Dictionary<K, V> dicData)
	{
		this.CreateViewer<K, V>(dicData, false);
	}

	public void CreateViewerWhenDataNotExist<K, V>(Dictionary<K, V> dicData)
	{
		this.CreateViewer<K, V>(dicData, true);
	}

	private void CreateViewer<K, V>(Dictionary<K, V> dicData, bool CanCreateWhenDataNotExist)
	{
		if (!CanCreateWhenDataNotExist && !this.HasDicData<K, V>(dicData))
		{
			return;
		}
		this.ExecuteTemplateOperation();
	}

	public void CreateViewer<T>(List<T> listData)
	{
		if (listData == null || listData.Count == 0)
		{
			return;
		}
		this.ExecuteTemplateOperation();
	}

	public void CreateViewer<T>(T data)
	{
		if (data == null)
		{
			return;
		}
		this.ExecuteTemplateOperation();
	}

	private void ExecuteTemplateOperation()
	{
		this.DelChildGameObject(this.m_goUnitParent);
		this.ValidateData();
		this.m_goUnitPrefab = this.GetUnitPrefab(this.m_unitPrefabPath);
		this.SetDataForViewer();
		this.Reposition();
	}

	private GameObject GetUnitPrefab(string unitPrefabPath)
	{
		this.m_goUnitPrefab = (this.m_goUnitPrefab ?? (Resources.Load(unitPrefabPath) as GameObject));
		return this.m_goUnitPrefab;
	}

	protected void DelChildGameObject(GameObject goParent)
	{
		IEnumerator enumerator = goParent.transform.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				Transform transform = (Transform)obj;
				UnityEngine.Object.Destroy(transform.gameObject);
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		goParent.transform.DetachChildren();
	}

	protected void DelChildGameObject()
	{
		IEnumerator enumerator = this.m_goUnitParent.transform.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				Transform transform = (Transform)obj;
				UnityEngine.Object.Destroy(transform.gameObject);
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		this.m_goUnitParent.transform.DetachChildren();
	}

	protected void Reposition()
	{
		UITable component = this.m_goUnitParent.GetComponent<UITable>();
		if (component)
		{
			component.Reposition();
			return;
		}
		UIGrid component2 = this.m_goUnitParent.GetComponent<UIGrid>();
		if (component2)
		{
			component2.Reposition();
			return;
		}
	}

	public static void Reposition(GameObject parent)
	{
		UITable component = parent.GetComponent<UITable>();
		if (component)
		{
			component.Reposition();
			return;
		}
		UIGrid component2 = parent.GetComponent<UIGrid>();
		if (component2)
		{
			component2.Reposition();
			return;
		}
	}

	protected virtual void ValidateData()
	{
	}

	protected void SetTransformInfo(GameObject copyPrefab)
	{
		this.SetTransformInfo(this.m_goUnitParent, copyPrefab);
	}

	protected void SetTransformInfo(GameObject parent, GameObject copyPrefabs)
	{
		copyPrefabs.transform.parent = parent.transform;
		copyPrefabs.transform.localScale = Vector3.one;
		copyPrefabs.transform.localPosition = Vector3.zero;
		copyPrefabs.transform.rotation = Quaternion.identity;
	}

	public bool HasDicData<K, V>(Dictionary<K, V> dicData)
	{
		return dicData != null && dicData.Count != 0;
	}

	public bool HasListData<T>(List<T> listData)
	{
		return listData != null && listData.Count > 0;
	}

	protected abstract void SetDataForViewer();

	protected GameObject m_goUnitParent;

	protected GameObject m_goUnitPrefab;

	protected string m_unitPrefabPath;

	private const float DURATION_TO_FADE = 0.5f;
}
