using BankContracts.BindingModels;
using BankContracts.ViewModels;
using BankDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BankDatabaseImplement.Models
{
    public class CreditProgram : ICreditProgramModel
    {
        public int Id { get; set; }
        [Required]
        public float Percent { get; set; }
        [Required]
        public string Name { get; set; }
        public int BankOperatorId { get; set; }
        public virtual BankOperator BankOperator { get; set; } = new();

        private Dictionary<int, ICurrencyModel>? _creditProgramCurrencies = null;
        [NotMapped]
        public Dictionary<int, ICurrencyModel> CreditProgramCurrencies
        {
            get
            {
                if (_creditProgramCurrencies == null)
                {
                    _creditProgramCurrencies = Currencies.ToDictionary(recDP => recDP.CurrencyId, recDP => recDP.Currency as ICurrencyModel);
                }
                return _creditProgramCurrencies;
            }
        }
        [ForeignKey("CreditProgramId")]
        public virtual List<CreditProgramCurrency> Currencies { get; set; } = new();

        public static CreditProgram? Create(BankDatabase context, CreditProgramBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new CreditProgram()
            {
                Id = model.Id,
                Name = model.Name,
                Percent = model.Percent,
                Currencies = model.CreditProgramCurrencies.Select(x => new CreditProgramCurrency
                {
                    Currency = context.Currencies.First(y => y.Id == x.Key)
                }).ToList(),
                BankOperatorId = model.BankOperatorId,
                BankOperator = context.BankOperators.First(x => x.Id == model.BankOperatorId),
            };
        }
        public void Update(CreditProgramBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Name = model.Name;
            Percent = model.Percent;
        }
        public CreditProgramViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            Percent = Percent,
            CreditProgramCurrencies = CreditProgramCurrencies,
        };
        public void UpdateCurrencies(BankDatabase context, CreditProgramBindingModel model)
        {
            var creditProgramsCurrencies = context.CreditProgramCurrencies.Where(rec => rec.CreditProgramId == model.Id).ToList();
            if (creditProgramsCurrencies != null && creditProgramsCurrencies.Count > 0)
            {
                context.CreditProgramCurrencies.RemoveRange(creditProgramsCurrencies.Where(rec => !model.CreditProgramCurrencies.ContainsKey(rec.CreditProgramId)));
                context.SaveChanges();
                var creditProgram = context.CreditPrograms.First(x => x.Id == Id);
                foreach (var updateCurrency in creditProgramsCurrencies)
                {
                    model.CreditProgramCurrencies.Remove(updateCurrency.CurrencyId);
                }
                foreach (var dp in model.CreditProgramCurrencies)
                {
                    context.CreditProgramCurrencies.Add(new CreditProgramCurrency
                    {
                        CreditProgram = creditProgram,
                        Currency = context.Currencies.First(x => x.Id == dp.Key),
                    });
                    context.SaveChanges();
                }
                _creditProgramCurrencies = null;
            }
        }
    }
}
