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

namespace Nox.Game {
    [CreateAssetMenu(fileName = "DefaultPartySettings", menuName = "Nox/Database/Entities/Default Party Settings")]
    public class PartySettings : ScriptableObject {
        [Header("Party System Defaults")]
        public int maxPartySize = 4;

        [Header("This will be default starting set")]
        public string testStartingSetId;

        [Header("Party Definition Sets")]
        public List<PartyDefinitionSet> testPartyDefinitionSets;

        private void OnValidate() {
            if(String.IsNullOrEmpty(testStartingSetId)) {
                Debug.LogError("DefaultPartySettings: startingSetId cannot be null or empty");
                return;
            }
            foreach(var partyDefinitionSet in testPartyDefinitionSets) {
                var partyDefinition = partyDefinitionSet.partyDefinition;
                if(partyDefinition == null) {
                    return;
                }

                for(var i = 0; i < partyDefinition.maxPartySize; i++) {
                    if (partyDefinition.members.Count <= i) {
                        partyDefinition.members.Add(new CharacterDefinition());
                    }
                }

                if(partyDefinition.members.Count <= partyDefinition.maxPartySize) {
                    continue;
                }
                Debug.LogError($"Party definition '{partyDefinitionSet.id}' has more members than the maximum allowed size.Removing extra members.");
                partyDefinition.members.RemoveRange(partyDefinition.maxPartySize, partyDefinition.members.Count - partyDefinition.maxPartySize);
            }
        }
    }

    [Serializable]
    public sealed class PartyDefinitionSet {
        public string id;
        public bool isTestingSet;
        public PartyDefinition partyDefinition;
    }
}
