using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using test.Models.Abstract;
using test.Models.ViewModel;
using test.Models.Entities;

namespace test.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        // etap1
        //------------------------
        public ProductController(IProductRepository productsrepo)
        {
            repo = productsrepo;
        }

        private IProductRepository repo;
        // ----------------------


        // etap 2
        // ----------------------------
        public ActionResult Products()
        {
            ProductView pv = new ProductView() { Products = repo.Products };
            return View(pv);
        }
         // ------------------------------


        // etap 4
        // --------------------------------------------------
        public ActionResult ShowAddingProduct()
        {
            return View("AddProduct",new Product());
        }

        [HttpPost]
        public ActionResult AddProduct(Product newProduct)  
        {
            if (ModelState.IsValid)
            {
                repo.AddProduct(newProduct);
                return RedirectToAction("Products"); // musimy zwrocic widok Products a nie AddProduct
            }
            else
                return View(newProduct);
        }
        //--------------------------------------------------------

        // etap 3
        // --------------------------------------------------
        public ActionResult ProductsSortByName()
        {
            ProductView pv = new ProductView() { Products = repo.Products.OrderBy(x => x.Name) };
            return View("Products",pv);
        }

        public ActionResult ProductsSortByPrice()
        {
            ProductView pv = new ProductView() { Products = repo.Products.OrderBy(x => x.Price) };
            return View("Products",pv);
        }
        // ------------------------------------------------------

    }
}
