using FamilyTreeAPI.Entities;
using FamilyTreeAPI.Interface;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;

namespace FamilyTreeAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PersonController : ControllerBase
    {
        private readonly IPerson _repo;
        public PersonController(IPerson repo)
        {
            _repo = repo;
        }
        [HttpPost("[action]")]
        public async Task<IActionResult> UploadImage(IFormFile file)
        {
            var keys = Request.Form;
            var files = Request.Form.Files;
            ResultModel<string> ret = new();
            if (files.Count > 0)
            {
                StringValues familyId = "";

                keys.TryGetValue("personId", out familyId);
               
                UploadCriteria criteria = new();
                criteria.File = file;
                criteria.PersonId = int.Parse(familyId);
              
                criteria.FileName = file.FileName;

                ret = await _repo.UploadImage(criteria);
            }
            return Ok(ret);

            
        }
        //DeleteUploadFile
        [HttpPost("[action]")]
        public ActionResult DeleteUploadFile(DeleteFileCriteria criteria)
        {

            ResultModel<int> ret = new();
            if (!string.IsNullOrEmpty(criteria.Filename))
            {

                ret = _repo.DeleteUploadFile(criteria);
            }
            return Ok(ret);
        }

        [HttpPost("[action]")]
        public async Task<IActionResult> SearchPerson(PersonCriteria criteria)
        {
            var list = await _repo.GetPerson(criteria);
            
            return Ok(list);
        }
        
        [HttpPost("[action]")]
        public async Task<IActionResult> GetChildress(ChildCriteria criteria)
        {
            var list = await _repo.GetChildren(criteria);
            
            return Ok(list);
        }
        [HttpPost("[action]")]
        public async Task<IActionResult> GetFamilyTreeBy(FamilyCriteria criteria)
        {
            var list = await _repo.GetFamilyTreeBy(criteria);
            return Ok(list);
        }
        [HttpGet("[action]/{id}")]
        public async Task<IActionResult> GetByPersonFamily(int id)
        {
            var list = await _repo.GetByFamilyAsync(id);
            return Ok(list);
        }
        [HttpGet("[action]/{id}")]
        public async Task<IActionResult> GetById(int id)
        {
            var list = await _repo.GetByIdAsync(id);
            return Ok(list);
        }

        [HttpPost("[action]")]
        public async Task<IActionResult> DeleteById(DeleteCriteria<int> criteria)
        {
            var list = await _repo.DeleteAsync(criteria.Id);
            return Ok(list);
        }

        [HttpPost("[action]")]
        public async Task<IActionResult> SavePerson([FromBody] PersonForSave model)
        {
            //var currentUser = (User?)(HttpContext.Items["User"]);
            //if (null == currentUser)
            //    return Unauthorized(new { message = "Unauthorized" });

            var response = await _repo.SaveAsync(model);

            return Ok(response);
        }

       
    }
}
