using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using FMSAdmin.Data;
using System.Linq;
using FMSAdmin.Helpers;
using FMSAdmin.Entities;
using System.Transactions;
using Microsoft.EntityFrameworkCore;



namespace FMSAdmin.Services {
    public class MonitoringPointService {
        private readonly ILogger<MonitoringPointService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;

        public MonitoringPointService(
            ILogger<MonitoringPointService> logger,
            FMSContext context,
            StorageHelper storage
        ) {
            _logger = logger;
            _context = context;
            _storage = storage;
        }

        public IQueryable<BemsMonitoringPoint> GetAll() {
            var query = _context.BemsMonitoringPoint;//.AsNoTracking();
            return query;


        }

        public IQueryable<BemsMonitoringPoint> Get(int siteId, int facilityCode, int propertyId) {
            var data = _context.BemsMonitoringPoint.Where(x => x.SiteId == siteId && x.FacilityCode == facilityCode && x.PropertyId == propertyId);
            return data;
        }

        public void Create(BemsMonitoringPoint data) {
            using (var trans = new TransactionScope()) {
                checkName(data);

                var persist = _context.BemsMonitoringPoint.Where(x => x.SiteId == data.SiteId && x.FacilityCode == data.FacilityCode).OrderByDescending(x => x.PropertyId).FirstOrDefault();
                if (persist == null) {
                    data.PropertyId = 1;
                } else { data.PropertyId = persist.PropertyId + 1; }
                _context.BemsMonitoringPoint.Add(data);
                _context.SaveChanges();

                trans.Complete();
            }



        }

        public void Edit(int siteId, int facilityCode, int propertyId, BemsMonitoringPoint data) {
            using (var trans = new TransactionScope()) {

                var persist = _context.BemsMonitoringPoint
                            .Where(x => x.SiteId == siteId && x.FacilityCode == facilityCode && x.PropertyId == propertyId).FirstOrDefault();
                if (persist == null) {
                    throw new ServiceException("정보를 찾을 수 없습니다.");
                }

                if (!persist.Name.Equals(data.Name)) {
                    checkName(data);
                }
                _logger.LogInformation("수정");
                persist.Name = data.Name;
                persist.ValueType = data.ValueType;
                persist.ServiceTypeId = data.ServiceTypeId;
                persist.FuelTypeId = data.FuelTypeId;
                persist.Description = data.Description;

                _context.BemsMonitoringPoint.Update(persist);
                _context.SaveChanges();


                trans.Complete();
            }


        }

        public void checkName(BemsMonitoringPoint data) {
            var check = _context.BemsMonitoringPoint.Where(x => x.SiteId == data.SiteId && x.FacilityCode == data.FacilityCode && x.Name == data.Name).Count();
            if (check > 0) {
                throw new ServiceException("이미 동일 이름이 존재합니다.");
            }
        }

        public void Delete(List<BemsMonitoringPoint> data) {
            using (var trans = new TransactionScope()) {


                foreach (var point in data) {
                    var persist = _context.BemsMonitoringPoint.First(x => x.SiteId == point.SiteId && x.FacilityCode == point.FacilityCode && x.PropertyId == point.PropertyId);
                    _context.BemsMonitoringPoint.Remove(persist);
                }

                _context.SaveChanges();
                trans.Complete();
            }
        }

        public void Deletes(int siteId, int facilityCode) {
            using (var trans = new TransactionScope()) {
                var data = _context.BemsMonitoringPoint.Where(x => x.SiteId == siteId && x.FacilityCode == facilityCode);
                _context.BemsMonitoringPoint.RemoveRange(data);
                _context.SaveChanges();
                trans.Complete();
            }
        }

        public void CreateAll(int siteId, int facilityTypeId, int facilityCode) {
            using (var trans = new TransactionScope()) {
                var list = _context.BemsMonitoringPointBaseData.Where(x => x.FacilityTypeId == facilityTypeId);

                foreach (var persist in list) {
                    BemsMonitoringPoint data = new BemsMonitoringPoint();

                    data.SiteId = siteId;
                    data.FacilityTypeId = facilityTypeId;
                    data.FacilityCode = facilityCode;
                    data.PropertyId = persist.PropertyId;
                    data.ValueType = persist.ValueType;
                    data.ServiceTypeId = persist.ServiceTypeId;
                    data.FuelTypeId = persist.FuelTypeId;
                    data.Name = persist.Name;

                    _context.BemsMonitoringPoint.Add(data);
                }
                _context.SaveChanges();

                trans.Complete();
            }
        }
    }
}