<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SERVERAPI.Models.Impl;
using Microsoft.AspNetCore.Hosting;
using SERVERAPI.Models;
using static SERVERAPI.Models.StaticData;
using System.Data;


namespace SERVERAPI.Utility
{
    public class ChemicalBalanceMessage
    {
        private UserData _ud;
        private Models.Impl.StaticData _sd;

        private const string MESSAGE_ICON_NONE = "none";
        private const string MESSAGE_ICON_GOOD = "good";
        private const string MESSAGE_ICON_WARNING = "warning";

        public ChemicalBalances chemicalBalances = new ChemicalBalances();        
        public bool displayBalances { get; set; }

        public ChemicalBalanceMessage(UserData ud, Models.Impl.StaticData sd)
        {
            _ud = ud;
            _sd = sd;
        }

        public List<BalanceMessages> DetermineBalanceMessages(string fieldName)
        {
            List<BalanceMessages> messages = new List<BalanceMessages>();
            bool legume = false;
            string message = string.Empty;
            BalanceMessages bm = new BalanceMessages();


            //get soil test values
            ConversionFactor _cf = _sd.GetConversionFactor();

            //get the chemical balances
            ChemicalBalances cb = GetChemicalBalances(fieldName);

            //determine if a legume is included in the crops
            List<FieldCrop> fieldCrops = _ud.GetFieldCrops(fieldName);

            if (fieldCrops.Count > 0) 
            {
                foreach (var _crop in fieldCrops)
                {
                    Crop crop = _sd.GetCrop(Convert.ToInt16(_crop.cropId));
                    if (crop.n_recommcd == 1) //is a legume
                        legume = true;
                }

                if (legume)
                {
                    // get sum of agronomic N for manure, fertilizer and other for field
                    // sum the above number with the crop removal N
                    // use the resulting number to determine message
                    int LegumeAgronomicN = GetLegumeAgronomicN(fieldName);
                    int LegumeRemovalN = GetLegumeRemovalN(fieldName);
                    bm = _sd.GetMessageByChemicalBalance("AgrN", LegumeAgronomicN + LegumeRemovalN, legume);
                    if (bm != null)
                        messages.Add(bm);
                }
                else
                {
                    bm = _sd.GetMessageByChemicalBalance("AgrN", chemicalBalances.balance_AgrN, legume);
                    if (bm != null)
                        messages.Add(bm);
                }

                bm = _sd.GetMessageByChemicalBalance("AgrP2O5", chemicalBalances.balance_AgrP2O5, legume);
                if (bm != null)
                    messages.Add(bm);

                bm = _sd.GetMessageByChemicalBalance("AgrK2O", chemicalBalances.balance_AgrK2O, legume);
                if (bm != null)
                    messages.Add(bm);

                bm = _sd.GetMessageByChemicalBalance("CropN", chemicalBalances.balance_CropN, legume);
                if (bm != null)
                    messages.Add(bm);

                bm = _sd.GetMessageByChemicalBalance("CropP2O5", chemicalBalances.balance_CropP2O5, legume);
                if (bm != null)
                    messages.Add(bm);

                bm = _sd.GetMessageByChemicalBalance("CropK2O", chemicalBalances.balance_CropK2O, legume);
                if (!string.IsNullOrEmpty(bm.Chemical))
                    messages.Add(bm);

                bm = _sd.GetMessageByChemicalBalance("AgrP2O5CropP2O5", chemicalBalances.balance_AgrP2O5, chemicalBalances.balance_CropP2O5, "CropP2O5");
                if (bm != null)
                    messages.Add(bm);

            }
            return messages;
        }

        public ChemicalBalances GetChemicalBalances(string fldName)
        {            
            displayBalances = false;

            chemicalBalances.balance_AgrN = 0;
            chemicalBalances.balance_AgrP2O5 = 0;
            chemicalBalances.balance_AgrK2O = 0;
            chemicalBalances.balance_CropN = 0;
            chemicalBalances.balance_CropP2O5 = 0;
            chemicalBalances.balance_CropK2O = 0;

            List<FieldCrop> crps = _ud.GetFieldCrops(fldName);
            foreach (var c in crps)
            {
                chemicalBalances.balance_AgrN -= Convert.ToInt16(c.reqN);
                chemicalBalances.balance_AgrP2O5 -= Convert.ToInt16(c.reqP2o5);
                chemicalBalances.balance_AgrK2O -= Convert.ToInt16(c.reqK2o);
                chemicalBalances.balance_CropN -= Convert.ToInt16(c.remN);
                chemicalBalances.balance_CropP2O5 -= Convert.ToInt16(c.remP2o5);
                chemicalBalances.balance_CropK2O -= Convert.ToInt16(c.remK2o);
            }

            List<NutrientManure> manures = _ud.GetFieldNutrientsManures(fldName);
            foreach (var m in manures)
            {
                chemicalBalances.balance_AgrN += Convert.ToInt16(m.yrN);
                chemicalBalances.balance_AgrP2O5 += Convert.ToInt16(m.yrP2o5);
                chemicalBalances.balance_AgrK2O += Convert.ToInt16(m.yrK2o);
                chemicalBalances.balance_CropN += Convert.ToInt16(m.ltN);
                chemicalBalances.balance_CropP2O5 += Convert.ToInt16(m.ltP2o5);
                chemicalBalances.balance_CropK2O += Convert.ToInt16(m.ltK2o);
            }

            List<NutrientFertilizer> fertilizers = _ud.GetFieldNutrientsFertilizers(fldName);
            foreach (var f in fertilizers)
            {
                chemicalBalances.balance_AgrN += Convert.ToInt16(f.fertN);
                chemicalBalances.balance_AgrP2O5 += Convert.ToInt16(f.fertP2o5);
                chemicalBalances.balance_AgrK2O += Convert.ToInt16(f.fertK2o);
                chemicalBalances.balance_CropN += Convert.ToInt16(f.fertN);
                chemicalBalances.balance_CropP2O5 += Convert.ToInt16(f.fertP2o5);
                chemicalBalances.balance_CropK2O += Convert.ToInt16(f.fertK2o);
            }

            List<NutrientOther> others = _ud.GetFieldNutrientsOthers(fldName);
            foreach (var m in others)
            {
                chemicalBalances.balance_AgrN += Convert.ToInt16(m.nitrogen);
                chemicalBalances.balance_AgrP2O5 += Convert.ToInt16(m.phospherous);
                chemicalBalances.balance_AgrK2O += Convert.ToInt16(m.potassium);
                chemicalBalances.balance_CropN += Convert.ToInt16(m.nitrogen);
                chemicalBalances.balance_CropP2O5 += Convert.ToInt16(m.phospherous);
                chemicalBalances.balance_CropK2O += Convert.ToInt16(m.potassium);
            }

            if (crps.Count > 0) //display balance messages when at least one crop has been added
                displayBalances = true;

            return chemicalBalances;
        }<commit_msg>Dummy Commit<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SERVERAPI.Models.Impl;
using Microsoft.AspNetCore.Hosting;
using SERVERAPI.Models;
using static SERVERAPI.Models.StaticData;
using System.Data;


namespace SERVERAPI.Utility
{
    public class ChemicalBalanceMessage
    {
        private UserData _ud;
        private Models.Impl.StaticData _sd;

        private const string MESSAGE_ICON_NONE = "none";
        private const string MESSAGE_ICON_GOOD = "good";
        private const string MESSAGE_ICON_WARNING = "warning";

        public ChemicalBalances chemicalBalances = new ChemicalBalances();        
        public bool displayBalances { get; set; }

        public ChemicalBalanceMessage(UserData ud, Models.Impl.StaticData sd)
        {
            _ud = ud;
            _sd = sd;
        }

        public List<BalanceMessages> DetermineBalanceMessages(string fieldName)
        {
            List<BalanceMessages> messages = new List<BalanceMessages>();
            bool legume = false;
            string message = string.Empty;
            BalanceMessages bm = new BalanceMessages();


            //get soil test values
            ConversionFactor _cf = _sd.GetConversionFactor();

            //get the chemical balances
            ChemicalBalances cb = GetChemicalBalances(fieldName);

            //determine if a legume is included in the crops
            List<FieldCrop> fieldCrops = _ud.GetFieldCrops(fieldName);

            if (fieldCrops.Count > 0) 
            {
                foreach (var _crop in fieldCrops)
                {
                    Crop crop = _sd.GetCrop(Convert.ToInt16(_crop.cropId));
                    if (crop.n_recommcd == 1) //is a legume
                        legume = true;
                }

                if (legume)
                {
                    // get sum of agronomic N for manure, fertilizer and other for field
                    // sum the above number with the crop removal N
                    // use the resulting number to determine message
                    int LegumeAgronomicN = GetLegumeAgronomicN(fieldName);
                    int LegumeRemovalN = GetLegumeRemovalN(fieldName);
                    bm = _sd.GetMessageByChemicalBalance("AgrN", LegumeAgronomicN + LegumeRemovalN, legume);
                    if (bm != null)
                        messages.Add(bm);
                }
                else
                {
                    bm = _sd.GetMessageByChemicalBalance("AgrN", chemicalBalances.balance_AgrN, legume);
                    if (bm != null)
                        messages.Add(bm);
                }

                bm = _sd.GetMessageByChemicalBalance("AgrP2O5", chemicalBalances.balance_AgrP2O5, legume);
                if (bm != null)
                    messages.Add(bm);

                bm = _sd.GetMessageByChemicalBalance("AgrK2O", chemicalBalances.balance_AgrK2O, legume);
                if (bm != null)
                    messages.Add(bm);

                bm = _sd.GetMessageByChemicalBalance("CropN", chemicalBalances.balance_CropN, legume);
                if (bm != null)
                    messages.Add(bm);

                bm = _sd.GetMessageByChemicalBalance("CropP2O5", chemicalBalances.balance_CropP2O5, legume);
                if (bm != null)
                    messages.Add(bm);

                bm = _sd.GetMessageByChemicalBalance("CropK2O", chemicalBalances.balance_CropK2O, legume);
                if (!string.IsNullOrEmpty(bm.Chemical))
                    messages.Add(bm);

                bm = _sd.GetMessageByChemicalBalance("AgrP2O5CropP2O5", chemicalBalances.balance_AgrP2O5, chemicalBalances.balance_CropP2O5, "CropP2O5");
                if (bm != null)
                    messages.Add(bm);

            }
            return messages;
        }

        public ChemicalBalances GetChemicalBalances(string fldName)
        {            
            displayBalances = false;

            chemicalBalances.balance_AgrN = 0;
            chemicalBalances.balance_AgrP2O5 = 0;
            chemicalBalances.balance_AgrK2O = 0;
            chemicalBalances.balance_CropN = 0;
            chemicalBalances.balance_CropP2O5 = 0;
            chemicalBalances.balance_CropK2O = 0;

            List<FieldCrop> crps = _ud.GetFieldCrops(fldName);
            foreach (var c in crps)
            {
                chemicalBalances.balance_AgrN -= Convert.ToInt16(c.reqN);
                chemicalBalances.balance_AgrP2O5 -= Convert.ToInt16(c.reqP2o5);
                chemicalBalances.balance_AgrK2O -= Convert.ToInt16(c.reqK2o);
                chemicalBalances.balance_CropN -= Convert.ToInt16(c.remN);
                chemicalBalances.balance_CropP2O5 -= Convert.ToInt16(c.remP2o5);
                chemicalBalances.balance_CropK2O -= Convert.ToInt16(c.remK2o);
            }

            List<NutrientManure> manures = _ud.GetFieldNutrientsManures(fldName);
            foreach (var m in manures)
            {
                chemicalBalances.balance_AgrN += Convert.ToInt16(m.yrN);
                chemicalBalances.balance_AgrP2O5 += Convert.ToInt16(m.yrP2o5);
                chemicalBalances.balance_AgrK2O += Convert.ToInt16(m.yrK2o);
                chemicalBalances.balance_CropN += Convert.ToInt16(m.ltN);
                chemicalBalances.balance_CropP2O5 += Convert.ToInt16(m.ltP2o5);
                chemicalBalances.balance_CropK2O += Convert.ToInt16(m.ltK2o);
            }

            List<NutrientFertilizer> fertilizers = _ud.GetFieldNutrientsFertilizers(fldName);
            foreach (var f in fertilizers)
            {
                chemicalBalances.balance_AgrN += Convert.ToInt16(f.fertN);
                chemicalBalances.balance_AgrP2O5 += Convert.ToInt16(f.fertP2o5);
                chemicalBalances.balance_AgrK2O += Convert.ToInt16(f.fertK2o);
                chemicalBalances.balance_CropN += Convert.ToInt16(f.fertN);
                chemicalBalances.balance_CropP2O5 += Convert.ToInt16(f.fertP2o5);
                chemicalBalances.balance_CropK2O += Convert.ToInt16(f.fertK2o);
            }

            List<NutrientOther> others = _ud.GetFieldNutrientsOthers(fldName);
            foreach (var m in others)
            {
                chemicalBalances.balance_AgrN += Convert.ToInt16(m.nitrogen);
                chemicalBalances.balance_AgrP2O5 += Convert.ToInt16(m.phospherous);
                chemicalBalances.balance_AgrK2O += Convert.ToInt16(m.potassium);
                chemicalBalances.balance_CropN += Convert.ToInt16(m.nitrogen);
                chemicalBalances.balance_CropP2O5 += Convert.ToInt16(m.phospherous);
                chemicalBalances.balance_CropK2O += Convert.ToInt16(m.potassium);
            }

            if (crps.Count > 0) //display balance messages when at least one Crop has been added
                displayBalances = true;

            return chemicalBalances;
        }
{'fraction_non_alphanumeric': 0.08301670370635889, 'fraction_numerical': 0.01849594953043229, 'mean_word_length': 1.8715520790448745, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6341414', 'n_tokens_mistral': 4328, 'n_tokens_neox': 4010, 'n_words': 695}