<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Manatee.Trello;
using ReadingList.Logging;
using ReadingList.Logic.Models;
using ReadingList.Logic.Services;
using ReadingList.Trello.Helpers;
using ReadingList.Trello.Models;
using ILog = ReadingList.Logging.ILog;

namespace ReadingList.Trello.Services
{
    public class ReadingListService : IReadingListService
    {
        private readonly IBoard _board;
        private readonly ILog _logger;

        public ReadingListService(
            ITrelloFactory factory,
            ILogFactory logFactory)
        {
            _board = factory.Board(TrelloBoardConstants.BoardId);
            _logger = logFactory.GetLogger(this.GetType());
        }

        public async Task<IEnumerable<Book>> GetReadingList(string listName, string label = null)
        {
            await _board.Lists.Refresh();

            var cardList = string.IsNullOrEmpty(label) ?
                _board.Lists.FirstOrDefault(l => l.Name.Equals(listName))?.Cards :
                _board.Lists.FirstOrDefault(l => l.Name.Equals(listName))?.Cards.Where(c => c.Labels.All(l => l.Name.ToLower().Equals(label.ToLower())));

            return cardList?.Select(card => BookMapper.CreateBook(card.Name, card.Labels.FirstOrDefault()?.Name.ToLower() ?? ReadingListConstants.UnspecifiedLabel)).ToList();
        }

        public async Task<bool> AddBookToBacklog(string book, string authors, string label)
        {
            await _board.Lists.Refresh();

            bool addSuccessfull;
            var backlogCardListId = _board.Lists.FirstOrDefault(l => l.Name.Equals(ReadingListConstants.Backlog))?.Id;
            var backlogCardList = new List(backlogCardListId);
            try
            {
                var bookLabel = _board.Labels.FirstOrDefault(l => l.Name.ToLower().Equals(label.ToLower()));
                await backlogCardList.Cards.Add(name: FormatCardName(book, authors), labels: new[] { bookLabel });
                _logger.Info($"Adding {book}, {authors}, {label} to {ReadingListConstants.Backlog}");
                addSuccessfull = true;
            }
            catch (Exception ex)
            {
                _logger.Error($"Error when trying to add {book}, {authors}, {label} to {ReadingListConstants.Backlog}: ", ex);
                addSuccessfull = false;
            }
            return addSuccessfull;
        }

        public async Task<bool> UpdateDoneListFromReadingList(string book)
        {
            await _board.Lists.Refresh();

            bool updateSuccessful;
            string doneCardListId = _board.Lists.FirstOrDefault(l => l.Name.Equals(ReadingListConstants.DoneReading))?.Id;
            var doneCardList = new List(doneCardListId);
            try
            {
                var card = _board.Lists.FirstOrDefault(l => l.Name.Equals(ReadingListConstants.CurrentlyReading))?.Cards.SingleOrDefault(c => c.Name.ToLower().Contains(book.ToLower()));
                if (card == null)
                {
                    _logger.Info($"Could not find {book} in {ReadingListConstants.CurrentlyReading}, so can't move to {ReadingListConstants.DoneReading}.");
                    return updateSuccessful = false;
                }
                card.List = doneCardList;
                card.Position = new Position(1);
                updateSuccessful = true;
                _logger.Info($"Moving {book} to {ReadingListConstants.DoneReading} from {ReadingListConstants.CurrentlyReading}");
            }
            catch (Exception ex)
            {
                _logger.Error($"Error when trying to move {book} to {ReadingListConstants.DoneReading}: ", ex);<commit_msg>Removed uneeded variable declaration<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Manatee.Trello;
using ReadingList.Logging;
using ReadingList.Logic.Models;
using ReadingList.Logic.Services;
using ReadingList.Trello.Helpers;
using ReadingList.Trello.Models;
using ILog = ReadingList.Logging.ILog;

namespace ReadingList.Trello.Services
{
    public class ReadingListService : IReadingListService
    {
        private readonly IBoard _board;
        private readonly ILog _logger;

        public ReadingListService(
            ITrelloFactory factory,
            ILogFactory logFactory)
        {
            _board = factory.Board(TrelloBoardConstants.BoardId);
            _logger = logFactory.GetLogger(this.GetType());
        }

        public async Task<IEnumerable<Book>> GetReadingList(string listName, string label = null)
        {
            await _board.Lists.Refresh();

            var cardList = string.IsNullOrEmpty(label) ?
                _board.Lists.FirstOrDefault(l => l.Name.Equals(listName))?.Cards :
                _board.Lists.FirstOrDefault(l => l.Name.Equals(listName))?.Cards.Where(c => c.Labels.All(l => l.Name.ToLower().Equals(label.ToLower())));

            return cardList?.Select(card => BookMapper.CreateBook(card.Name, card.Labels.FirstOrDefault()?.Name.ToLower() ?? ReadingListConstants.UnspecifiedLabel)).ToList();
        }

        public async Task<bool> AddBookToBacklog(string book, string authors, string label)
        {
            await _board.Lists.Refresh();

            bool addSuccessfull;
            var backlogCardListId = _board.Lists.FirstOrDefault(l => l.Name.Equals(ReadingListConstants.Backlog))?.Id;
            var backlogCardList = new List(backlogCardListId);
            try
            {
                var bookLabel = _board.Labels.FirstOrDefault(l => l.Name.ToLower().Equals(label.ToLower()));
                await backlogCardList.Cards.Add(name: FormatCardName(book, authors), labels: new[] { bookLabel });
                _logger.Info($"Adding {book}, {authors}, {label} to {ReadingListConstants.Backlog}");
                addSuccessfull = true;
            }
            catch (Exception ex)
            {
                _logger.Error($"Error when trying to add {book}, {authors}, {label} to {ReadingListConstants.Backlog}: ", ex);
                addSuccessfull = false;
            }
            return addSuccessfull;
        }

        public async Task<bool> UpdateDoneListFromReadingList(string book)
        {
            await _board.Lists.Refresh();

            bool updateSuccessful;
            string doneCardListId = _board.Lists.FirstOrDefault(l => l.Name.Equals(ReadingListConstants.DoneReading))?.Id;
            var doneCardList = new List(doneCardListId);
            try
            {
                var card = _board.Lists.FirstOrDefault(l => l.Name.Equals(ReadingListConstants.CurrentlyReading))?.Cards.SingleOrDefault(c => c.Name.ToLower().Contains(book.ToLower()));
                if (card == null)
                {
                    _logger.Info($"Could not find {book} in {ReadingListConstants.CurrentlyReading}, so can't move to {ReadingListConstants.DoneReading}.");
                    return false;
                }
                card.List = doneCardList;
                card.Position = new Position(1);
                updateSuccessful = true;
                _logger.Info($"Moving {book} to {ReadingListConstants.DoneReading} from {ReadingListConstants.CurrentlyReading}");
            }
            catch (Exception ex)
            {
                _logger.Error($"Error when trying to move {book} to {ReadingListConstants.DoneReading}: ", ex);
{'fraction_non_alphanumeric': 0.10639721627408993, 'fraction_numerical': 0.0002676659528907923, 'mean_word_length': 2.372292418772563, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1555561', 'n_tokens_mistral': 2077, 'n_tokens_neox': 1965, 'n_words': 391}