using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoviDok.Api;
using CoviDok.Api.Request;
using CoviDok.Api.Response;
using CoviDok.BLL;
using CoviDok.BLL.Sessions;
using CoviDok.BLL.User.Managers;
using CoviDok.Data.Model;
using CoviDok.Data.SessionProviders;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CoviDok.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CaseController : ControllerBase
    {
        private readonly SessionHandler Handler = new SessionHandler();
        private readonly CaseManager mgr = new CaseManager();

        // POST /api/Case/{id}
        [HttpPost("{id}")]
        public async Task<ActionResult<Case>> PostGetCase(int id, string SessionId)
        {
            try {
                Session s = await Handler.GetSession(SessionId);
                return await mgr.GetCase(s, id);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (UnauthorizedAccessException)
            {
                return Unauthorized();
            }
        }

        // POST /api/Case/{id}/update

        [HttpPut("{id}/update")]
        public async Task<IActionResult> PostUpdate(int id, CaseUpdate data)
        {            
            try
            {
                Session s = await Handler.GetSession(data.SessionId);
                await mgr.UpdateCase(s, id, data.UpdateMsg, data.Images);
                return Ok();
            }
            catch (UnauthorizedAccessException)
            {
                return Unauthorized();
            }
            catch (KeyNotFoundException) {
                return NotFound();
            }
            catch (InvalidOperationException)
            {
                return BadRequest();
            }
            
        }

        [HttpPost]
        public async Task<ActionResult<Case>> NewCase(CaseCreate data)
        {
            try
            {
                Session s = await Handler.GetSession(data.SessionId);
                Case c = await mgr.CreateCase(s, data.DoctorId, data.ChildId, data.Title, data.StartDate);
                return CreatedAtAction("PostGetCase", new { id = c.Id }, c);
            }
            catch (UnauthorizedAccessException)
            {
                return Unauthorized();
            }
        }

        [HttpPost("{id}/updates")]
        public async Task<ActionResult<List<Update>>> GetUpdatesForCase(int id, string SessionId)
        {
            try {
                Session s = await Handler.GetSession(SessionId);
                return await mgr.GetUpdatesForCase(s, id);
            }
            catch (UnauthorizedAccessException)
            {
                return Unauthorized();
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpPost("updates/{id}")]
        public async Task<ActionResult<Update>>  GetUpdate(int id, string SessionId)
        {
            try
            {
                Session s = await Handler.GetSession(SessionId);
                return await mgr.GetUpdate(s, id);
            }
            catch (UnauthorizedAccessException)
            {
                return Unauthorized();
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        // POST /api/Case/{id}/close
        [HttpPost("{id}/close")]
        public async Task<IActionResult> PostClose(int id, string SessionId)
        {
            try
            {
                Session s = await Handler.GetSession(SessionId);
                await mgr.SetCertified(s, id);
                return Ok();
            }
            catch (UnauthorizedAccessException)
            {
                return Unauthorized();
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }
        // POST /api/Case/{id}/close
        [HttpPost("{id}/cure")]
        public async Task<IActionResult> PostCured(int id, string SessionId)
        {
            try
            {
                Session s = await Handler.GetSession(SessionId);
                await mgr.SetCured(s, id);
                return Ok();
            }
            catch (UnauthorizedAccessException)
            {
                return Unauthorized();
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        // POST /api/Case/filter
        [HttpPost("filter")]
        public async Task<ActionResult<List<Case>>> Filter(CaseFilter filters)
        {
            try
            {
                Session s = await Handler.GetSession(filters.SessionId);
                return await mgr.FilterCases(s, filters);
            }
            catch (UnauthorizedAccessException)
            {
                return Unauthorized();
            }
            
        }

    }
}