using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using RestSharp;

namespace WorkerService.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HomeController : ControllerBase
    {
        private readonly ILogger<HomeController> _logger;
        private readonly RestClient _restClient;

        public static List<WorkerEntity> list = new List<WorkerEntity>();
        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
            _restClient = new RestClient("http://nginx/postservice/Home");
        }

        [HttpGet("get")]
        public IActionResult Get()
        {
            if (list == null || list.Count == 0)
            {
                return NotFound();
            }

            var result = list.Select(l => new GetEntity
            {
                Id = l.Id,
                FIO = l.FIO,
                PostId = l.PostId,
            });

            return Ok(result);
        }

        [HttpGet("get/{Id}")]
        public IActionResult Get([FromRoute] Guid Id)
        {
            var obj = list.Where(l => l.Id == Id);
            return obj == null ? NotFound() : Ok(obj);
        }

        [HttpPost("create")]
        public async Task<IActionResult> CreateAsync([FromBody] CreateUpdateWorker data)
        {
            try
            {
                var model = new WorkerEntity()
                {
                    Id = Guid.NewGuid(),
                    FIO = data.FIO,
                    PostId = data.PostId,
                };

                var restRequest = new RestRequest($"/get/{data.PostId}", Method.Get);
                var responce = await _restClient.ExecuteAsync<PostEntity>(restRequest);

                if (!responce.IsSuccessful)
                {
                    return BadRequest(responce);
                }

                model.Post = responce.Data;

                list.Add(model);

                return Ok(model);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpPut("update/{Id}")]
        public IActionResult Update([FromRoute] Guid Id,
                                    [FromBody] CreateUpdateWorker data)
        {
            var oldModel = list.Where(l => l.Id == Id).Select(l => list.IndexOf(l)).FirstOrDefault();
            if (oldModel != null)
            {
                list[oldModel].FIO = data.FIO;
                list[oldModel].PostId = data.PostId;

                var modelForResult = new CreateUpdateWorker
                {
                    FIO = data.FIO,
                    PostId = data.PostId,
                };

                return Ok(modelForResult);
            }
            else return NotFound();
        }

        [HttpDelete("delete/{Id}")]
        public IActionResult Delete([FromRoute] Guid Id)
        {
            var model = list.Where(l => l.Id != Id).FirstOrDefault();

            if (model != null)
            {
                list.Remove(model);
                return Ok();
            }
            else return NotFound();
        }
    }
}
