﻿using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using HelperTool.Core.Helper;
using System.Text.RegularExpressions;
using HelperTool.Services.Interfaces;
using HelperTool.ViewModels;

namespace HelperTool.Controllers
{
    [ApiVersion("1.0")]
    [Route("v{version:apiVersion}/functions")]
    public class FunctionsController : Controller
    {
        private IHtmlService _htmlService;

        public FunctionsController(IHtmlService htmlService)
        {
            _htmlService = htmlService;
        }


        [HttpGet]
        [Route("generateguid")]
        public async Task<IActionResult> GenerateNewGuids(int guidCount)
        {
            var list = await Task.Run(() => GuidOperations.GenerateGuids(guidCount));
            return Json(list);
        }

        [HttpGet]
        [Route("regexmatch")]
        public IActionResult RegexMatch(string text, string regex)
        {
            try
            {
                if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(regex))
                {
                    return BadRequest("Please fill regex and text area.");
                }

                Regex reg = new Regex(regex);
                Match match = reg.Match(text);
                if (match.Success)
                {
                    return Ok(match.Value);
                }
                else
                    return BadRequest("Not match");
            }
            catch (System.Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpGet]
        [Route("generatepassword")]
        public IActionResult GeneratePassword(int passwordLength, bool strongPassword)
        {
            try
            {
                var random = new Random();

                int seed = random.Next(1, int.MaxValue);
                //const string allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
                const string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
                const string specialCharacters = @"!#$%&'()*+,-./:;<=>?@[\]_";

                var chars = new char[passwordLength];
                var rd = new Random(seed);

                for (var i = 0; i < passwordLength; i++)
                {
                    if (strongPassword && i % random.Next(3, passwordLength) == 0)
                    {
                        chars[i] = specialCharacters[rd.Next(0, specialCharacters.Length)];
                    }
                    else
                    {
                        chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
                    }
                }

                return Ok(new string(chars));
            }
            catch (System.Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpPost]
        [Route("convertbase64")]
        public IActionResult ConvertBase64Data([FromBody]ConvertBase64Model model)
        {
            try
            {
                if (model.Encode)
                {
                    var result = _htmlService.Base64Encode(model.Value);
                    return Ok(result);
                }
                else
                {
                    var result = _htmlService.Base64Decode(model.Value);
                    return Ok(result);
                }
            }
            catch (Exception)
            {
                return BadRequest();
            }
        }

        [HttpPost]
        [Route("removelinebreaks")]
        public IActionResult RemoveLineBreaks([FromBody]LineBreaksModel model)
        {
            try
            {
                var result = _htmlService.RemoveLineBreaks(model.RawString, model.AllBreaks);
                return Ok(result);
            }
            catch (Exception)
            {
                return BadRequest();
            }
        }
    }
}
