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

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

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

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

        public IQueryable<FmsWeekKeyWork> Get(int siteId, string workDate) {
            var data = _context.FmsWeekKeyWork.Where(x => x.SiteId == siteId && x.WorkDate == Convert.ToDateTime(workDate));
            return data;
        }

        public IQueryable<FmsWeekKeyWork> GetWeek(int siteId, DateTime startDate, DateTime endDate) {
            var query = _context.FmsWeekKeyWork.Where(x => x.SiteId == siteId && x.WorkDate.Date >= startDate.Date && x.WorkDate.Date <= endDate.Date);
            return query;
        }

        public void Edit(int siteId, string workDate, FmsWeekKeyWork data) {
            var persist = _context.FmsWeekKeyWork
                            .Where(x => x.SiteId == siteId && x.WorkDate == Convert.ToDateTime(workDate)).FirstOrDefault();
            if (persist == null) {
                throw new ServiceException("정보를 찾을 수 없습니다.");
            }

            persist.SiteId = data.SiteId;
            persist.Year = data.Year;
            persist.Month = data.Month;
            persist.Week = data.Week;
            persist.WorkDate = data.WorkDate;
            persist.WorkName = data.WorkName;
            checkWeek(persist);
            _context.FmsWeekKeyWork.Update(persist);
            _context.SaveChanges();
        }

        public void Create(FmsWeekKeyWork data) {
            using (var transaction = _context.Database.BeginTransaction()) {
                if (data.IsAllSite) {
                    var temp = _context.FmsWeekKeyWork.Where(x => x.WorkDate == data.WorkDate).ToList();
                    _context.FmsWeekKeyWork.RemoveRange(temp);

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

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

                } else {
                    checkWeek(data);
                    _context.FmsWeekKeyWork.Add(data);
                    _context.SaveChanges();
                }
                transaction.Commit();
            }
        }

        public void Delete(int siteId, string workDate) {
            var data = _context.FmsWeekKeyWork.First(x => x.SiteId == siteId && x.WorkDate == Convert.ToDateTime(workDate));
            _context.FmsWeekKeyWork.Remove(data);
            _context.SaveChanges();
        }

        public void checkWeek(FmsWeekKeyWork data) {
            var check = _context.FmsWeekKeyWork.Where(x => x.SiteId == data.SiteId && x.WorkDate == data.WorkDate).Count();
            if (check > 0 && data.WeekKeyWorkId == 0) {
                var site = _context.CmSite.Where(x => x.SiteId == data.SiteId).FirstOrDefault();

                throw new ServiceException(site.Name + "의 " + data.Year + "년도 " + data.Month + "월 " + data.Week + "주 업무를 이미 등록 하셨습니다.");
            }
        }
    }
}