using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using System.Web.Http.OData;
using iBemsDataService;
using iBemsDataService.Model;

namespace iBemsDataService.Controllers
{
    public class CmPatrolHistoryExController : ODataController
    {
        private iBemsEntities db = new iBemsEntities();

        // GET: odata/CmPatrolHistoryEx
        [Queryable]
        public List<CmPatrolHistoryEx> GetCmPatrolHistoryEx()
        {
            var groupQuery =
                from m in db.CmPatrolHistory
                group m by new
                {
                    SiteId = m.SiteId,
                    Year = m.startDate.Year,
                    Month = m.startDate.Month,
                    Day = m.startDate.Day
                }
                into g
                select g;

            List<CmPatrolHistoryEx> historyEx = new List<CmPatrolHistoryEx>();

            if (groupQuery == null || groupQuery.Any() == false)
            {
                return historyEx;
            }

            //CalculationValue[] values = new CalculationValue[groupQuery.Count()];
            foreach (var g in groupQuery)
            {
                DateTime find = new DateTime(g.Key.Year, g.Key.Month, g.Key.Day, 0, 0, 0);
                DateTime findE = new DateTime(g.Key.Year, g.Key.Month, g.Key.Day, 23, 59, 59);
                IEnumerable<CmPatrolHistory> pHistroy = from x in db.CmPatrolHistory
                                                        where x.SiteId == g.Key.SiteId && x.startDate >= find && x.startDate <= findE && x.endDate != null
                                                        select x;

                if ((pHistroy != null) && (pHistroy.Count() > 0))
                {
                    CmPatrolHistoryEx ex = new CmPatrolHistoryEx();
                    TimeSpan sp = new TimeSpan(0);
                    foreach (CmPatrolHistory h in pHistroy)
                    {
                        ex.TotalCnt++;
                        //if (h.resultTypeId != 1) ex.ErrorCnt++;
                        if (h.resultAbnormalCnt > 0) ex.ErrorCnt++;
                        if (h.endDate != null)
                            sp += ((DateTime)h.endDate).Subtract(h.startDate);
                        ex.PatrolHistoryId = h.PatrolHistoryId; // 의미없음, 순번을위해 같은날짜중 마지막 id가설정되므로 문제없음
                    }
                    ex.OkCnt = ex.TotalCnt - ex.ErrorCnt;
                    ex.TotalTimeMin = (int)sp.TotalMinutes;
                    ex.SiteId = g.Key.SiteId;
                    ex.startDate = find;

                    historyEx.Add(ex);
                }
                /*{
                    DateTime = new DateTime(g.Key.Year, g.Key.Month, 1),
                    //Value = g.Sum(x => x.CurrentValue)
                    Value = g.Sum(x => x.DailyValue)
                };
                i++; */
            }
            /*
                            select new CmPatrolHistoryEx
                            {
                                SiteId = m.SiteId,
                                PatrolHistoryId = m.PatrolHistoryId,
                                PlanId = m.PlanId,
                                startDate = m.startDate,
                                endDate = m.endDate,
                                resultTypeId = m.resultTypeId,
                                resultDesc = m.resultDesc,

                                TotalCnt = 100,
                                OkCnt = 100,
                                ErrorCnt = 100,
                                TotalTimeMin = 10,
                            }
                        ).ToList(); */
            /*
                        var historyAllList = GetCmPatrolHistoryStockHistory();

                        foreach (var material in mterials)
                        {
                            var temp = historyAllList.Where(m => m.MaterialId == material.MaterialId).FirstOrDefault();

                            if (temp != null)
                            {
                                material.StockCount = temp.StockCount;
                                material.StockAmount = temp.StockAmount;
                                //hcLee 2015 11 11
                                material.WarehouseId = temp.WarehouseId;
                                material.WHouseName = GetWHouseName(temp.WarehouseId);

                                if (material.ReasonableStockCount == null) material.NeedCount = 0;
                                else material.NeedCount = (int)material.ReasonableStockCount - material.StockCount;
                            }
                        }
                        */
            #region === Obslote ===
            #endregion

            //return history.ToList();
            return historyEx;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}