// GET: Checks/Create
        public async Task <IActionResult> Create()
        {
            var user = await _userHelper.GetUserByEmailAsync(User.Identity.Name);

            var patient = await this.GetPatientAsync(user.Id);


            double hb = 0, sum = 0;

            /*int hour = DateTime.Now.Hour;
             * var minute = DateTime.Now.Minute;
             *
             * string h = "10:12";
             * DateTime hF = DateTime.Parse(h);
             * int hFinal = hF.Hour;*/


            var cont = _dataContext.Checks
                       .Include(p => p.Patient)
                       .Where(p => p.Patient.Id == patient.Id)
                       .Count();

            if (cont != 0)
            {
                var checks = _dataContext.Checks
                             .Include(p => p.Patient)
                             .Where(p => p.Patient.Id == patient.Id);

                foreach (var i in checks)
                {
                    sum += i.Glucometry;
                }
                hb = sum / cont;
            }


            var model = new AddCheckViewModel
            {
                PatientId = patient.Id,
                Hb1       = hb,
                Date      = DateTime.Now,
                Hour      = DateTime.Now,
            };

            return(View(model));
        }
        public async Task <IActionResult> Create(AddCheckViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userHelper.GetUserByEmailAsync(User.Identity.Name);

                double objective = user.Objective;
                int    hour      = model.Hour.Hour;


                double sensibility   = 0;
                var    sensibilities = _dataContext.Sensibilities
                                       .Include(p => p.Patient)
                                       .Where(p => p.Patient.Id == model.PatientId);
                foreach (var s in sensibilities)
                {
                    if (hour >= s.StartTime.Hour && hour <= s.EndTime.Hour)
                    {
                        sensibility = s.Value;
                    }
                }

                double ratio  = 0;
                var    ratios = _dataContext.Ratios
                                .Include(p => p.Patient)
                                .Where(p => p.Patient.Id == model.PatientId);
                foreach (var r in ratios)
                {
                    if (hour >= r.StartTime.Hour && hour <= r.EndTime.Hour)
                    {
                        ratio = r.Value;
                    }
                }

                double hb = 0, sum = 0;

                var cont = _dataContext.Checks
                           .Include(p => p.Patient)
                           .Where(p => p.Patient.Id == model.PatientId)
                           .Count();
                if (cont != 0)
                {
                    var checks = _dataContext.Checks
                                 .Include(p => p.Patient)
                                 .Where(p => p.Patient.Id == model.PatientId);

                    foreach (var i in checks)
                    {
                        sum += i.Glucometry;
                    }
                    sum += model.Glucometry;
                    hb   = sum / cont + 1;
                }
                else
                {
                    hb += model.Glucometry;
                }

                double bolo = ((model.Glucometry - objective) / sensibility) + (model.Carbohydrates / ratio);

                var check = new Check
                {
                    Carbohydrates = model.Carbohydrates,
                    Glucometry    = model.Glucometry,
                    Date          = model.Date,
                    Hour          = model.Hour,
                    Bolus         = bolo,
                    Hb1           = hb,
                    Patient       = await _dataContext.Patients.FindAsync(model.PatientId),
                };

                _dataContext.Checks.Add(check);
                await _dataContext.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }