using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json.Linq;
using WebMail.Model;
using Telerik.Web.UI;

public class MessagesController : ApiController
{
    private WebMailContext db = new WebMailContext();

    private enum OutlookAppSystemFolders
    {
        Inbox = 1,
        Junk = 2,
        Drafts = 3,
        Deleted = 4
    }

    [HttpGet]
    [Route("api/messages/")]
    public IEnumerable<Message> Get()
    {
        return db.Messages.ToList();
    }

    // GET api/<controller>/5
    public Message Get(int id)
    {
        var message = db.Messages.Find(id);
        return message;
    }

    [HttpPost]
    [Route("api/messages/{id}/inbox")]
    public HttpResponseMessage Inbox(int id)
    {
        Message message = db.Messages.Find(id);

        if (message != null)
        {
            // 1 is ID of "Inbox" folder in database. This ID can be retrieved with additional LINQ query
            message.FolderId = (int)OutlookAppSystemFolders.Inbox;
            db.SaveChanges();
            return new HttpResponseMessage(HttpStatusCode.OK);
        }

        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }

    [HttpPost]
    [Route("api/messages/{id}/mark")]
    public HttpResponseMessage Mark(int id, bool read)
    {
        Message message = db.Messages.Find(id);

        if (message != null)
        {
            message.IsRead = read;
            db.SaveChanges();
        }

        return Request.CreateResponse<Message>(HttpStatusCode.NoContent, message);
    }

    [HttpPost]
    [Route("api/messages/{id}/spam")]
    public HttpResponseMessage Spam(int id)
    {
        Message message = db.Messages.Find(id);

        if (message != null)
        {
            // 2 is ID of "Junk" folder in database. This ID can be retrieved with additional LINQ query
            message.FolderId = (int)OutlookAppSystemFolders.Junk;
            db.SaveChanges();
            return new HttpResponseMessage(HttpStatusCode.OK);
        }

        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }

    [HttpPost]
    [Route("api/messages/{id}/drafts")]
    public HttpResponseMessage Drafts(int id)
    {
        Message message = db.Messages.Find(id);

        if (message != null)
        {
            // 3 is ID of "Drafts" folder in database. This ID can be retrieved with additional LINQ query
            message.FolderId = (int)OutlookAppSystemFolders.Drafts;
            db.SaveChanges();
            return new HttpResponseMessage(HttpStatusCode.OK);
        }

        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }


    // POST api/<controller>
    public void Post([FromBody]string value)
    {
    }

    // PUT api/<controller>/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/<controller>/5
    public HttpResponseMessage Delete(int id)
    {
        var message = db.Messages.SingleOrDefault(x => x.MessageID == id);

        if (message != null)
        {
            // 4 is ID of "Deleted" folder in database. This ID can be retrieved with additional LINQ query
            message.FolderId = (int)OutlookAppSystemFolders.Deleted;
            db.SaveChanges();
            return new HttpResponseMessage(HttpStatusCode.OK);
        }

        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }

    [HttpPost]
    [Route("api/messages/search")]
    public SearchBoxData Search([FromBody]JObject context)
    {
        string text = (string)context["context"]["Text"];
        IEnumerable<SearchBoxItemData> filtered = from message in db.Messages
                                                  where message.Body.Contains(text)
                                                  select new SearchBoxItemData
                                                  {
                                                      Text = message.Subject,
                                                      Value = message.MessageID.ToString()
                                                  };

        SearchBoxData res = new SearchBoxData();
        res.Items = filtered.ToArray();

        return res;

    }
}
