using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;

using FMSAdmin.Data;
using FMSAdmin.Helpers;
using FMSAdmin.Entities;
using Microsoft.Extensions.Logging;
using FMSAdmin.Models;
using System.Linq.Expressions;
using LinqKit;

namespace FMSApp.Services {
    public class AlarmPointService {
        private readonly ILogger<AlarmPointService> _logger;
        private readonly FMSContext _context;

        public AlarmPointService(
            ILogger<AlarmPointService> logger,
            FMSContext context) {
            _logger = logger;
            _context = context;
        }

        public void Create(FmsAlarmPoint data) {
            _context.FmsAlarmPoint.Add(data);
            _context.SaveChanges();
        }

        public IQueryable<FmsAlarmPoint> GetAll() {
            var query = _context.FmsAlarmPoint;
            return query;
        }

        public FmsAlarmPoint Get(int siteId, int facilityTypeId, int facilityCode, int propertyId) {
            var data = _context.FmsAlarmPoint
                            .Where(x => x.SiteId == siteId
                                && x.FacilityTypeId == facilityTypeId
                                && x.FacilityCode == facilityCode
                                && x.PropertyId == propertyId).FirstOrDefault();
            return data;
        }

        public IQueryable<FmsAlarmPoint> Gets(int id, int siteId) {
            var data = _context.FmsAlarmPoint.Where(x => x.SiteId == siteId);
            return data;
        }

        public void Delete(int siteId, int facilityTypeId, int facilityCode, int propertyId) {
            var data = _context.FmsAlarmPoint.First(x => x.SiteId == siteId
                                && x.FacilityTypeId == facilityTypeId
                                && x.FacilityCode == facilityCode
                                && x.PropertyId == propertyId);
            _context.FmsAlarmPoint.Remove(data);
            _context.SaveChanges();
        }

        public IQueryable<BemsMonitoringPointHistory15min> GetPointHistoryList(int siteId) {
            var query =
                from x in _context.BemsMonitoringPointHistory15min
                where x.FmsAlarmPoint.SiteId == siteId
                select x;
            return query;
        }

        public IQueryable<FmsAlarmLog> GetAlarmLog(int siteId) {
            var query =
                from x in _context.FmsAlarmLog
                where x.AlarmPoint.SiteId == siteId
                select x;
            return query;
        }

    }
}