using System.Collections.Generic;
using System.Linq;

namespace Wizard;

public class AIStyleData
{
	private List<AICategory> categoryFilter;

	private Dictionary<AIStyleKey, List<AIPolicyData>> policyDic = new Dictionary<AIStyleKey, List<AIPolicyData>>();

	public AIStyleData()
	{
	}

	public AIStyleData(List<AICategory> filter)
	{
		categoryFilter = filter;
	}

	public void Clear()
	{
		categoryFilter = null;
		policyDic.Clear();
	}

	public void CreateFromAsset(List<AIPolicyDataAsset> assetList)
	{
		foreach (AIPolicyDataAsset asset in assetList)
		{
			AIPolicyData aIPolicyData = new AIPolicyData(asset);
			AIStyleKey key = new AIStyleKey(aIPolicyData.Category, aIPolicyData.PolicyType);
			MixInPolicy(key, aIPolicyData);
		}
	}

	public List<AIPolicyData> ConvertToPolicyList()
	{
		List<AIPolicyData> list = new List<AIPolicyData>();
		foreach (KeyValuePair<AIStyleKey, List<AIPolicyData>> item in policyDic)
		{
			foreach (AIPolicyData item2 in item.Value)
			{
				list.Add(item2);
			}
		}
		return list;
	}

	public bool MixInPolicy(AIStyleKey key, AIPolicyData value)
	{
		if (categoryFilter != null && !categoryFilter.Any((AICategory c) => c == key.category))
		{
			return false;
		}
		if (policyDic.ContainsKey(key))
		{
			List<AIPolicyData> list = policyDic[key];
			if (list == null)
			{
				list = new List<AIPolicyData>();
				list.Add(value);
				return true;
			}
			int priority = list[0].Priority;
			if (value.Priority > priority)
			{
				list.Clear();
				list.Add(value);
				return true;
			}
			if (value.Priority == priority)
			{
				list.Add(value);
				return true;
			}
			return false;
		}
		List<AIPolicyData> list2 = new List<AIPolicyData>();
		list2.Add(value);
		policyDic.Add(key, list2);
		return true;
	}

	public void MixInStyle(AIStyleData addStyle)
	{
		foreach (KeyValuePair<AIStyleKey, List<AIPolicyData>> pair in addStyle.policyDic)
		{
			if (pair.Value == null || (categoryFilter != null && !categoryFilter.Any((AICategory c) => c == pair.Key.category)))
			{
				continue;
			}
			foreach (AIPolicyData item in pair.Value)
			{
				MixInPolicy(pair.Key, item);
			}
		}
	}
}
