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

namespace CCNetWrapper
{
    public class ProjectLog
    {
        public List<BuildLog> BuildLogs { get; set; }

        public ProjectLog()
        { BuildLogs = new List<BuildLog>(); }

        public int GetCommitsPerUser(string userName)
        {
            return BuildLogs.Select(x => x.User == userName).ToArray().Count();
        }

        public void SortByMinorBuild()
        {
            BuildLogs = BuildLogs.OrderByDescending(x => x.GetMinorBuildNumber()).ToList();
        }

        private FogbugzWrapper.FogbugzClient _fBClient;

        public void SetFBClient(FogbugzWrapper.FogbugzClient fogbugzClient, ref long progTotal, ref long progCurrent)
        {
            _fBClient = fogbugzClient;
            progTotal = BuildLogs.Count;
            progCurrent = 0;
            foreach (var bl in BuildLogs)
            {
                bl.FBStatus = (string.IsNullOrEmpty(bl.Fogbugz) || _fBClient == null)
                ? FogbugzWrapper.FBEvent.FBStatuses.UNKNOWN
                : _fBClient.GetStatus(int.Parse(bl.Fogbugz));
                bl.ReleaseNote = (string.IsNullOrEmpty(bl.Fogbugz) || _fBClient == null)
                ? string.Empty
                : _fBClient.GetReleaseNote(int.Parse(bl.Fogbugz));
                Interlocked.Increment(ref progCurrent);
            }
        }
    }
}