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

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;
using System.Transactions;

namespace FMSAdmin.Services {
    public class GuidelineService {
        private readonly ILogger<GuidelineService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;
        private readonly PushHelper _push;

        public GuidelineService(
            ILogger<GuidelineService> logger,
            FMSContext context,
            StorageHelper storage,
            PushHelper push) {
            _logger = logger;
            _context = context;
            _storage = storage;
            _push = push;
        }

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

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

        public void Create(CmGuideline data) {
            using (var trans = new TransactionScope()) {
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    data.CmFile.SiteId = data.SiteId;
                    var category = _context.CmFileCategory.First(x => x.Name == "guideline");
                    data.CmFile.FileCategory = category;
                    data.CmFile.CreatedDate = DateTime.Now;
                } else {
                    data.CmFile = null;
                }
                data.IsUse = true;
                data.ReadCount = 0;
                data.AddDate = DateTime.Now;
                _context.CmGuideline.Add(data);
                _context.SaveChanges();

                if (data.CmFile != null && data.CmFile.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile.Path, data.CmFile);
                }

                trans.Complete();
            }

            if (data.IsAllView != null && data.IsAllView == true) {
                _push.SendToAllUsers("관리지침", data.Title, new {
                    type = "guideline",
                    id = data.GuidelineId
                });
            } else {
                _push.SendToSiteUsers(data.SiteId, "관리지침", data.Title, new {
                    type = "guideline",
                    id = data.GuidelineId
                });
            }
        }

        public void Edit(int id, int siteId, CmGuideline data) {
            using (var trans = new TransactionScope()) {
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    data.CmFile.SiteId = data.SiteId;
                    var category = _context.CmFileCategory.First(x => x.Name == "guideline");
                    data.CmFile.FileCategory = category;
                    data.CmFile.CreatedDate = DateTime.Now;
                }

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

                persist.SiteId = data.SiteId;
                persist.GuidelineId = data.GuidelineId;
                persist.BusinessFieldId = data.BusinessFieldId;
                persist.Title = data.Title;
                persist.Contents = data.Contents;
                persist.RegisterUserId = data.RegisterUserId;
                persist.UpdateDate = data.UpdateDate;
                persist.IsAllView = data.IsAllView;
                if (data.CmFile != null && data.CmFile.IsUpload) {
                    persist.CmFile = data.CmFile;
                }
                _context.CmGuideline.Update(persist);
                _context.SaveChanges();

                if (data.CmFile != null && data.CmFile.IsUpload) {
                    // 카피
                    _storage.CopyEntity(data.CmFile.Path, persist.CmFile);
                }

                trans.Complete();
            }
        }

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

        public void AddCount(int id, int siteId) {
            var persist = _context.CmGuideline
                            .Where(x => x.GuidelineId == id && x.SiteId == siteId).FirstOrDefault();
            if (persist == null) {
                throw new ServiceException("정보를 찾을 수 없습니다.");
            }

            persist.ReadCount += 1;
            _context.CmGuideline.Update(persist);
            _context.SaveChanges();
        }
    }
}