using Kendo.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using KendoWithChart.Models;
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;

namespace KendoWithChart.Controllers
{
    public class HomeController : Controller
    {
        public IEnumerable<FundAdminPositionDailyView> FundAdminPositionDailyData;


        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index([DataSourceRequest]DataSourceRequest request)
        {
            // get the data for the page
            FundAdminPositionDailyData = GetFundAdminPosition(request);
            ViewBag.FundAdminPositionDailyView = FundAdminPositionDailyData;

            // return the model to the page
            var viewModel = new FundAdminPositionViewModel
            {
                FundAdminPositionDailyChart = GetChartData(FundAdminPositionDailyData)
            };

            return View(viewModel);
        }


        /// <summary>
        /// Gets the chart data.
        /// </summary>
        /// <param name="objFundAdminPositionDailyData">The object fund admin position daily data.</param>
        /// <returns></returns>
        private IEnumerable<FundAdminPositionDailyChart> GetChartData(IEnumerable<FundAdminPositionDailyView> objFundAdminPositionDailyData)
        {
            bool bitChartSet = false;

            List<FundAdminPositionDailyChart> objFundAdminPositionDailyChart = new List<FundAdminPositionDailyChart>();

            foreach (var objRow in objFundAdminPositionDailyData)
            {
                if (!bitChartSet)
                {
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "", TotalAmt = objRow.NULL });

                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_BND", TotalAmt = objRow.MA_BND });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_CDX", TotalAmt = objRow.MA_CDX });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_CRD", TotalAmt = objRow.MA_CRD });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_EEM", TotalAmt = objRow.MA_EEM });

                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_EUR", TotalAmt = objRow.MA_EUR });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_GLD", TotalAmt = objRow.MA_GLD });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_JPY", TotalAmt = objRow.MA_JPY });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_RUS", TotalAmt = objRow.MA_RUS });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_SLV", TotalAmt = objRow.MA_SLV });

                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_SOY", TotalAmt = objRow.MA_SOY });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_SPX", TotalAmt = objRow.MA_SPX });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_T10", TotalAmt = objRow.MA_T10 });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_T30", TotalAmt = objRow.MA_T30 });
                    objFundAdminPositionDailyChart.Add(new FundAdminPositionDailyChart { StrategyCode = "MA_TBL", TotalAmt = objRow.MA_TBL });

                    bitChartSet = true;
                }

            }

            return objFundAdminPositionDailyChart.AsQueryable();
        }


        private IEnumerable<FundAdminPositionDailyView> GetFundAdminPosition([DataSourceRequest]DataSourceRequest request)
        {
            // default the grid
            string strSortField = "CLOSE_OF_BUSINESS_DATE";
            string strSortDir = "desc";

            string strGridSort = Request.QueryString["grid-sort"];
            string strGridPage = Request.QueryString["grid-page"];
            string[] sortArray;

            if (strGridSort != null && strGridSort != "")
            {
                sortArray = strGridSort.Split('-');
                strSortField = sortArray[0];
                strSortDir = sortArray[1];
            }


            if (strGridPage == null || strGridPage == "")
                strGridPage = "1";

            if (request.PageSize == 0)
            {
                request.PageSize = 10;
            }


            // get the data from the database
            using (FM_Entities objFM_Entities = new FM_Entities())
            {
                var dataSet = (from x in objFM_Entities.FUND_ADMIN_POSITION
                               group x by new { x.CLOSE_OF_BUSINESS_DATE, x.STRATEGY_CODE } into y
                               select new
                               {
                                   CloseOfBusinessDate = y.Key.CLOSE_OF_BUSINESS_DATE,
                                   StrategyCode = y.Key.STRATEGY_CODE,
                                   DailyTotalPNL = y.Sum(z => z.DAILY_TOTAL_PNL)
                               }).OrderByDescending(group => group.CloseOfBusinessDate)
                               .GroupBy(c => c.CloseOfBusinessDate)
                               .Select(g => new FundAdminPositionDailyView()
                               {
                                   CLOSE_OF_BUSINESS_DATE = g.Key,
                                   NULL = g.Where(c => c.StrategyCode == null).Sum(c => c.DailyTotalPNL),
                                   MA_BND = g.Where(c => c.StrategyCode == "MA_BND").Sum(c => c.DailyTotalPNL),
                                   MA_CDX = g.Where(c => c.StrategyCode == "MA_CDX").Sum(c => c.DailyTotalPNL),
                                   MA_CRD = g.Where(c => c.StrategyCode == "MA_CRD").Sum(c => c.DailyTotalPNL),
                                   MA_EEM = g.Where(c => c.StrategyCode == "MA_EEM").Sum(c => c.DailyTotalPNL),
                                   MA_EUR = g.Where(c => c.StrategyCode == "MA_EUR").Sum(c => c.DailyTotalPNL),
                                   MA_GLD = g.Where(c => c.StrategyCode == "MA_GLD").Sum(c => c.DailyTotalPNL),
                                   MA_JPY = g.Where(c => c.StrategyCode == "MA_JPY").Sum(c => c.DailyTotalPNL),
                                   MA_RUS = g.Where(c => c.StrategyCode == "MA_RUS").Sum(c => c.DailyTotalPNL),
                                   MA_SLV = g.Where(c => c.StrategyCode == "MA_SLV").Sum(c => c.DailyTotalPNL),
                                   MA_SOY = g.Where(c => c.StrategyCode == "MA_SOY").Sum(c => c.DailyTotalPNL),
                                   MA_SPX = g.Where(c => c.StrategyCode == "MA_SPX").Sum(c => c.DailyTotalPNL),
                                   MA_T10 = g.Where(c => c.StrategyCode == "MA_T10").Sum(c => c.DailyTotalPNL),
                                   MA_T30 = g.Where(c => c.StrategyCode == "MA_T30").Sum(c => c.DailyTotalPNL),
                                   MA_TBL = g.Where(c => c.StrategyCode == "MA_TBL").Sum(c => c.DailyTotalPNL),
                               });

                IQueryable<FundAdminPositionDailyView> pageDate = dataSet
                                                                  .OrderByDescending(group => group.CLOSE_OF_BUSINESS_DATE);

                if (request.PageSize != 0)
                {
                    pageDate = pageDate
                               .Skip((Convert.ToInt32(strGridPage) - 1) * request.PageSize)
                               .Take(request.PageSize);
                }

                ViewData["total"] = dataSet.Count();

                return pageDate.ToList();
            }

        }


        
    }
}
