using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CyriousInternalDatasource.Models;
using System.Configuration;

namespace CyriousInternalDatasource.Controllers
{
    public class ValuesController : ApiController
    {
        public class DataResponse
        {
            public IQueryable data;
            public int count;
        }

        // GET api/values
        public HttpResponseMessage Get(int skip = 0, int take = 50, string sortField = "OrderNumber", bool ordByDesc = false)
        {
            /// Go get the connection string from the web.config
            ConnectionStringSettings dbConnectionStringSettings = ConfigurationManager.ConnectionStrings["KSRUserWebsiteConnectionString"];

            DataContext db = new DataContext(dbConnectionStringSettings.ConnectionString);
            
            Table<OrderStatus> OrderStatuses = db.GetTable<OrderStatus>();
            IQueryable<OrderStatus> orderQuery;
            
            //if/else statement for if you're descending or ascending
            if (ordByDesc)
            {
                //switch case for each heading, and for assigning api/values
                switch (sortField)
                {
                    case "OrderStation": orderQuery =
                     from order in OrderStatuses.OrderByDescending(os => os.OrderStation).Skip(skip).Take(take)
                     select order;
                        break;
                    case "CompanyName": orderQuery =
                      from order in OrderStatuses.OrderByDescending(os => os.CompanyName).Skip(skip).Take(take)
                      select order;
                        break;
                    case "Contact": orderQuery =
                      from order in OrderStatuses.OrderByDescending(os => os.Contact).Skip(skip).Take(take)
                      select order;
                        break;
                    case "Description": orderQuery =
                      from order in OrderStatuses.OrderByDescending(os => os.Description).Skip(skip).Take(take)
                      select order;
                        break;
                    case "Status": orderQuery =
                      from order in OrderStatuses.OrderByDescending(os => os.Status).Skip(skip).Take(take)
                      select order;
                        break;
                    case "OrderNumber": orderQuery =
                      from order in OrderStatuses.OrderByDescending(os => os.OrderNumber).Skip(skip).Take(take)
                      select order;
                        break;
                    case "Notes": orderQuery =
                      from order in OrderStatuses.OrderByDescending(os => os.OrderNotes).Skip(skip).Take(take)
                      select order;
                        break;
                    case "Station_at_Sale": orderQuery =
                      from order in OrderStatuses.OrderByDescending(os => os.Station_at_Sale).Skip(skip).Take(take)
                      select order;
                        break;
                    default:
                        orderQuery =
                      from order in OrderStatuses.OrderByDescending(os => os.OrderNumber).Skip(skip).Take(take)
                      select order;
                        break;
                }
            }
            else
            {
                switch (sortField)
                {
                    case "OrderStation": orderQuery =
                     from order in OrderStatuses.OrderBy(os => os.OrderStation).Skip(skip).Take(take)
                     select order;
                        break;
                    case "CompanyName": orderQuery =
                      from order in OrderStatuses.OrderBy(os => os.CompanyName).Skip(skip).Take(take)
                      select order;
                        break;
                    case "Contact": orderQuery =
                      from order in OrderStatuses.OrderBy(os => os.Contact).Skip(skip).Take(take)
                      select order;
                        break;
                    case "Description": orderQuery =
                      from order in OrderStatuses.OrderBy(os => os.Description).Skip(skip).Take(take)
                      select order;
                        break;
                    case "Status": orderQuery =
                      from order in OrderStatuses.OrderBy(os => os.Status).Skip(skip).Take(take)
                      select order;
                        break;
                    case "OrderNumber": orderQuery =
                      from order in OrderStatuses.OrderBy(os => os.OrderNumber).Skip(skip).Take(take)
                      select order;
                        break;
                    case "Notes": orderQuery =
                      from order in OrderStatuses.OrderBy(os => os.OrderNotes).Skip(skip).Take(take)
                      select order;
                        break;
                    case "Station_at_Sale": orderQuery =
                      from order in OrderStatuses.OrderBy(os => os.Station_at_Sale).Skip(skip).Take(take)
                      select order;
                        break;
                    default:
                        orderQuery =
                      from order in OrderStatuses.OrderBy(os => os.OrderNumber).Skip(skip).Take(take)
                      select order;
                        break;
                }
            }

            return Request.CreateResponse(new DataResponse { data = orderQuery, count = OrderStatuses.Count() });
        }
    }
}