public ActionResult Delete(long id, Country vm)
        {
            ModelState.Clear();
            Country country = this.db.Countries
                                  .Where(c => c.Id == vm.Id)
                                  .FirstOrDefault();

            if (country == null)
                return HttpNotFound();

            if(country.States.Count > 0)
            {
                this.FlashError("Você não pode apagar esse país pois ele tem estados cadastrados. Remova todos os estados antes de apagar o país");
                return View(vm);
            }

            try
            {
                db.Countries.Remove(country);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                this.FlashError("Ocorreu um problema ao apagar o país");
            }

            return View(vm);
        }
        public SensorsController()
        {
            if(this.db.Sensors.Count() == 0)
            {
                City city;
                State state;
                Country country;
                if(db.Countries.Count() == 0)
                {
                    country = new Country { Name = "Brasil" };
                    db.Countries.Add(country);
                    db.SaveChanges();
                }
                else
                {
                    country = db.Countries.Where(c => c.Name == "Brasil").FirstOrDefault();
                }

                if(db.States.Count() == 0)
                {
                    state = new State { Name = "Santa Catarina", Country = country, Code = "SC" };
                    db.States.Add(state);
                    db.SaveChanges();
                }else
                {
                    state = db.States.Where(s => s.Name == "Santa Catarina").FirstOrDefault();
                }

                if(db.Cities.Count() == 0)
                {
                    city = new City { Name = "Joinville", State = state };
                    db.Cities.Add(city);
                    db.SaveChanges();
                }else
                {
                    city = db.Cities.Where(c => c.Name == "Joinville").FirstOrDefault();
                }

                for(int i = 0; i < 100; i++)
                {
                    Sensor sensor = new Sensor();
                    sensor.Address = "Rua XV de Novembro " + i;
                    sensor.City = city;
                    sensor.Latitude = 10.0;
                    sensor.Longitude = 10.0;
                    sensor.Name = "Sensor " + i;
                    sensor.SerialNumber = i.ToString();
                    db.Sensors.Add(sensor);
                }

                db.SaveChanges();

            }
        }
        public ActionResult Create(Country vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Country country = new Country();
                    country.Name = vm.Name;
                    country.CreatedAt = DateTime.Now;
                    country.UpdatedAt = DateTime.Now;
                    this.db.Countries.Add(country);
                    this.db.SaveChanges();
                    this.FlashInfo("País cadastrado com sucesso.");
                    return RedirectToAction("Index");
                }
                catch(Exception ex)
                {
                    ModelState.AddModelError("", "Ocorreu um problema ao salvar o país");
                }

            }

            return View(vm);
        }
        public ActionResult Edit(long id, Country vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Country country = this.db.Countries
                                  .Where(c => c.Id == id)
                                  .FirstOrDefault();

                    if (country == null)
                        return HttpNotFound();

                    country.Name = vm.Name;
                    country.UpdatedAt = DateTime.Now;
                    this.db.SaveChanges();
                    this.FlashInfo("País alterado com sucesso.");
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", "Ocorreu um problema ao salvar o país");
                }

            }

            return View(vm);
        }
        public ActionResult Create()
        {
            Country country = new Country();

            return View(country);
        }