using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using PhilExample.Interfaces;
using PhilExampleCrawler.Common.Models;
using Xamarin.Forms;

namespace PhilExample.Services
{
    internal class CrawlSessionService : ICrawlSessionService
    {
        private readonly IRESTAccess _rest = DependencyService.Get<IRESTAccess>();

        public async Task<CrawlSession> RegisterCrawlSessionAsync(int userID, CrawlSession newCS)
        {
            var url = App.WebAPI_URL + $"CrawlSession/RegisterCrawlSession?userID={userID}";
            var cs = await _rest.PostAsync<CrawlSession>(url, newCS);
            return cs;
        }

        public async Task<bool> UpdateCrawlSessionAsync(int userID, CrawlSession cs)
        {
            var url = App.WebAPI_URL + $"CrawlSession/UpdateCrawlSession" +
                                                                        $"?userID={userID}";
            bool updated = await _rest.PostAsync<bool>(url, cs);
            return updated;
        }

        public async Task<bool> DeleteAsync(int crawlSessionID)
        {
            var url = App.WebAPI_URL + $"CrawlSession/DeleteCrawlSession" +
                                                                        $"?crawlSessionID={crawlSessionID}";
            bool deleted = await _rest.DeleteAsync(url);
            return deleted;
        }

        public async Task<List<Insertion>> GetLatestInsertionAsync(int userID, int? limit)
        {
            var url = App.WebAPI_URL + $"CrawlSession/GetLatestInsertion?userID={userID}";
            if (limit.HasValue)
                url += "&limit=" + limit.Value;
            var cs = await _rest.GetAsync<List<Insertion>>(url);
            return cs;
        }

    }
}
