using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;//required for authorize attribute
using System.Security.Claims;
using rockfishCore.Models;
using rockfishCore.Util;
using System.Linq;
using System;

//requried to inject configuration in constructor
using Microsoft.Extensions.Configuration;



namespace rockfishCore.Controllers
{
    //Authentication controller
    [Produces("application/json")]
    [Route("api/Mail")]
    [Authorize]
    public class MailController : Controller
    {
        private readonly rockfishContext _context;
        private readonly IConfiguration _configuration;
        public MailController(rockfishContext context, IConfiguration configuration)
        {
            _context = context;
            _configuration = configuration;
        }

        //Fetch inbox emails from sales and support
        [HttpGet("salesandsupportsummaries")]
        public JsonResult GetSalesAndSupportSummaries()
        {
            return Json(Util.RfMail.GetSalesAndSupportSummaries());
        }

        //Fetch a preview of a message
        [HttpGet("preview/{account}/{folder}/{id}")]
        public JsonResult GetPreview([FromRoute] string account, [FromRoute] string folder, [FromRoute] uint id)
        {
            return new JsonResult(Util.RfMail.GetMessagePreview(account, folder, id));
            //return Json(new { message = Util.RfMail.GetMessagePreview(account, folder, id) });
        }


        [HttpPost("reply/{account}/{id}")]
        public JsonResult Reply([FromRoute] string account, [FromRoute] uint id, [FromBody] dtoReplyMessageItem m)
        {
            if (string.IsNullOrWhiteSpace(m.composition))
            {
                return Json(new { msg = "MailController:Reply->There is no reply text", error = 1 });
            }
            RfMail.rfMailAccount acct = RfMail.rfMailAccount.support;
            if (account.Contains("sales"))
            {
                acct = RfMail.rfMailAccount.sales;
            }

            try
            {
                RfMail.ReplyMessage(id, acct, m.composition, true, m.trackDelivery);
                return Json(new { msg = "message sent", ok = 1 });
            }
            catch (Exception ex)
            {
                return Json(new { msg = ex.Message, error = 1 });
            }
        }


         [HttpPost("isspam/{account}/{id}")]
        public JsonResult IsSpam([FromRoute] string account, [FromRoute] uint id)
        {
           
            RfMail.rfMailAccount acct = RfMail.rfMailAccount.support;
            if (account.Contains("sales"))
            {
                acct = RfMail.rfMailAccount.sales;
            }

            try
            {
                RfMail.HandleSpamMessage(id, acct);
                return Json(new { msg = "message processed as spam", ok = 1 });
            }
            catch (Exception ex)
            {
                return Json(new { msg = ex.Message, error = 1 });
            }
        }
        // //------------------------------------------------------

        public class dtoReplyMessageItem
        {
            public string composition;
            public bool trackDelivery;

        }

    }//eoc
}//eons
