using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Winbooks.Apis.Services;
using Winbooks.Apis.Services.v2;
using Winbooks.TORM.OM;
using Winbooks.TORM.OM.Accounting;

namespace Winbooks.Apis.Linker
{
    public class CommonLinker
    {
        protected EntitiesServices _folder;
        protected Cache _cache;
        protected Settings _settings;

        protected GLAccount _defaultCentralAccountCustomer;
        protected GLAccount _defaultCentralAccountSupplier;

        public CommonLinker(EntitiesServices folder, Settings settings, Cache cache)
        {
            _folder = folder;
            _settings = settings;
            _cache = cache;

            _defaultCentralAccountCustomer = _cache.GetGLAccountByCentralAccountTypeCode("CUSTOMER");
            _defaultCentralAccountSupplier = _cache.GetGLAccountByCentralAccountTypeCode("SUPPLIER");
        }

        protected Guid GetCentralGLAccountCustomer(Customer customer)
        {
            if (customer == null)
                throw new ArgumentException("GetCentralGLAccountCustomer(customer): customer is null");
            return customer.GLAccount2_Id == Guid.Empty ? _defaultCentralAccountCustomer.Id : customer.GLAccount2_Id;
        }

        protected Guid GetCentralGLAccountSupplier(Supplier supplier)
        {
            if (supplier == null)
                throw new ArgumentException("GetCentralGLAccountSupplier(supplier): supplier is null");
            return supplier.GLAccount2_Id == Guid.Empty ? _defaultCentralAccountSupplier.Id : supplier.GLAccount2_Id;
        }

        protected decimal CheckRounded(decimal dec, int numberOfDecimals)
        {
            if (dec - decimal.Round(dec, numberOfDecimals) != 0)
                throw new ArgumentException(string.Format("The amount {0} is not rounded at {1} decimals.", dec, numberOfDecimals));
            return dec;
        }


    }
}
