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

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

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

        // public void CreateAllSite() {
        //     var siteList = _context.CmSite.Where(x => x.SiteId != 100).OrderBy(x => x.SortOrderNo).ToList();
        //     var floorCodeList = _context.CmFloorCode.OrderBy(x => x.SortNum).ToList();
        //     //IList<CmFloor> floorList = new List<CmFloor>();

        //     if (_context.CmFloor.ToList().Count() == 0) {
        //         foreach (var site in siteList) {
        //             foreach (var code in floorCodeList) {
        //                 _logger.LogError(site.SiteId + "");
        //                 _logger.LogError(site.CmBuilding.First().BuildingId + "");
        //                 _logger.LogError(code.FloorCode + "");
        //                 _logger.LogError(code.Name + "");
        //                 _context.CmFloor.Add(new CmFloor {
        //                     FloorId = 0,
        //                     SiteId = site.SiteId,
        //                     BuildingId = site.CmBuilding.First().BuildingId,
        //                     FloorCode = code.FloorCode,
        //                     Name = code.Name,
        //                 });
        //                 _context.SaveChanges();
        //             }
        //         }
        //     }

        // }

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

        public void Edit(int id, int siteId, CmFloor data) {
            var persist = _context.CmFloor
                            .Where(x => x.FloorId == id && x.SiteId == siteId).FirstOrDefault();
            if (persist == null) {
                throw new ServiceException("정보를 찾을 수 없습니다.");
            }
            if (!persist.Name.Equals(data.Name)) {
                checkName(data);
            }
            persist.SiteId = data.SiteId;
            persist.BuildingId = data.BuildingId;
            persist.Name = data.Name;
            persist.FloorCode = data.FloorCode;

            _context.CmFloor.Update(persist);
            _context.SaveChanges();
        }


        public void ImportExcel(IList<CmFloor> list) {
            using (var transaction = _context.Database.BeginTransaction()) {
                _context.CmFloor.AddRange(list);
                _context.SaveChanges();
                transaction.Commit();
            }
        }


        public int checkName(CmFloor data, bool isExcel = false) {
            var check = _context.CmFloor.Where(x => x.SiteId == data.SiteId && x.BuildingId == data.BuildingId && x.Name == data.Name).Count();
            if (!isExcel && check > 0) {
                throw new ServiceException("이미 동일 정보가 존재합니다.(빌딩명칭, 층 명칭 확인 필요)");
            }
            return check;
        }

        public void Delete(int id, int siteId) {
            var data = _context.CmFloor.First(x => x.FloorId == id && x.SiteId == siteId);
            _context.CmFloor.Remove(data);
            _context.SaveChanges();
        }

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

        public IQueryable<CmFloor> Get(int id, int siteId) {
            var data = _context.CmFloor.Where(x => x.FloorId == id && x.SiteId == siteId);
            return data;
        }

    }
}