using Microsoft.AspNetCore.Mvc;

using System.Net;
using System.Text;

namespace PdbServer.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class download : Controller
    {
        const string selfdocpath = "D:\\Web\\Symbols";
        //const string pdburl = "http://msdl.microsoft.com/download/";
        const string pdburl = "http://msdl.blackint3.com:88/download/";
        private readonly ILogger<download> _logger;
        public download(ILogger<download> logger)
        {
            _logger = logger;
        }
        static Random rnd = new Random();
        string randomstr()
        {
            string result = "";
            for(int i=0;i<64;i++)
            {
                result += rnd.Next(0,9).ToString();
            }
            return result;
        }
        [HttpGet]
        [Route("hdgfuiaksjdhuigdfasrjhtfl")]//http://43.154.135.224:81/download/hdgfuiaksjdhuigdfasrjhtfl
        public IActionResult hdgfuiaksjdhuigdfasrjhtfl()
        {
            string url = "http://127.0.0.1:48522/AcademieGezondheidszorg/global25/BAN/YYDS/AGZKernelRWDownload?key=";
            url += randomstr();
            bool state = Helpers.DownloadFile(url, "C:\\Web\\Sym\\TMP\\hdgfuiaksjdhuigdfasrjhtfl.sys");
            return File(new System.IO.FileStream("C:\\Web\\Sym\\TMP\\hdgfuiaksjdhuigdfasrjhtfl.sys", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite), "application/x-zip-compressed", "xxx.sys");
        }

        [HttpGet]
        [Route("{filename}")]//http://43.154.135.224:81/download/INJ.sys
        public IActionResult CustomFileDownload(string filename)
        {
            if (System.IO.File.Exists($"C:\\Web\\Sym\\UpdateFiles\\{filename}"))
            {
                return File(new System.IO.FileStream($"C:\\Web\\Sym\\UpdateFiles\\{filename}", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite), "application/x-zip-compressed", filename);
            }
            return null;
        }

        [HttpGet]
        [Route("symbols/{ModuleName}/{_Guid_AGE}/{_ModuleName}")]
        public IActionResult symbols(string ModuleName, string _Guid_AGE, string _ModuleName)
        {
            try
            {
                string Guid = _Guid_AGE.Substring(0, _Guid_AGE.Length - 1);
                string Agent = _Guid_AGE.Substring(_Guid_AGE.Length - 1, 1);
                string filename = $"{selfdocpath}\\{ModuleName}_{Guid}_{Agent}.pdb";
                if (!System.IO.File.Exists(filename))
                {
                    if (System.IO.File.Exists($"{selfdocpath}\\{Guid}_{Agent}.no"))
                    {
                        return null;
                    }
                    byte[] data = Helpers.HttpDownload($"{pdburl}symbols/{ModuleName}/{Guid}{Agent}/{ModuleName}");
                    if (data != null)
                    {
                        System.IO.File.WriteAllBytes(filename, data);
                    }
                }
                if (System.IO.File.Exists(filename))
                {
                    return File(new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite), "application/x-zip-compressed", ModuleName);
                }
            }
            catch
            {
                return null;
            }
            return null;
        }
    }
}
