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;
using FMSApp.Repositories;

namespace FMSAdmin.Services {
    public class WorkBaseService {
        private readonly ILogger<WorkBaseService> _logger;
        private readonly FMSContext _context;
        private readonly WorkRequestRepository _workRequestRepository;
        private readonly WorkConfigRepository _workConfigRepository;

        public WorkBaseService(
            ILogger<WorkBaseService> logger,
            FMSContext context,
            WorkRequestRepository workRequestRepository,
            WorkConfigRepository workConfigRepository) {
            _logger = logger;
            _context = context;
            _workRequestRepository = workRequestRepository;
            _workConfigRepository = workConfigRepository;
        }

        public void ImportExcel(IList<FmsWorkRequest> list, string userId) {
            // using (var trans = new TransactionScope()) {
            foreach (var item in list) {
                // FmsWorkRequest INSERT
                _context.FmsWorkRequest.Add(item);
                _context.SaveChanges();

                // FmsWorkOrder INSERT
                _workRequestRepository.WorkToOrder(item.SiteId, item.WorkRequestId, item.RequestUserId, item.Content, false);

                // FmsWorkResult INSERT
                if (!_workConfigRepository.IsAutoResult(true)) {
                    var order = _context.FmsWorkOrder.FirstOrDefault(
                        x => x.SiteId == item.SiteId
                        && x.WorkRequestId == item.WorkRequestId
                    );
                    // order.FmsWorkRequest.WorkProgressId = 3;
                    _workRequestRepository.WorkToResult(order, new FmsWorkResult() {
                        StartDate = item.StartWorkDate.Date,
                        EndDate = item.StartWorkDate.Date,
                        Content = "",
                        WorkerUserId = item.RequestUserId,
                    });
                }

                var result = _context.FmsWorkResult.FirstOrDefault(
                    x => x.SiteId == item.SiteId
                    && x.WorkRequestId == item.WorkRequestId
                );
                result.IsConfirmed = true;
                result.ConfirmedUserId = item.RequestUserId;
                result.WorkerUserId = item.RequestUserId;
                result.ConfirmedDate = item.RequestDate;
                _context.FmsWorkResult.Update(result);
                _context.SaveChanges();

                // FmsWorkRequest UPDATE
                var request = _context.FmsWorkRequest.FirstOrDefault(
                    x => x.SiteId == item.SiteId
                    && x.WorkRequestId == item.WorkRequestId
                );
                request.WorkProgressId = 6;
                _context.FmsWorkRequest.Update(request);
                _context.SaveChanges();
            }

            //     trans.Complete();
            // }
        }
    }
}