using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Http.Description;
using DevExpress.Office.Utils;
using iBemsDataService.Model;
using iBemsDataService.Util;
using System.Transactions;
using System.Diagnostics;

namespace iBemsDataService.Controllers
{

    public class BemsEnergyDailyController : ApiController
    {
        private iBemsEntities db = new iBemsEntities();


        [ResponseType(typeof(void))]
        [ActionName("InsertPart")]
        public IHttpActionResult PostBemsEnergyDailyInsert(EnergyDaily EnergyDaily)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                // 그룹을 추가하면서 메뉴들을 그룹에 맞는 권한 테이블로 모두 옮겨 놓는다.
                using (var transaction = new TransactionScope())
                {
                    var newPriceFormula = db.BemsEnergyDaily.Add(new BemsEnergyDaily
                    {
                        SiteId = EnergyDaily.SiteId,
                        BuildingId = EnergyDaily.BuildingId,
                        ServiceTypeId = EnergyDaily.ServiceTypeId,
                        FuelTypeId = EnergyDaily.FuelTypeId,
                        CreatedDate = EnergyDaily.CreatedDate,
                        Prediction = Math.Round(EnergyDaily.Prediction * 100) / 100.0,
                    });
                    db.SaveChanges();
                    transaction.Complete();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return StatusCode(HttpStatusCode.NoContent);
        }

        [ResponseType(typeof(void))]
        [ActionName("InsertAll")]
        public IHttpActionResult PostBemsPriceFormulaInsertAll(List<EnergyDaily> EnergyDaily)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                using (var transaction = new TransactionScope())
                {
                    foreach (var m in EnergyDaily)
                    {
                        var foundQuery = db.BemsEnergyDaily.Where(
                            x =>
                                x.SiteId == m.SiteId &&
                                x.BuildingId == m.BuildingId &&
                                x.ServiceTypeId == m.ServiceTypeId &&
                                x.FuelTypeId == m.FuelTypeId &&
                                x.CreatedDate == m.CreatedDate);

                        if (foundQuery.Any() == false)
                        {
                            var newM = db.BemsEnergyDaily.Add(new BemsEnergyDaily
                            {
                                SiteId = m.SiteId,
                                BuildingId = m.BuildingId,
                                ServiceTypeId = m.ServiceTypeId,
                                FuelTypeId = m.FuelTypeId,
                                CreatedDate = m.CreatedDate,
                                Prediction = m.Prediction,
                                Measurement = m.Measurement,
                                Goal = m.Goal,
                                Prediction_Regression = m.Prediction_Regression,
                                MeanT = m.MeanT,
                                HDD = m.HDD,
                                CDD = m.CDD
                            });
                        }
                    }
                    db.SaveChanges();
                    transaction.Complete();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return StatusCode(HttpStatusCode.NoContent);
        }

        [ResponseType(typeof(void))]
        [ActionName("DeleteAll")]
        public IHttpActionResult PostBemsPriceFormulaDeleteAll(EnergyDaily EnergyDaily)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                using (var transaction = new TransactionScope())
                {
                    var foundQuery = db.BemsEnergyDaily.Where(
                        x =>
                             x.SiteId == EnergyDaily.SiteId &&
                                x.BuildingId == EnergyDaily.BuildingId &&
                                x.ServiceTypeId == EnergyDaily.ServiceTypeId &&
                                x.FuelTypeId == EnergyDaily.FuelTypeId &&
                                x.CreatedDate == EnergyDaily.CreatedDate);

                    //db.BemsPriceFormula.Remove(foundQuery.FirstOrDefault()); 한개만 지운다
                    new QueryManager<Model.BemsEnergyDaily>(db).Delete(db.BemsEnergyDaily, foundQuery);
                    db.SaveChanges();

                    transaction.Complete();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return StatusCode(HttpStatusCode.NoContent);
        }

        [ResponseType(typeof(void))]
        [ActionName("Update")]
        public IHttpActionResult PostBemsEnergyDailyUpdate(EnergyDaily EnergyDaily)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                using (var transaction = new TransactionScope())
                {
                    var foundQuery = db.BemsEnergyDaily.Where(
                        x =>
                        x.SiteId == EnergyDaily.SiteId &&
                        x.BuildingId == EnergyDaily.BuildingId &&
                        x.ServiceTypeId == EnergyDaily.ServiceTypeId &&
                        x.FuelTypeId == EnergyDaily.FuelTypeId &&
                        x.CreatedDate == EnergyDaily.CreatedDate
                        );
                    new QueryManager<BemsEnergyDaily>(db).Delete(db.BemsEnergyDaily, foundQuery);

                    var newPriceFormula = db.BemsEnergyDaily.Add(new BemsEnergyDaily
                    {
                        SiteId = EnergyDaily.SiteId,
                        BuildingId = EnergyDaily.BuildingId,
                        ServiceTypeId = EnergyDaily.ServiceTypeId,
                        FuelTypeId = EnergyDaily.FuelTypeId,
                        CreatedDate = EnergyDaily.CreatedDate,
                        Prediction = EnergyDaily.Prediction,
                        Measurement = EnergyDaily.Measurement,
                        Goal = EnergyDaily.Goal,
                        Prediction_Regression = EnergyDaily.Prediction_Regression,
                        MeanT = EnergyDaily.MeanT,
                        HDD = EnergyDaily.HDD,
                        CDD = EnergyDaily.CDD
                    });
                    db.SaveChanges();
                    transaction.Complete();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return StatusCode(HttpStatusCode.NoContent);
        }

        [ResponseType(typeof(void))]
        [ActionName("UpdatePart")]
        public IHttpActionResult PostBemsEnergyDailyUpdatePart(EnergyDaily EnergyDaily)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                using (var transaction = new TransactionScope())
                {
                    var foundQuery = db.BemsEnergyDaily
                   .Where(x =>
                     x.SiteId == EnergyDaily.SiteId &&
                     x.BuildingId == EnergyDaily.BuildingId &&
                     x.ServiceTypeId == EnergyDaily.ServiceTypeId &&
                     x.FuelTypeId == EnergyDaily.FuelTypeId &&
                     x.CreatedDate == EnergyDaily.CreatedDate);

                    var tempvalue = foundQuery.FirstOrDefault();
                    new QueryManager<Model.BemsEnergyDaily>(db).Delete(db.BemsEnergyDaily, foundQuery);
                    var newPriceFormula = db.BemsEnergyDaily.Add(new BemsEnergyDaily
                    {
                        SiteId = EnergyDaily.SiteId,
                        BuildingId = EnergyDaily.BuildingId,
                        ServiceTypeId = EnergyDaily.ServiceTypeId,
                        FuelTypeId = EnergyDaily.FuelTypeId,
                        CreatedDate = EnergyDaily.CreatedDate,
                        Prediction = Math.Round(EnergyDaily.Prediction * 100) / 100.0,
                        Measurement = tempvalue.Measurement,
                        Goal = tempvalue.Goal,
                        Prediction_Regression = tempvalue.Prediction_Regression,
                        MeanT = tempvalue.MeanT,
                        HDD = tempvalue.HDD,
                        CDD = tempvalue.CDD
                    });
                    db.SaveChanges();
                    transaction.Complete();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return StatusCode(HttpStatusCode.NoContent);
        }
        [ResponseType(typeof(void))]
        [ActionName("UpdatePart2")]
        public IHttpActionResult PostBemsEnergyDailyUpdatePart2(EnergyDaily EnergyDaily)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                using (var transaction = new TransactionScope())
                {
                    var foundQuery = db.BemsEnergyDaily
                   .Where(x =>
                     x.SiteId == EnergyDaily.SiteId &&
                     x.BuildingId == EnergyDaily.BuildingId &&
                     x.ServiceTypeId == EnergyDaily.ServiceTypeId &&
                     x.FuelTypeId == EnergyDaily.FuelTypeId &&
                     x.CreatedDate == EnergyDaily.CreatedDate);

                    var tempvalue = foundQuery.FirstOrDefault();
                    new QueryManager<Model.BemsEnergyDaily>(db).Delete(db.BemsEnergyDaily, foundQuery);
                    var newPriceFormula = db.BemsEnergyDaily.Add(new BemsEnergyDaily
                    {
                        SiteId = EnergyDaily.SiteId,
                        BuildingId = EnergyDaily.BuildingId,
                        ServiceTypeId = EnergyDaily.ServiceTypeId,
                        FuelTypeId = EnergyDaily.FuelTypeId,
                        CreatedDate = EnergyDaily.CreatedDate,
                        Prediction = Math.Round(EnergyDaily.Prediction * 100) / 100.0,
                        Measurement = tempvalue.Measurement,
                        Goal = Math.Round(EnergyDaily.Goal * 100) / 100.0,
                        Prediction_Regression = tempvalue.Prediction_Regression,
                        MeanT = tempvalue.MeanT,
                        HDD = tempvalue.HDD,
                        CDD = tempvalue.CDD
                    });
                    db.SaveChanges();
                    transaction.Complete();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return StatusCode(HttpStatusCode.NoContent);
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        public class EnergyDaily
        {
            public int SiteId { get; set; }
            public int BuildingId { get; set; }
            public Int16 ServiceTypeId { get; set; }
            public Int16 FuelTypeId { get; set; }
            public DateTime CreatedDate { get; set; }
            public float Prediction { get; set; }
            public float Measurement { get; set; }
            public float Goal { get; set; }
            public float Prediction_Regression { get; set; }
            public float MeanT { get; set; }
            public float HDD { get; set; }
            public float CDD { get; set; }
        };
    }
}