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

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

        public async Task<IEnumerable<Category>> GetCategoriesAsync()
        {
            var url = App.WebAPI_URL + "ExamplePage/GetCategories";
            var cats = await _rest.GetAsync<IEnumerable<Category>>(url);
            return cats;
        }

        public ICategory GetDefaultCategory()
        {
            return new Category("Alle Kategorien", -1);
        }

        public async Task<IEnumerable<Category>> GetRadiiAsync()
        {
            var url = App.WebAPI_URL + "ExamplePage/GetRadii";
            var radii = await _rest.GetAsync<IEnumerable<Category>>(url);
            return radii;
        }

        public ICategory GetDefaultRadius()
        {
            return new Category("Unbegrenzt", 0);
        }


        //TODO: Show as dropdown -> develop an overlapping dropdown
        public async Task<List<PickerItem>> GetLocationSuggestionsAsync(string query)
        {
            var url = "https://www.examplepage.com/whatever-json?query="
                        + HttpUtility.UrlEncode(query);

            var locSuggs = await _rest.GetAsync<Dictionary<string, string>>(url);
            var pItems = new List<PickerItem>();

            foreach (var locSugg in locSuggs)
                if (locSugg.Key != null && 
                    locSugg.Key.StartsWith("_") && 
                    int.TryParse(locSugg.Key.Substring(1), out int locID))

                    pItems.Add(new PickerItem(locSugg.Value, locID));

            return pItems;
        }
    }
}
