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

namespace PkmnLibSharp.Battling
{
    public class BattleBuilder
    {
        protected readonly BattleLibrary _library;
        protected readonly bool _canFlee;
        protected readonly byte _numberOfSides;
        protected readonly byte _pokemonPerSide;
        protected ulong? _seed;
        protected readonly List<BattleParty> _parties = new List<BattleParty>();
        
        public BattleBuilder(BattleLibrary library, bool canFlee, byte numberOfSides = 2, byte pokemonPerSide = 1)
        {
            _library = library;
            _canFlee = canFlee;
            _numberOfSides = numberOfSides;
            _pokemonPerSide = pokemonPerSide;
        }

        public BattleBuilder WithRandomSeed(ulong seed)
        {
            _seed = seed;
            return this;
        }

        public BattleBuilder WithPartyOnPositions(PokemonParty party, params BattlePosition[] positions)
        {
            var battleParty = new BattleParty(party, positions.SelectMany(x => new[] {x.Side, x.Index}).ToArray());
            _parties.Add(battleParty);
            return this;
        }

        public virtual Battle Build()
        {
            // Use the milliseconds since epoch time as random seed if none is specified.
            _seed ??= (ulong) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
            return new Battle(_library, _parties.ToArray(), _canFlee, _numberOfSides, _pokemonPerSide, _seed.Value);
        }
    }
}