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 ProductAlternatesController : Controller
    {
        private readonly ApplicationDbContext _context;

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

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

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

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

            return View(productAlternate);
        }

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

        // POST: ProductAlternates/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("ProductId,Alternate")] ProductAlternate productAlternate)
        {
            if (ModelState.IsValid)
            {
                _context.Add(productAlternate);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(productAlternate);
        }

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

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

        // POST: ProductAlternates/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("ProductId,Alternate")] ProductAlternate productAlternate)
        {
            if (id != productAlternate.Id)
            {
                return NotFound();
            }

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

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

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

            return View(productAlternate);
        }

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

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