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

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

        public MonthWorkService(
            ILogger<MonthWorkService> logger,
            FMSContext context) {
            _logger = logger;
            _context = context;
        }
        public void Create(FmsMonthKeyWork data) {
            using (var transaction = _context.Database.BeginTransaction()) {
                if (data.IsAllSite) {
                    var temp = _context.FmsMonthKeyWork.Where(x => x.Month == data.Month).ToList();
                    _context.FmsMonthKeyWork.RemoveRange(temp);

                    var siteIdList = _context.CmSite.Select(x => x.SiteId).ToList();

                    IList<FmsMonthKeyWork> list = new List<FmsMonthKeyWork>();
                    foreach (var siteId in siteIdList) {
                        var mothKeywork = new FmsMonthKeyWork {
                            SiteId = siteId,
                            Month = data.Month,
                            WorkName = data.WorkName,
                        };
                        list.Add(mothKeywork);
                    }
                    _context.FmsMonthKeyWork.AddRange(list);
                    _context.SaveChanges();

                } else {
                    checkMonth(data);
                    _context.FmsMonthKeyWork.Add(data);
                    _context.SaveChanges();
                }
                transaction.Commit();
            }
        }

        public void Edit(int siteId, int month, FmsMonthKeyWork data) {

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

            persist.SiteId = data.SiteId;
            persist.Month = data.Month;
            persist.WorkName = data.WorkName;
            checkMonth(persist);
            _context.FmsMonthKeyWork.Update(persist);
            _context.SaveChanges();
        }

        public void Delete(int siteId, int month) {
            var data = _context.FmsMonthKeyWork.First(x => x.Month == month && x.SiteId == siteId);
            _context.FmsMonthKeyWork.Remove(data);
            _context.SaveChanges();
        }

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

        public IQueryable<FmsMonthKeyWork> Get(int siteId, int month) {
            var data = _context.FmsMonthKeyWork.Where(x => x.Month == month && x.SiteId == siteId);
            return data;
        }


        public void checkMonth(FmsMonthKeyWork data) {
            var check = _context.FmsMonthKeyWork.Where(x => x.SiteId == data.SiteId && x.Month == data.Month).Count();
            if (check > 0 && data.MonthKeyWorkId == 0) {
                var site = _context.CmSite.Where(x => x.SiteId == data.SiteId).FirstOrDefault();

                throw new ServiceException(site.Name + "의 " + data.Month + "월 업무를 이미 등록 하셨습니다.");
            }
        }
        public string GetKeyWork(int siteId, int month) {
            var keyWork = _context.FmsMonthKeyWork.Where(x => x.SiteId == siteId && x.Month == month).FirstOrDefault();
            if (keyWork == null) {
                return "";
            } else {
                return keyWork.WorkName;
            }

        }

    }
}