using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.PdbDownloader.Logic.Pe;

using System;
using System.Text;

namespace PdbServer.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class Users : ControllerBase
    {
        const string selfdocpath = "D:\\Web\\UserData";
        private readonly ILogger<Users> _logger;
        public Users(ILogger<Users> logger)
        {
            _logger = logger;
        }
        [HttpGet]
        [Route("FileDownLoadGZ")]
        public string FileDownLoadGZ(string Filename)
        {
            StringBuilder sb = new StringBuilder();
            string filename = $"{selfdocpath}\\{Filename}";
            if (System.IO.File.Exists(filename))
            {
                byte[] bytes = Symbols.Compress(System.IO.File.ReadAllBytes(filename));
                for (int i = 0; i < bytes.Length; i++)
                {
                    sb.Append(bytes[i].ToString("X2"));
                }
            }
            return sb.ToString();
        }
        static List<string> Ips = new List<string>();
        [HttpGet]
        [Route("FileDownLoadGZLog")]
        public string FileDownLoadGZLog(string Filename)
        {
            var remoteIpAddress = HttpContext.Connection.RemoteIpAddress.ToString();
            if (!Ips.Contains(remoteIpAddress))
            {
                Ips.Add(remoteIpAddress);
                System.IO.File.WriteAllLines($"{selfdocpath}\\Log.txt", Ips);
            }
            StringBuilder sb = new StringBuilder();
            string filename = $"{selfdocpath}\\{Filename}";
            if (System.IO.File.Exists(filename))
            {
                byte[] bytes = Symbols.Compress(System.IO.File.ReadAllBytes(filename));
                for (int i = 0; i < bytes.Length; i++)
                {
                    sb.Append(bytes[i].ToString("X2"));
                }
            }
            return sb.ToString();
        }
        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("FileDownLoadNormal")]
        public IActionResult FileDownLoadNormal(string Filename)
        {
            StringBuilder sb = new StringBuilder();
            string filename = $"{selfdocpath}\\{Filename}";
            FileInfo fileInfo = new FileInfo(filename);
            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",
                    fileInfo.Name);
            }
            return null;

        }
        List<byte> ParseHex(string hex)
        {
            List<byte> result = new List<byte>();
            int len = hex.Length;
            bool ish = true;
            byte val = 0;
            for (int i = 0; i < len; i++)
            {
                bool isval = false;
                byte v = 0;
                if (hex[i] >= '0' && hex[i] <= '9')
                {
                    v = (byte)(hex[i] - '0');
                    isval = true;
                }
                else if (hex[i] >= 'A' && hex[i] <= 'F')
                {
                    v = (byte)(10 + (hex[i] - 'A'));
                    isval = true;
                }

                else if (hex[i] >= 'a' && hex[i] <= 'f')
                {
                    v = (byte)(10 + (hex[i] - 'a'));
                    isval = true;
                }
                if (isval)
                {
                    if (ish)
                    {
                        val = (byte)((v << 4) | val);
                    }
                    else
                    {
                        val = (byte)(v | val);
                        result.Add(val);
                        val = 0;
                    }
                    ish = !ish;
                }
            }
            return result;
        }
        [HttpPost]
        [Route("FileUpLoad/{Filename}")]
        public int FileUpLoad(string Filename, [FromBody] string buffer)
        {
            var bytes = ParseHex(buffer);
            if (System.IO.File.Exists($"{selfdocpath}\\{Filename}"))
            {
                System.IO.File.WriteAllBytes($"{selfdocpath}\\{Filename}", bytes.ToArray());
                return 2;
            }
            else
            {
                System.IO.File.WriteAllBytes($"{selfdocpath}\\{Filename}", bytes.ToArray());
                return 1;
            }
        }
        [HttpPost]
        [Route("FileUpLoadBytes/{Filename}")]
        public int FileUpLoadBytes(string Filename, [FromBody] HttpRequestMessage req)
        {
            var content = req.Content.ReadAsStream();
            byte[] bytes = new byte[content.Length];
            int len = content.Read(bytes, 0, bytes.Length);

            if (System.IO.File.Exists($"{selfdocpath}\\{Filename}"))
            {
                System.IO.File.WriteAllBytes($"{selfdocpath}\\{Filename}", bytes.ToArray());
                return 2;
            }
            else
            {
                System.IO.File.WriteAllBytes($"{selfdocpath}\\{Filename}", bytes.ToArray());
                return 1;
            }
        }
    }
}
