using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using System.Linq;

using TechCheck.Data.StackOverflow;
using System;
using Microsoft.Extensions.Logging;

namespace TechCheck.Controllers
{
    /*
    
        stack-overflow
            tag-usages [absolute number]
            question-accepted-answer-rate [percentage]
            
        github
        
        twitter
    
    */
   
    
    [Route("api")]
    public sealed class ApiController : Controller
    {
        ILogger _logger;

        public ApiController(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger("ApiController");
        }
        
        [HttpGet("{technology}")]
        public async Task<Category[]> GetAsync(string technology)
        {   
            using(var stackClient = new StackClient(_logger))
            {
                var tags = await stackClient.GetAsync<StackTag>(
                    new [] { "tags", technology, "info" },
                    new Dictionary<string,string> {
                        { "order", "desc" }, 
                        { "sort", "popular" } 
                    }
                );
                
                var questions = await stackClient.GetAsync<StackQuestion>(
                    new [] { "search", "advanced" },
                    new Dictionary<string,string> { 
                        { "order", "desc"}, 
                        { "sort", "activity"},
                        { "q", Uri.EscapeDataString(technology) }
                    }
                );
                
                var answeredQuestions = questions.Where(x => x.Is_Answered == true).ToArray();
                var averageQuestionViews = questions.Length > 0 ? questions.Average(x => x.View_Count) : -1;
                var averageQuestionScore = questions.Length > 0 ? questions.Average(x => x.Score) : -1;
                
                return new []
                { 
                    new Category
                    {
                        Name = "StackOverflow",
                        Stats = new[]
                        {
                            new Stat 
                            {
                                Name = "Tag Usages",
                                Value =  tags.Length == 1 ? tags[0].Count.ToString() : 0.ToString()
                            },
                            new Stat 
                            {
                                Name = "Answered Questions",
                                Value = Math.Round((double)answeredQuestions.Length / questions.Length * 100) + "%"
                            },
                            new Stat 
                            {
                                Name = "Average Question Views",
                                Value = Math.Round(averageQuestionViews).ToString()
                            },
                            new Stat 
                            {
                                Name = "Average Question Score",
                                Value = Math.Round(averageQuestionScore).ToString()
                            }
                        }
                    }
                };
            }
        }
        
    }
    public class Category
    {
        public string Name { get; set; }
        
        public Stat[] Stats { get; set; }
    }
    
    public class Stat
    {
        public string Name { get; set; }
        
        public string Value { get; set; }
    }
}
