using Bellwether.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Bellwether
{
    public class BellwetherAPI
    {
        public async Task<List<SolarEnergyModel>> GetSolarenergy()
        {
            var url = "http://plm.bellwether-corp.com:8081/BW_Service.asmx/ExportSolarenergy_All_1";
            var result = await CallApi<SolarEnergyRawModel>(url);
            if (result == null)
            {
                return new List<SolarEnergyModel>();
            }
            var toReturn = new List<SolarEnergyModel>();
            foreach(var rawData in result)
            {
                var parseResult = SolarEnergyModel.Parse(rawData);
                if (string.IsNullOrEmpty(parseResult.Area))
                    continue;
                toReturn.Add(parseResult);
            }
            return toReturn;
        }

        public async Task<List<SolarenergyCapacityModel>> GetSolarenergyCapacity()
        {
            var url = "http://plm.bellwether-corp.com:8081/BW_Service.asmx/ExportSolarenergy_All_2";
            var result = await CallApi<SolarenergyCapacityRawModel>(url);
            if (result == null)
            {
                return new List<SolarenergyCapacityModel>();
            }
            var toReturn = new List<SolarenergyCapacityModel>();
            foreach (var rawData in result)
            {
                var parseResult = SolarenergyCapacityModel.Parse(rawData);
                if (string.IsNullOrEmpty(parseResult.Area))
                    continue;
                toReturn.Add(parseResult);
            }
            return toReturn;
        }

        public async Task<TitleModel> GetSolarenergyTitle1()
        {
            var url = "http://plm.bellwether-corp.com:8081/BW_Service.asmx/ExportSolarenergy_All_3";
            var result = await CallApi<TitleRawModel>(url);
            if (result == null || result.Count() == 0)
            {
                return new TitleModel();
            }
            return TitleModel.Parse(result[0]);
        }

        public async Task<TitleModel> GetSolarenergyTitle2()
        {
            var url = "http://plm.bellwether-corp.com:8081/BW_Service.asmx/ExportSolarenergy_All_4";
            var result = await CallApi<TitleRawModel>(url);
            if (result == null || result.Count() == 0)
            {
                return new TitleModel();
            }
            return TitleModel.Parse(result[0]);
        }

        private async Task<T[]> CallApi<T>(string url)
        {
            var result = await new HttpClient().GetStringAsync(url);
            Regex regex = new Regex(".*\"data\":(\\[.*\\]).*");
            var matchResult = regex.Match(result);
            if (matchResult == null || !matchResult.Success || matchResult.Groups.Count <= 1)
            {
                return null;
            }
            try
            {
                return JsonConvert.DeserializeObject<T[]>(matchResult.Groups[1].Value);
            }
            catch
            {
                return null;
            }
        }
    }
}
