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

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

    // GET api/<controller>
    public HttpResponseMessage Get()
    {
        var notes = from note in db.Notes
                    join category in db.Folders on note.FolderId equals category.Id
                    select new { note.Id, note.Subject, note.DateCreated, category.Name, note.FolderId };

        return this.Request.CreateResponse(HttpStatusCode.OK, notes);
    }

    // GET api/<controller>/5
    public Note Get(int id)
    {
        Note note = db.Notes.FirstOrDefault(x => x.Id == id);

        if (note == null)
        {
             throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        }

        return note;
    }

    // POST api/<controller>
    public HttpResponseMessage Post([FromBody]Note note)
    {
        note.DateCreated = DateTime.Now;
        if (String.IsNullOrEmpty(note.NoteContent))
        {
            note.NoteContent = "New Note";
        }
        db.Notes.Add(note);
        db.SaveChanges();

        return Request.CreateResponse<Note>(HttpStatusCode.Created, note);
    }

    // PUT api/<controller>/5
    public HttpResponseMessage Put(int id, [FromBody]Note note)
    {
        var noteToUpdate = db.Notes.Find(id);

        if (noteToUpdate != null)
        {
            // Update FolderID only if it is modified
            if (note.FolderId != 0)
            {
                noteToUpdate.FolderId = note.FolderId;
            }
            if (!string.IsNullOrEmpty(note.NoteContent))
            {
                noteToUpdate.NoteContent = note.NoteContent;
            }
            db.SaveChanges();
        }

        return Request.CreateResponse<Note>(HttpStatusCode.NoContent, noteToUpdate);
    }

    // DELETE api/<controller>/5
    public HttpResponseMessage Delete(int id)
    {
        var note = db.Notes.SingleOrDefault(x => x.Id == id);

        if (note != null)
        {
            db.Notes.Remove(note);
            db.SaveChanges();
            return new HttpResponseMessage(HttpStatusCode.OK);
        }

        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }

    [HttpPost]
    [Route("api/notes/search")]
    public SearchBoxData Search([FromBody]JObject context)
    {
        string text = (string)context["context"]["Text"];
        IEnumerable<SearchBoxItemData> filtered = from note in db.Notes
                                                  where note.Subject.StartsWith(text)
                                                  select new SearchBoxItemData
                                                  {
                                                      Text = note.Subject,
                                                      Value = note.Id.ToString()
                                                  };

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

        return res;

    }
}
