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;

namespace FoodShop.Controllers
{
    public class TaxCodesController : Controller
    {
        private readonly ApplicationDbContext _context;

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

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

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

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

            return View(taxCode);
        }

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

        // POST: TaxCodes/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,Name,Percentuale")] TaxCode taxCode)
        {
            if (ModelState.IsValid)
            {
                _context.Add(taxCode);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(taxCode);
        }

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

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

        // POST: TaxCodes/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(string id, [Bind("Id,Name,Percentuale")] TaxCode taxCode)
        {
            if (id != taxCode.Id)
            {
                return NotFound();
            }

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

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

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

            return View(taxCode);
        }

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

        private bool TaxCodeExists(string id)
        {
            return _context.TaxCodes.Any(e => e.Id == id);
        }
    }
}
