<commit_before>﻿using food_tracker.DAL;
using food_tracker.Interfaces;
using food_tracker.ListItems;
using food_tracker.Repository;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace food_tracker {
    public partial class trackerForm : Form {

        private readonly TextBox[] textBoxes, textBoxesWithoutName;
        private readonly Label[] dailyTotalLabels;
        private readonly Helper helper;
        private readonly IRepository _nutritionRepo = null;
        private readonly IDayRepository _dayRepository;

        public trackerForm() {
            InitializeComponent();

            textBoxes = new TextBox[] { nameTextBox, caloriesTextBox, fatTextBox, saturatesTextBox, carbsTextBox, sugarsTextBox, fibreTextBox, proteinTextBox, saltTextBox, amountTextbox };
            textBoxesWithoutName = new TextBox[] { caloriesTextBox, fatTextBox, saturatesTextBox, carbsTextBox, sugarsTextBox, fibreTextBox, proteinTextBox, saltTextBox };
            dailyTotalLabels = new Label[] { totalCalLbl, totalFatLbl, totalCarbsLbl, totalFibreLbl, totalProteinLbl, totalSatFatLbl, totalSugarsLbl, totalSaltLbl};

            _nutritionRepo = new NutritionRepository();
            _dayRepository = new DayRepository();
            helper = new Helper();

            setHelpProviders();
            helper.calculateTotals(currentDayItems);
            
            Shown += new EventHandler(loadDataEvent);
        }

        private void setHelpProviders() {
            helpProvider1.SetShowHelp(caloriesTextBox, true);
            helpProvider1.SetHelpString(caloriesTextBox, "Enter the amount of calories in grams.");

            helpProvider1.SetShowHelp(carbsTextBox, true);
            helpProvider1.SetHelpString(carbsTextBox, "Enter the amount of carbohydrates in grams.");

            helpProvider1.SetShowHelp(saturatesTextBox, true);
            helpProvider1.SetHelpString(saturatesTextBox, "Enter the amount of saturated fat in grams.");

            helpProvider1.SetShowHelp(fatTextBox, true);
            helpProvider1.SetHelpString(fatTextBox, "Enter the amount of fat in grams.");

            helpProvider1.SetShowHelp(proteinTextBox, true);
            helpProvider1.SetHelpString(proteinTextBox, "Enter the amount of protein in grams.");

            helpProvider1.SetShowHelp(sugarsTextBox, true);
            helpProvider1.SetHelpString(sugarsTextBox, "Enter the amount of sugars in grams.");

            helpProvider1.SetShowHelp(saltTextBox, true);
            helpProvider1.SetHelpString(saltTextBox, "Enter the amount of salt in grams.");

            helpProvider1.SetShowHelp(fibreTextBox, true);
            helpProvider1.SetHelpString(fibreTextBox, "Enter the amount of fibre in grams.");
        }

        private void loadDataEvent(object sender, EventArgs e) {
            loadData();
            showTotals();
        }

        private void loadData() {
            pastItemsCombo.Items.Clear();
            var day = Md5Hashing.CreateMD5(dateTimePicker.Text.Replace(" ", ""));
            var data = _nutritionRepo.GetAll(day);
            foreach(var item in data) {
                currentDayItems.Items.Add(new FoodBoxItem(item.calories, item.fats, item.saturatedFats, item.carbohydrates, 
                    item.sugars, item.protein, item.salt, item.fibre, item.name, item.NutritionItemId, item.amount, item.dateTime));
            }

            var distinct = _nutritionRepo.GetAllUnique();
            foreach (var item in distinct) {
                pastItemsCombo.Items.Add(new FoodComboItem(item.name, item.NutritionItemId, item.calories, item.fats, 
                    item.saturatedFats, item.carbohydrates, item.sugars, item.protein, item.salt, item.fibre, item.amount));
            }

        }

        private void addNewItemButton_Click(object sender, EventArgs e) {

            if (String.IsNullOrWhiteSpace(nameTextBox.Text)) {
                MessageBox.Show("You must enter a value for the name field.", "", MessageBoxButtons.OK);
                return;
            }

            if (helper.isFieldEmpty(textBoxesWithoutName)) {
                MessageBox.Show("You must enter a value for all available fields.", "", MessageBoxButtons.OK);
                return;
            }

            var day = addOrUpdateCurrentDay(dateTimePicker.Text.Replace(" ", ""));

            var textboxvalues = new List<double>();
            foreach (var textbox in textBoxesWithoutName) {
                var value = helper.parseDouble(textbox.Text);
                if (value != null) {
                    textboxvalues.Add(value.Value);
                } else {
                    return;
                }
            }

            var amount = helper.parseDouble(amountTextbox.Text);
            if(amount == null) {
                return;
            }
            
            var nutrition = new NutritionItem(
                nameTextBox.Text,
                day,
                textboxvalues,
                amount.Value
            );

            _nutritionRepo.Add(nutrition);
            _dayRepository.Update(day);
            
            currentDayItems.Items.Add(new FoodBoxItem(nutrition.calories, nutrition.fats, nutrition.saturatedFats, 
                nutrition.carbohydrates, nutrition.sugars, nutrition.protein, 
                nutrition.salt, nutrition.fibre, nutrition.name, nutrition.NutritionItemId, nutrition.amount, nutrition.dateTime));
            
            showTotals();
            resetFields();
        }
        
        private void showTotals() {            
            var totals = helper.calculateTotals(currentDayItems);
<commit_msg>Add debug statements<commit_after>﻿using food_tracker.DAL;
using food_tracker.Interfaces;
using food_tracker.ListItems;
using food_tracker.Repository;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace food_tracker {
    public partial class trackerForm : Form {

        private readonly TextBox[] textBoxes, textBoxesWithoutName;
        private readonly Label[] dailyTotalLabels;
        private readonly Helper helper;
        private readonly IRepository _nutritionRepo = null;
        private readonly IDayRepository _dayRepository;

        public trackerForm() {
            InitializeComponent();

            textBoxes = new TextBox[] { nameTextBox, caloriesTextBox, fatTextBox, saturatesTextBox, carbsTextBox, sugarsTextBox, fibreTextBox, proteinTextBox, saltTextBox, amountTextbox };
            textBoxesWithoutName = new TextBox[] { caloriesTextBox, fatTextBox, saturatesTextBox, carbsTextBox, sugarsTextBox, fibreTextBox, proteinTextBox, saltTextBox };
            dailyTotalLabels = new Label[] { totalCalLbl, totalFatLbl, totalCarbsLbl, totalFibreLbl, totalProteinLbl, totalSatFatLbl, totalSugarsLbl, totalSaltLbl};

            _nutritionRepo = new NutritionRepository();
            _dayRepository = new DayRepository();
            helper = new Helper();

            setHelpProviders();
            helper.calculateTotals(currentDayItems);
            
            Shown += new EventHandler(loadDataEvent);
        }

        private void setHelpProviders() {
            helpProvider1.SetShowHelp(caloriesTextBox, true);
            helpProvider1.SetHelpString(caloriesTextBox, "Enter the amount of calories in grams.");

            helpProvider1.SetShowHelp(carbsTextBox, true);
            helpProvider1.SetHelpString(carbsTextBox, "Enter the amount of carbohydrates in grams.");

            helpProvider1.SetShowHelp(saturatesTextBox, true);
            helpProvider1.SetHelpString(saturatesTextBox, "Enter the amount of saturated fat in grams.");

            helpProvider1.SetShowHelp(fatTextBox, true);
            helpProvider1.SetHelpString(fatTextBox, "Enter the amount of fat in grams.");

            helpProvider1.SetShowHelp(proteinTextBox, true);
            helpProvider1.SetHelpString(proteinTextBox, "Enter the amount of protein in grams.");

            helpProvider1.SetShowHelp(sugarsTextBox, true);
            helpProvider1.SetHelpString(sugarsTextBox, "Enter the amount of sugars in grams.");

            helpProvider1.SetShowHelp(saltTextBox, true);
            helpProvider1.SetHelpString(saltTextBox, "Enter the amount of salt in grams.");

            helpProvider1.SetShowHelp(fibreTextBox, true);
            helpProvider1.SetHelpString(fibreTextBox, "Enter the amount of fibre in grams.");
        }

        private void loadDataEvent(object sender, EventArgs e) {
            loadData();
            showTotals();
        }

        private void loadData() {
            pastItemsCombo.Items.Clear();
            var day = Md5Hashing.CreateMD5(dateTimePicker.Text.Replace(" ", ""));
            var data = _nutritionRepo.GetAll(day);
            foreach(var item in data) {
                currentDayItems.Items.Add(new FoodBoxItem(item.calories, item.fats, item.saturatedFats, item.carbohydrates, 
                    item.sugars, item.protein, item.salt, item.fibre, item.name, item.NutritionItemId, item.amount, item.dateTime));
            }

            var distinct = _nutritionRepo.GetAllUnique();
            foreach (var item in distinct) {
                pastItemsCombo.Items.Add(new FoodComboItem(item.name, item.NutritionItemId, item.calories, item.fats, 
                    item.saturatedFats, item.carbohydrates, item.sugars, item.protein, item.salt, item.fibre, item.amount));
            }

        }

        private void addNewItemButton_Click(object sender, EventArgs e) {

            if (String.IsNullOrWhiteSpace(nameTextBox.Text)) {
                MessageBox.Show("You must enter a value for the name field.", "", MessageBoxButtons.OK);
                return;
            }

            if (helper.isFieldEmpty(textBoxesWithoutName)) {
                MessageBox.Show("You must enter a value for all available fields.", "", MessageBoxButtons.OK);
                return;
            }

            var day = addOrUpdateCurrentDay(dateTimePicker.Text.Replace(" ", ""));

            var textboxvalues = new List<double>();
            foreach (var textbox in textBoxesWithoutName) {
                var value = helper.parseDouble(textbox.Text);
                if (value != null) {
                    textboxvalues.Add(value.Value);
                } else {
                    return;
                }
            }

            var amount = helper.parseDouble(amountTextbox.Text);
            if(amount == null) {
                return;
            }
            
            var nutrition = new NutritionItem(
                nameTextBox.Text,
                day,
                textboxvalues,
                amount.Value
            );

            _nutritionRepo.Add(nutrition);
            var result = _dayRepository.Update(day);
            if (result == null || typeof(WholeDay) != result.GetType()) Debug.WriteLine($"Could not update that item. Returned result was; {result}");
            
            currentDayItems.Items.Add(new FoodBoxItem(nutrition.calories, nutrition.fats, nutrition.saturatedFats, 
                nutrition.carbohydrates, nutrition.sugars, nutrition.protein, 
                nutrition.salt, nutrition.fibre, nutrition.name, nutrition.NutritionItemId, nutrition.amount, nutrition.dateTime));
            
            showTotals();
            resetFields();
        }
        
        private void showTotals() {            
            var totals = helper.calculateTotals(currentDayItems);

{'fraction_non_alphanumeric': 0.086377309840997, 'fraction_numerical': 0.003094112591319295, 'mean_word_length': 2.308501563832812, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5591659', 'n_tokens_mistral': 3116, 'n_tokens_neox': 2897, 'n_words': 672}