using C1.Web.Mvc;
using C1.Web.Mvc.Serialization;
using DashboardModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;

namespace DashboardDemo.Controllers
{
    public partial class HomeController : Controller
    {
        private static DataService _dataService;
        private static string _dataXmlFolderPath;
        public DataService DataService
        {
            get
            {
                return _dataService ?? (_dataService = DataService.GetService(DataXmlFolderPath));
            }
        }
        public string DataXmlFolderPath
        {
            get
            {
                return _dataXmlFolderPath ?? (_dataXmlFolderPath = System.Web.HttpContext.Current.Server.MapPath("~/Content"));
            }
        }

        private void UpdateDateRange()
        {
            DateRange range = new DateRange();
            range.Start = (DateTime.ParseExact(this.HttpContext.Request["startDate"], "M/d/yyyy", CultureInfo.InvariantCulture));
            range.End = (DateTime.ParseExact(this.HttpContext.Request["endDate"], "M/d/yyyy", CultureInfo.InvariantCulture));
            DataService.DateRange = range;
        }

        public ActionResult Index()
        {
            ViewBag.BudgetItemList = DataService.BudgetItemList;
            ViewBag.DateSaleList = DataService.DateSaleList;
            return View();
        }

        [HttpPost]
        public ActionResult GetTopData()
        {
            UpdateDateRange();

            ArrayList respone = new ArrayList();
            List<SaleItem> topProducts = DataService.GetTopSaleProductList(3);
            respone.Add(CollectionViewHelper.Read<SaleItem>(new CollectionViewRequest<SaleItem>(), topProducts));


            List<SaleItem> topCustomers = DataService.GetTopSaleCustomerList(7);
            respone.Add(CollectionViewHelper.Read<SaleItem>(new CollectionViewRequest<SaleItem>(), topCustomers));

            List<SaleGoalItem> goalItems = DataService.CategorySalesVsGoal;
            respone.Add(CollectionViewHelper.Read<SaleGoalItem>(new CollectionViewRequest<SaleGoalItem>(), goalItems));

            List<SaleGoalItem> regionalGoalItems = DataService.RegionSalesVsGoal;
            respone.Add(CollectionViewHelper.Read<SaleGoalItem>(new CollectionViewRequest<SaleGoalItem>(), regionalGoalItems));

            return this.C1Json(respone);
        }
    }
}
