using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using FoodShop.Data;
using FoodShop.Models;
using Microsoft.AspNetCore.Authorization;

namespace FoodShop.Controllers
{
    [Authorize(Roles = "Admin,PowerUser")]
    public class NotiziasController : Controller
    {
        private readonly ApplicationDbContext _context;

        public NotiziasController(ApplicationDbContext context)
        {
            _context = context;
        }

        // GET: Notizias
        public async Task<IActionResult> Index()
        {
            return View(await _context.Notizias.ToListAsync());
        }

        // GET: Notizias/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var notizia = await _context.Notizias
                .FirstOrDefaultAsync(m => m.Id == id);
            if (notizia == null)
            {
                return NotFound();
            }

            return View(notizia);
        }

        // GET: Notizias/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: Notizias/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Title,Subtitle,Newtext,DateCreated,Deleted")] Notizia notizia)
        {
            if (ModelState.IsValid)
            {
                _context.Add(notizia);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(notizia);
        }

        // GET: Notizias/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var notizia = await _context.Notizias.FindAsync(id);
            if (notizia == null)
            {
                return NotFound();
            }
            return View(notizia);
        }

        // POST: Notizias/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Title,Subtitle,Newtext,DateCreated,Deleted")] Notizia notizia)
        {
            if (id != notizia.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(notizia);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!NotiziaExists(notizia.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(notizia);
        }

        // GET: Notizias/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var notizia = await _context.Notizias
                .FirstOrDefaultAsync(m => m.Id == id);
            if (notizia == null)
            {
                return NotFound();
            }

            return View(notizia);
        }

        // POST: Notizias/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var notizia = await _context.Notizias.FindAsync(id);
            _context.Notizias.Remove(notizia);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool NotiziaExists(int id)
        {
            return _context.Notizias.Any(e => e.Id == id);
        }
    }
}
