using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Transactions;

using FMSAdmin.Data;
using FMSAdmin.Helpers;
using FMSAdmin.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using FMSAdmin.Models;

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

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

        public void Save(BemsReportFormat data) {
            using (var trans = new TransactionScope()) {
                var persist_1 = 0;

                if (data.ReportFormatId == 0) {
                    var iCnt = _context.BemsReportFormat.Where(x => x.SiteId == data.SiteId).Count();

                    if (iCnt > 0) persist_1 = _context.BemsReportFormat.Where(x => x.SiteId == data.SiteId).Max(x => x.ReportFormatId);
                    else persist_1 = 0;
                    data.ReportFormatId = persist_1 + 1;
                }

                var persist = _context.BemsReportFormat
                                .Where(x => x.SiteId == data.SiteId && x.ReportFormatId == data.ReportFormatId).FirstOrDefault();
                if (persist == null) {

                    // 설비 기본정보 생성되지 않도록 처리
                    foreach (var facility in data.BemsReportFormatToFacility) {
                        facility.CmFacility = null;
                    }


                    _context.BemsReportFormat.Add(data);
                    _context.SaveChanges();
                } else {
                    persist.BusinessFieldId = data.BusinessFieldId;
                    persist.FormatName = data.FormatName;

                    persist.BemsReportFormatToFacility.Clear();
                    persist.BemsReportFormatContent.Clear();

                    // 추가된 설비 생성
                    foreach (var facility in data.BemsReportFormatToFacility) {
                        persist.BemsReportFormatToFacility.Add(new BemsReportFormatToFacility() {
                            SiteId = persist.SiteId,
                            ReportFormatId = persist.ReportFormatId,
                            FacilityCode = facility.FacilityCode,
                        });
                    }

                    // 추가된 일지 생성
                    foreach (var info in data.BemsReportFormatContent) {
                        persist.BemsReportFormatContent.Add(new BemsReportFormatContent() {
                            SiteId = persist.SiteId,
                            ReportFormatId = persist.ReportFormatId,
                            ContentId = info.ContentId,
                            Content = info.Content,
                        });
                    }

                    _context.BemsReportFormat.Update(persist);


                    _context.SaveChanges();

                }
                trans.Complete();
            }
        }

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


        }

        public IQueryable<BemsReportFormat> Get(int reportFormatId, int siteId) {
            var data = _context.BemsReportFormat.Where(x => x.SiteId == siteId && x.ReportFormatId == reportFormatId);
            return data;
        }

        public void Delete(int reportFormatId, int siteId) {
            using (var trans = new TransactionScope()) {
                var data = _context.BemsReportFormat.First(x => x.ReportFormatId == reportFormatId && x.SiteId == siteId);
                var detail = _context.BemsReportFormatContent.Where(x => x.ReportFormatId == reportFormatId && x.SiteId == siteId);
                var facility = _context.BemsReportFormatToFacility.Where(x => x.ReportFormatId == reportFormatId);

                _context.BemsReportFormatToFacility.RemoveRange(facility);
                _context.BemsReportFormatContent.RemoveRange(detail);
                _context.BemsReportFormat.Remove(data);
                _context.SaveChanges();

                trans.Complete();
            }
        }
    }
}