<commit_before>﻿using System;
using System.Linq;
using Sequencing.WeatherApp.Models;

namespace Sequencing.WeatherApp.Controllers
{
    /// <summary>
    /// Class for SendInfo data updates/retrievals
    /// </summary>
    internal class SendInfoWorker
    {
        private string name;

        public SendInfoWorker(string name)
        {
            this.name = name;
        }

        public SendInfo GetInfo()
        {
            using (var _ctx = new WeatherAppDbEntities())
            {
                SendInfo _firstOrDefault = _ctx.SendInfoes.FirstOrDefault(info => info.UserName == name)
                                           ?? CreateDefaults(name);
                return _firstOrDefault;
            }
        }

        public static SendInfo CreateDefaults(string name)
        {
            return new SendInfo
                   {
                       UserName = name,
                       WeekendMode = WeekEndMode.SendBoth,
                       TimeWeekDay = "6 AM",
                       TimeWeekEnd = "8 AM"
                   };
        }

        private void UpdateImpl(Action<SendInfo> si)
        {
            using (var _ctx = new WeatherAppDbEntities())
            {
                var _firstOrDefault = _ctx.SendInfoes.FirstOrDefault(info => info.UserName == name);
                if (_firstOrDefault == null)
                {
                    _firstOrDefault = new SendInfo { UserName = name };

                    _ctx.SendInfoes.Add(_firstOrDefault);
                }
                si(_firstOrDefault);
                _ctx.SaveChanges();
            }
        }

        public void SetLocation(string city)
        {
            if (!String.IsNullOrEmpty(city))   
                UpdateImpl(delegate(SendInfo info)
                           {
                               info.City = city;
                               info.LastWeatherUpdate = null;
                               info.WeatherUpdateDt = null;
                           });
        }

        public void SetDataFile(string selectedName, string selectedId)
        {
            if (!String.IsNullOrEmpty(selectedName))
                UpdateImpl(info => { info.DataFileName = selectedName;
                                       info.DataFileId = selectedId;
                });
        }

        private decimal ParseOffset(string offset)
        {
            decimal _sign = 1;
            if (offset.StartsWith("-"))
                _sign = -1;
            if (offset.Contains(":"))
            {
                var _strings = offset.Substring(1).Split(':');
                return _sign*(decimal.Parse(_strings[0]) + decimal.Parse(_strings[1])/60);
            }
            else
                return decimal.Parse(offset);
        }

        public InviteChanges SetNotification(bool emailChk, bool smsChk, string email, string phone, string wakeupDay, string wakeupEnd, string tz, string tzOffset, WeekEndMode weekendMode, TemperatureMode temperature)
        {
            var _res = new InviteChanges();
            UpdateImpl(info =>
                       {
                           if (!(info.SendEmail ?? false) && emailChk)
                               _res.SendEmail = true;
                           if (!(info.SendSms ?? false) && smsChk)
                               _res.SendSms = true;
                           info.SendEmail = emailChk;
                           info.SendSms = smsChk;
                           info.UserEmail = email;
                           info.UserPhone = phone;
                           info.TimeWeekDay = wakeupDay;
                           info.TimeWeekEnd = wakeupEnd;
                           if (!string.IsNullOrEmpty(tzOffset))
                               info.TimeZoneOffset = ParseOffset(tzOffset);
                           info.TimeZoneValue = tz;
                           info.WeekendMode = weekendMode;
                           info.Temperature = temperature;
                       });
            return _res;
        }
    }
}<commit_msg>Added email and sms check<commit_after>﻿using System;
using System.Linq;
using Sequencing.WeatherApp.Models;

namespace Sequencing.WeatherApp.Controllers
{
    /// <summary>
    /// Class for SendInfo data updates/retrievals
    /// </summary>
    internal class SendInfoWorker
    {
        private string name;

        public SendInfoWorker(string name)
        {
            this.name = name;
        }

        public SendInfo GetInfo()
        {
            using (var _ctx = new WeatherAppDbEntities())
            {
                SendInfo _firstOrDefault = _ctx.SendInfoes.FirstOrDefault(info => info.UserName == name)
                                           ?? CreateDefaults(name);
                return _firstOrDefault;
            }
        }

        public static SendInfo CreateDefaults(string name)
        {
            return new SendInfo
                   {
                       UserName = name,
                       WeekendMode = WeekEndMode.SendBoth,
                       TimeWeekDay = "6 AM",
                       TimeWeekEnd = "8 AM"
                   };
        }

        private void UpdateImpl(Action<SendInfo> si)
        {
            using (var _ctx = new WeatherAppDbEntities())
            {
                var _firstOrDefault = _ctx.SendInfoes.FirstOrDefault(info => info.UserName == name);
                if (_firstOrDefault == null)
                {
                    _firstOrDefault = new SendInfo { UserName = name };

                    _ctx.SendInfoes.Add(_firstOrDefault);
                }
                si(_firstOrDefault);
                _ctx.SaveChanges();
            }
        }

        public void SetLocation(string city)
        {
            if (!String.IsNullOrEmpty(city))   
                UpdateImpl(delegate(SendInfo info)
                           {
                               info.City = city;
                               info.LastWeatherUpdate = null;
                               info.WeatherUpdateDt = null;
                           });
        }

        public void SetDataFile(string selectedName, string selectedId)
        {
            if (!String.IsNullOrEmpty(selectedName))
                UpdateImpl(info => { info.DataFileName = selectedName;
                                       info.DataFileId = selectedId;
                });
        }

        private decimal ParseOffset(string offset)
        {
            decimal _sign = 1;
            if (offset.StartsWith("-"))
                _sign = -1;
            if (offset.Contains(":"))
            {
                var _strings = offset.Substring(1).Split(':');
                return _sign*(decimal.Parse(_strings[0]) + decimal.Parse(_strings[1])/60);
            }
            else
                return decimal.Parse(offset);
        }

        public InviteChanges SetNotification(bool emailChk, bool smsChk, string email, string phone, string wakeupDay, string wakeupEnd, string tz, string tzOffset, WeekEndMode weekendMode, TemperatureMode temperature)
        {
            var _res = new InviteChanges();
            UpdateImpl(info =>
                       {
                           if (ShouldSendInitialEmail(info, email, emailChk))
                               _res.SendEmail = true;

                           if (ShouldSendInitialSms(info, phone, smsChk))
                               _res.SendSms = true;

                           info.SendEmail = emailChk;
                           info.SendSms = smsChk;
                           info.UserEmail = email;
                           info.UserPhone = phone;
                           info.TimeWeekDay = wakeupDay;
                           info.TimeWeekEnd = wakeupEnd;
                           if (!string.IsNullOrEmpty(tzOffset))
                               info.TimeZoneOffset = ParseOffset(tzOffset);
                           info.TimeZoneValue = tz;
                           info.WeekendMode = weekendMode;
                           info.Temperature = temperature;
                       });

            return _res;
        }

        private bool ShouldSendInitialEmail(SendInfo info, string email, bool emailChk)
        {
            bool isAlreadySubscribed = !(info.SendEmail ?? false);

            if (!emailChk)
                return false;

            if (!isAlreadySubscribed)
                return true;

            bool emailMatches = email.Equals(info.UserEmail);

            return !emailMatches;
        }

        private bool ShouldSendInitialSms(SendInfo info, string phone, bool smsChk)
        {
            bool isAlreadySubscribed = !(info.SendSms ?? false);

            if (!smsChk)
                return false;

            if (!isAlreadySubscribed)
                return true;

            bool phoneMatches = phone.Equals(info.UserPhone);

            return !phoneMatches;
        }
    }
}
{'fraction_non_alphanumeric': 0.07928531546621999, 'fraction_numerical': 0.0020100502512562816, 'mean_word_length': 1.170625302956859, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4810207', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2092, 'n_words': 448}