using System.Linq;
using Microsoft.AspNetCore.Mvc;

namespace Yavsc.Controllers
{
    using System.Security.Claims;
    using Models;
    using Models.Musical;
    using Yavsc.Helpers;
    using Yavsc.Server.Helpers;

    public class InstrumentsController : Controller
    {
        private readonly ApplicationDbContext _context;

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

        // GET: Instruments
        public IActionResult Index()
        {
            return View(_context.Instrument.ToList());
        }

        // GET: Instruments/Details/5
        public IActionResult Details(long? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            Instrument instrument = _context.Instrument.Single(m => m.Id == id);
            if (instrument == null)
            {
                return NotFound();
            }

            return View(instrument);
        }

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

        // POST: Instruments/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(Instrument instrument)
        {
            if (ModelState.IsValid)
            {
                _context.Instrument.Add(instrument);
                _context.SaveChanges(User.GetUserId());
                return RedirectToAction("Index");
            }
            return View(instrument);
        }

        // GET: Instruments/Edit/5
        public IActionResult Edit(long? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            Instrument instrument = _context.Instrument.Single(m => m.Id == id);
            if (instrument == null)
            {
                return NotFound();
            }
            return View(instrument);
        }

        // POST: Instruments/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Edit(Instrument instrument)
        {
            if (ModelState.IsValid)
            {
                _context.Update(instrument);
                _context.SaveChanges(User.GetUserId());
                return RedirectToAction("Index");
            }
            return View(instrument);
        }

        // GET: Instruments/Delete/5
        [ActionName("Delete")]
        public IActionResult Delete(long? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            Instrument instrument = _context.Instrument.Single(m => m.Id == id);
            if (instrument == null)
            {
                return NotFound();
            }

            return View(instrument);
        }

        // POST: Instruments/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public IActionResult DeleteConfirmed(long id)
        {
            Instrument instrument = _context.Instrument.Single(m => m.Id == id);
            _context.Instrument.Remove(instrument);
            _context.SaveChanges(User.GetUserId());
            return RedirectToAction("Index");
        }
    }
}
