using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Verse;

using UnityEngine;


namespace RimWorld{
public class PawnGenOption
{
	//Config
	public int 					selectionWeight;
	public PawnKindDef			kind;

	//Properties
	public float Cost{get{return kind.combatPower;}}
}

public class FactionDef : Def
{
	//General config
	public RulePackDef			factionNameMaker;
	public string				fixedName = null;
	public bool					humanlikeFaction = true;
	public bool					hidden = false;
	public List<PawnGroupMaker>	pawnGroupMakers = null;
	public string				pawnsPlural = "characters";
	public float				raidCommonality = 0;
	public bool					canFlee = true;
	public bool					canSiege = false;
	public bool					canStageAttacks = false;
	public float				earliestRaidDays = 0;
	public string				leaderTitle = "Leader";

	//Faction generation
	public int					requiredCountAtGameStart = 0;
	public bool					canMakeRandomly = false;

	//Humanlike faction config
	public RulePackDef			pawnNameMaker;
	public TechLevel			techLevel = TechLevel.Undefined;
	public string				backstoryCategory = null;
	public List<string>			hairTags = new List<string>();
	public ThingFilter			apparelStuffFilter = null;

	//Relations (can apply to non-humanlike factions)
	public FloatRange			startingGoodwill = FloatRange.Zero;
	public bool					mustStartOneEnemy = false;	//Game starts with one of these enemy at least
	public FloatRange			naturalColonyGoodwill = FloatRange.Zero;
	public float				goodwillDailyGain = 1f;
	public float				goodwillDailyFall = 1f;
	public bool					appreciative = true;

	//Map drawing
	public string				homeIconPath;
	public Color				homeIconColor = Color.white;

	//Unsaved
	[Unsaved] public Material	baseRenderMaterial = BaseContent.BadMat;





	public override void PostLoad()
	{
		base.PostLoad();

		if( !homeIconPath.NullOrEmpty() )
			baseRenderMaterial = MaterialPool.MatFrom(homeIconPath, ShaderDatabase.Transparent, homeIconColor);
	}

	public override void ResolveReferences()
	{
		base.ResolveReferences();

		if( apparelStuffFilter != null )
			apparelStuffFilter.ResolveReferences();
	}

	public override IEnumerable<string> ConfigErrors()
	{
		foreach( var error in base.ConfigErrors() )
			yield return error;

		if( factionNameMaker == null && fixedName == null )
			yield return "FactionTypeDef " + defName + " lacks a factionNameMaker and a fixedName.";

		if( techLevel == TechLevel.Undefined )
			yield return defName + " has no tech level.";

		if( humanlikeFaction )
		{
			if( backstoryCategory == null )
				yield return defName + " is humanlikeFaction but has no backstory category.";

			if( hairTags.Count == 0 )
				yield return defName + " is humanlikeFaction but has no hairTags.";
		}
	}

	public float MinPointsToGeneratePawnGroup()
	{
		if( pawnGroupMakers.NullOrEmpty() )
			return int.MaxValue;

		return pawnGroupMakers.Min( pgm=>pgm.MinPointsToGenerateAnything );
	}

	public bool CanUseStuffForApparel( ThingDef stuffDef )
	{
		if( apparelStuffFilter == null )
			return true;

		return apparelStuffFilter.Allows( stuffDef );
	}



	public static FactionDef Named( string defName )
	{
		return DefDatabase<FactionDef>.GetNamed(defName);
	}
}}

