using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebMail.Model;
using WebMail.Model.DTO;

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

    [Route("api/folders/Mail")]
    public IEnumerable<FolderDTO> GetMailFolders()
    {
        IEnumerable<FolderDTO> folders = from folder in db.Folders
                                         where folder.Category == "Mail"
                                         let children = from child in db.Folders
                                                        where child.ParentId == folder.Id
                                                        select child
                                         select new FolderDTO
                                         {
                                             ParentId = folder.ParentId,
                                             Name = folder.Name + " (" + db.Messages.Count(m => m.FolderId == folder.Id) + ")",
                                             Id = folder.Id,
                                             Attributes = folder.Id.ToString() + "|" + (children.Count() > 0).ToString()
                                         };

        return folders;
    }

    [Route("api/folders/Contacts")]
    public IEnumerable<FolderDTO> GetContactsFolders()
    {
        IEnumerable<FolderDTO> folders = from folder in db.Folders
                                         where folder.Category == "Contacts"
                                         let children = from child in db.Folders
                                                        where child.ParentId == folder.Id
                                                        select child
                                         select new FolderDTO
                                         {
                                             ParentId = folder.ParentId,
                                             Name = folder.Name + " (" + db.Contacts.Count(m => m.FolderId == folder.Id) + ")",
                                             Id = folder.Id,
                                             Attributes = folder.Id.ToString() + "|" + (children.Count() > 0).ToString()
                                         };

        return folders;
    }

    [Route("api/folders/Notes")]
    public IEnumerable<FolderDTO> GetNotesFolders()
    {
        IEnumerable<FolderDTO> folders = from folder in db.Folders
                                         where folder.Category == "Notes"
                                         let children = from child in db.Folders
                                                        where child.ParentId == folder.Id
                                                        select child
                                         select new FolderDTO
                                         {
                                             ParentId = folder.ParentId,
                                             Name = folder.Name + " (" + db.Notes.Count(m => m.FolderId == folder.Id) + ")",
                                             Id = folder.Id,
                                             Attributes = folder.Id.ToString() + "|" + (children.Count() > 0).ToString()
                                         };

        return folders;
    }

    [Route("api/folders/Calendar")]
    public IEnumerable<FolderDTO> GetCalendarFolders()
    {
        IEnumerable<FolderDTO> folders = from folder in db.Folders
                                         where folder.Category == "Calendar"
                                         let children = from child in db.Folders
                                                        where child.ParentId == folder.Id
                                                        select child
                                         select new FolderDTO
                                         {
                                             ParentId = folder.ParentId,
                                             Name = folder.Name,
                                             Id = folder.Id,
                                             Attributes = folder.Id.ToString() + "|" + (children.Count() > 0).ToString()
                                         };

        return folders;
    }

    public HttpResponseMessage Post([FromBody]FolderDTO folderDTO)
    {
        Folder folder = new Folder()
        {
            Name = folderDTO.Name,
            Category = folderDTO.Attributes,
            ParentId = folderDTO.ParentId
        };

        db.Folders.Add(folder);
        db.SaveChanges();

        return Request.CreateResponse<Folder>(HttpStatusCode.Created, folder);
    }

    public HttpResponseMessage Put(int id, [FromBody]FolderDTO folderDTO)
    {
        var folder = db.Folders.Find(id);

        if (folder != null)
        {
            db.Entry(folder).CurrentValues.SetValues(folderDTO);
            db.SaveChanges();
        }

        return Request.CreateResponse<Folder>(HttpStatusCode.NoContent, folder);
    }

    public HttpResponseMessage Delete(int id)
    {
        var folder = db.Folders.SingleOrDefault(x => x.Id == id);

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

        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }
}