namespace FKGees.DocsDefinitions.Decrets;

public class Przecena : IDecretsDefinition
{
    public int DocType => 18;
    public string Type => "Pc";
    public List<Definition> Definitions { get; } =
    [ 
        new Definition("342-0-24", "MA", "[snr]-[znr]"),
        new Definition("343-0-24", "MA", "[sbr]-[snr]"), 
        new Definition("332-0-24", "WN", "[sbr]")
    ];

    public Task<List<DecretsResult>> Process(IReadOnlyList<Dok> documents)
    {
        var result = new List<DecretsResult>();

        var toProcess = documents
            .Where(x => x.TypDok == DocType);

        foreach (var dok in toProcess)
        {
            Definitions.ForEach(d =>
            {
                var item = dok.DecretKontr();
                item.Data = dok.DataPom;
                item.DataWplywu = dok.Data;
                item.Nr = dok.NrDok;
                item.Opis = dok.TekstZaCo();
                item.StronaKonta = d.AccountSide;
                item.KontoFk = d.Account;
                item.Kwota = Processvalue(dok, d.Expression).AsString();
                
                result.Add(item);
            });
        }

        return Task.FromResult(result);
    }

    private decimal Processvalue(Dok doc, string expression)
    {
        switch(expression)
        {
            case "[snr]-[znr]":
                return doc.Netto - doc.NettoDet;
            case "[sbr]-[snr]":
                return doc.Podatek - doc.PodatekDet;
            case "[sbr]":
                return doc.Razem;
            
        }

        return 0;
    }
}