using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Sockeye.Models;
using Sockeye.Api.ControllerHelpers;
using Sockeye.Biz;


namespace Sockeye.Api.Controllers
{
    [ApiController]
    [ApiVersion("8.0")]
    [Route("api/v{version:apiVersion}/memo")]
    [Produces("application/json")]
    [Authorize]
    public class MemoController : ControllerBase
    {
        private readonly AyContext ct;
        private readonly ILogger<MemoController> log;
        private readonly ApiServerState serverState;

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="dbcontext"></param>
        /// <param name="logger"></param>
        /// <param name="apiServerState"></param>  
        public MemoController(AyContext dbcontext, ILogger<MemoController> logger, ApiServerState apiServerState)
        {
            ct = dbcontext;
            log = logger;
            serverState = apiServerState;
        }

        /// <summary>
        /// Create Memo
        /// </summary>
        /// <param name="newObject"></param>
        /// <param name="apiVersion">From route path</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> PostMemo([FromBody] SendMemo newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            MemoBiz biz = MemoBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var RouteUserId = UserIdFromContext.Id(HttpContext.Items);
            //v8 migrate hacky workaround to allow specifying toid
            //-7 to id means it's migrating from v7 so treat as a single object


            if (newObject.Users.Count == 1 && newObject.Users[0] == -7 && RouteUserId == 1)
            {
                Memo newMemo = new Memo();
                Sockeye.Util.CopyObject.Copy(newObject.Memo, newMemo);
                // newMemo.ToId = newObject.Memo.ToId;
                // newMemo.FromId = newObject.Memo.FromId;
                Memo o = await biz.CreateAsync(newMemo);
                if (o == null)
                    return BadRequest(new ApiErrorResponse(biz.Errors));
                return Ok(ApiOkResponse.Response(new { Id = o.Id }));//v8 migrate needs to id number to fixup the log post migrate
            }
            else
            {

                foreach (long lUserId in newObject.Users)
                {
                    Memo newMemo = new Memo();
                    Sockeye.Util.CopyObject.Copy(newObject.Memo, newMemo);
                    newMemo.ToId = lUserId;
                    newMemo.FromId = RouteUserId;
                    Memo o = await biz.CreateAsync(newMemo);
                    if (o == null)
                        return BadRequest(new ApiErrorResponse(biz.Errors));
                }

                //return nothing but ok
                return Accepted();
            }

        }

        public class SendMemo
        {
            public SendMemo()
            {
                Users = new List<long>();
            }
            [Required]
            public Memo Memo { get; set; }

            [Required]
            public List<long> Users { get; set; }
        }
        //------------


        //NO DUPLICATING MEMOS
        // /// <summary>
        // /// Duplicate Memo
        // /// (Wiki and Attachments are not duplicated)
        // /// </summary>
        // /// <param name="id">Source object id</param>
        // /// <param name="apiVersion">From route path</param>
        // /// <returns>Memo</returns>
        // [HttpPost("duplicate/{id}")]
        // public async Task<IActionResult> DuplicateMemo([FromRoute] long id, ApiVersion apiVersion)
        // {
        //     if (!serverState.IsOpen)
        //         return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
        //     MemoBiz biz = MemoBiz.GetBiz(ct, HttpContext);
        //     if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
        //         return StatusCode(403, new ApiNotAuthorizedResponse());
        //     if (!ModelState.IsValid)
        //         return BadRequest(new ApiErrorResponse(ModelState));
        //     Memo o = await biz.DuplicateAsync(id);
        //     if (o == null)
        //         return BadRequest(new ApiErrorResponse(biz.Errors));
        //     else
        //         return CreatedAtAction(nameof(MemoController.GetMemo), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        // }

        /// <summary>
        /// Get Memo
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Memo</returns>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetMemo([FromRoute] long id)
        {
            //NOTE: In this case always getting own memo only
            //also it's always just for read only purposes so it should include from user name
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            MemoBiz biz = MemoBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.GetAsync(id);
            if (o == null) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            var fromUser = await ct.User.AsNoTracking().SingleOrDefaultAsync(z => z.Id == o.FromId);
            var from = "??";
            if (fromUser != null) from = fromUser.Name;
            var ret = new
            {
                Id = o.Id,
                Name = o.Name,
                Notes = o.Notes,
                Wiki = o.Wiki,
                CustomFields = o.CustomFields,
                Tags = o.Tags,
                Viewed = o.Viewed,
                Replied = o.Replied,
                FromId = o.FromId,
                ToId = o.ToId,
                Sent = o.Sent,
                FromName = from
            };
            return Ok(ApiOkResponse.Response(ret));
        }

        //NO UPDATING MEMOS
        // /// <summary>
        // /// Update Memo
        // /// </summary>        
        // /// <param name="updatedObject"></param>
        // /// <returns></returns>
        // [HttpPut]
        // public async Task<IActionResult> PutMemo([FromBody] Memo updatedObject)
        // {
        //     if (!serverState.IsOpen)
        //         return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
        //     if (!ModelState.IsValid)
        //         return BadRequest(new ApiErrorResponse(ModelState));
        //     MemoBiz biz = MemoBiz.GetBiz(ct, HttpContext);
        //     if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
        //         return StatusCode(403, new ApiNotAuthorizedResponse());
        //     var o = await biz.PutAsync(updatedObject); 
        //     if (o == null)
        //     {
        //         if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
        //             return StatusCode(409, new ApiErrorResponse(biz.Errors));
        //         else
        //             return BadRequest(new ApiErrorResponse(biz.Errors));
        //     }
        //     return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));;
        // }

        /// <summary>
        /// Delete Memo
        /// </summary>
        /// <param name="id"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteMemo([FromRoute] long id)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            MemoBiz biz = MemoBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, biz.BizType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.DeleteAsync(id))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }





        //------------


    }//eoc
}//eons