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

namespace Nox.Game {
    [CreateAssetMenu(fileName = "DefaultPartySettings", menuName = "Nox/Database/Entities/Default Party Settings")]
    public class DefaultPartySettings : ScriptableObject {
        [Header("This will be default starting set")]
        public string startingSetId;

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

        [Header("Testing Party Definition Sets")]
        public StarterCharacterSettings starterCharacterSettings;

        private void OnValidate() {
            if(String.IsNullOrEmpty(startingSetId)) {
                Debug.LogError("DefaultPartySettings: startingSetId cannot be null or empty");
                return;
            }
            foreach(var partyDefinitionSet in partyDefinitionSets) {
                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(partyDefinitionSets.FirstOrDefault(pds => pds.id == partyDefinitionSet.id && pds.isTestingSet) != null) {
                    var testingSet = partyDefinitionSets.FirstOrDefault(pds => pds.id == partyDefinitionSet.id && pds.isTestingSet);
                    ApplyClassAndRacialBonuses(testingSet);
                }

                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);
            }
        }
        private void ApplyClassAndRacialBonuses(PartyDefinitionSet testingSet) {
            var partyDefinition = testingSet.partyDefinition;
            foreach(var member in partyDefinition.members) {
                var baseSettings = starterCharacterSettings.defaultEntityAttributes;
                var classAttributes = starterCharacterSettings.classBonuses.FirstOrDefault(c => c.@class == member.Class)?.bonusAttributes;
                var racialAttributes = starterCharacterSettings.racialBonuses.FirstOrDefault(rb => rb.race == member.Race)?.bonusAttributes;
                if (classAttributes != null && racialAttributes != null) {
                    member.Attributes += baseSettings + classAttributes + racialAttributes;
                }
            }
        }
    }

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