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;

namespace FMSApp.Services {
    public class AccidentService {
        private readonly ILogger<AccidentService> _logger;
        private readonly FMSContext _context;
        private readonly StorageHelper _storage;
        private readonly AppSettings _appSettings;

        public AccidentService(
            ILogger<AccidentService> logger,
            FMSContext context,
            IOptions<AppSettings> appSettings,
            StorageHelper storage) {
            _logger = logger;
            _context = context;
            _appSettings = appSettings.Value;
            _storage = storage;
        }

        public void Save(FmsAccident data) {
            using (var transaction = _context.Database.BeginTransaction()) {
                var persist = _context.FmsAccident
                                .Where(x => x.SiteId == data.SiteId
                                    && x.AccidentId == data.AccidentId).FirstOrDefault();
                if (persist == null) {
                    if (data.CmFile1 != null && data.CmFile1.IsUpload) {
                        data.CmFile1.SiteId = data.SiteId;
                        var category = _context.CmFileCategory.First(x => x.Name == "accident");
                        data.CmFile1.FileCategory = category;
                        data.CmFile1.CreatedDate = DateTime.Now;
                    } else {
                        data.CmFile1 = null;
                    }
                    if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                        data.CmFile2.SiteId = data.SiteId;
                        var category = _context.CmFileCategory.First(x => x.Name == "accident");
                        data.CmFile2.FileCategory = category;
                        data.CmFile2.CreatedDate = DateTime.Now;
                    } else {
                        data.CmFile2 = null;
                    }
                    if (data.CmFile3 != null && data.CmFile3.IsUpload) {
                        data.CmFile3.SiteId = data.SiteId;
                        var category = _context.CmFileCategory.First(x => x.Name == "accident");
                        data.CmFile3.FileCategory = category;
                        data.CmFile3.CreatedDate = DateTime.Now;
                    } else {
                        data.CmFile3 = null;
                    }
                    if (data.CmFile4 != null && data.CmFile4.IsUpload) {
                        data.CmFile4.SiteId = data.SiteId;
                        var category = _context.CmFileCategory.First(x => x.Name == "accident");
                        data.CmFile4.FileCategory = category;
                        data.CmFile4.CreatedDate = DateTime.Now;
                    } else {
                        data.CmFile4 = null;
                    }

                    _context.FmsAccident.Add(data);
                    _context.SaveChanges();

                    if (data.CmFile1 != null && data.CmFile1.IsUpload) {
                        _storage.CopyEntity(data.CmFile1.Path, data.CmFile1);
                    }
                    if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                        _storage.CopyEntity(data.CmFile2.Path, data.CmFile2);
                    }
                    if (data.CmFile3 != null && data.CmFile3.IsUpload) {
                        _storage.CopyEntity(data.CmFile3.Path, data.CmFile3);
                    }
                    if (data.CmFile4 != null && data.CmFile4.IsUpload) {
                        _storage.CopyEntity(data.CmFile4.Path, data.CmFile4);
                    }

                } else {
                    if (data.CmFile1 != null && data.CmFile1.IsUpload) {
                        var category = _context.CmFileCategory.First(x => x.Name == "accident");
                        persist.CmFile1 = new CmFile {
                            SiteId = data.SiteId,
                            FileCategory = category,
                            CreatedDate = DateTime.Now,
                            Name = data.CmFile1.Name,
                            FileSize = data.CmFile1.FileSize,
                            ContentType = data.CmFile1.ContentType,
                        };

                        _logger.LogInformation("upload file1");
                    } else if (data.CmFile1 != null && data.CmFile1.IsDelete) {
                        persist.FileId1 = null;
                        persist.CmFile1 = null;
                        _logger.LogInformation("delete file1");
                    }
                    if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                        var category = _context.CmFileCategory.First(x => x.Name == "accident");
                        persist.CmFile2 = new CmFile {
                            SiteId = data.SiteId,
                            FileCategory = category,
                            CreatedDate = DateTime.Now,
                            Name = data.CmFile2.Name,
                            FileSize = data.CmFile2.FileSize,
                            ContentType = data.CmFile2.ContentType,
                        };
                        _logger.LogInformation("upload file2");
                    } else if (data.CmFile2 != null && data.CmFile2.IsDelete) {
                        persist.FileId2 = null;
                        persist.CmFile2 = null;
                        _logger.LogInformation("delete file2");
                    }
                    if (data.CmFile3 != null && data.CmFile3.IsUpload) {
                        var category = _context.CmFileCategory.First(x => x.Name == "accident");
                        persist.CmFile3 = new CmFile {
                            SiteId = data.SiteId,
                            FileCategory = category,
                            CreatedDate = DateTime.Now,
                            Name = data.CmFile3.Name,
                            FileSize = data.CmFile3.FileSize,
                            ContentType = data.CmFile3.ContentType,
                        };
                        _logger.LogInformation("upload file3");
                    } else if (data.CmFile3 != null && data.CmFile3.IsDelete) {
                        persist.FileId3 = null;
                        persist.CmFile3 = null;
                        _logger.LogInformation("delete file3");
                    }
                    if (data.CmFile4 != null && data.CmFile4.IsUpload) {
                        var category = _context.CmFileCategory.First(x => x.Name == "accident");
                        persist.CmFile4 = new CmFile {
                            SiteId = data.SiteId,
                            FileCategory = category,
                            CreatedDate = DateTime.Now,
                            Name = data.CmFile4.Name,
                            FileSize = data.CmFile4.FileSize,
                            ContentType = data.CmFile4.ContentType,
                        };
                        _logger.LogInformation("upload file4");
                    } else if (data.CmFile4 != null && data.CmFile4.IsDelete) {
                        persist.FileId4 = null;
                        persist.CmFile4 = null;
                        _logger.LogInformation("delete file4");
                    }

                    persist.AccidentTypeId = data.AccidentTypeId;
                    persist.Name = data.Name;
                    persist.AccidentLocation = data.AccidentLocation;
                    persist.StartDate = data.StartDate;
                    persist.EndDate = data.EndDate;
                    persist.CompanyId = data.CompanyId;
                    persist.DepartmentId = data.DepartmentId;
                    persist.Reason = data.Reason;
                    persist.Action = data.Action;
                    persist.Action2 = data.Action2;
                    persist.Damage = data.Damage;
                    persist.Measurement = data.Measurement;
                    persist.Firstwitness = data.Firstwitness;
                    persist.VictimName = data.VictimName;
                    persist.VictimRelation = data.VictimRelation;
                    persist.VictimAge = data.VictimAge;
                    persist.ClosingDate = data.ClosingDate;
                    persist.IsInsurance = data.IsInsurance;
                    persist.InsuranceContent = data.InsuranceContent;
                    _context.FmsAccident.Update(persist);
                    _context.SaveChanges();

                    if (data.CmFile1 != null && data.CmFile1.IsUpload) {
                        _storage.CopyEntity(data.CmFile1.Path, persist.CmFile1);
                    }
                    if (data.CmFile2 != null && data.CmFile2.IsUpload) {
                        _storage.CopyEntity(data.CmFile2.Path, persist.CmFile2);
                    }
                    if (data.CmFile3 != null && data.CmFile3.IsUpload) {
                        _storage.CopyEntity(data.CmFile3.Path, persist.CmFile3);
                    }
                    if (data.CmFile4 != null && data.CmFile4.IsUpload) {
                        _storage.CopyEntity(data.CmFile4.Path, persist.CmFile4);
                    }

                }
                transaction.Commit();
            }
        }

        public void Delete(int siteId, int accidentId) {
            var data = _context.FmsAccident.First(x => x.SiteId == siteId && x.AccidentId == accidentId);
            _context.FmsAccident.Remove(data);
            _context.SaveChanges();
        }

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