/**
 * Created by Djidjelly Siclait on 11/26/2016.
 */
package com.evapps.Controller;

import com.evapps.Service.CRUD.CreateDataService;
import com.evapps.Service.CRUD.DeleteDataService;
import com.evapps.Service.CRUD.ReadDataService;
import com.evapps.Service.CRUD.UpdateDataService;
import com.evapps.Tools.Enums.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AdminController {

    // Services
    @Autowired
    private CreateDataService CDS;
    @Autowired
    private ReadDataService RDS;
    @Autowired
    private UpdateDataService UDS;
    @Autowired
    private DeleteDataService DDS;

    // Gets
    @GetMapping("/admin")
    public ModelAndView index(Model model){

        if (!RDS.isUserLoggedIn())
            return new ModelAndView("redirect:/login");

        model.addAttribute("userRole", RDS.getCurrentLoggedUser().getRole());
        model.addAttribute("user", RDS.getSessionAttr("user"));

        if (RDS.getCurrentLoggedUser().getRole() != Permission.ADMIN)
            model.addAttribute("isAdmin", false);
        else
            model.addAttribute("isAdmin", true);

        return new ModelAndView("/Backend/homepage/index");
    }

    @GetMapping("/admin/inventory")
    public ModelAndView viewInventory(Model model){

        if(!RDS.isUserLoggedIn())
            return new ModelAndView("redirect:/login");

        model.addAttribute("selection", RDS.findAllRegisteredProducts());

        return new ModelAndView("");
    }

    @GetMapping("/admin/users")
    public ModelAndView viewUsers(Model model){

        if (!RDS.isUserLoggedIn())
            return new ModelAndView("redirect:/login");

        model.addAttribute("userList", RDS.findAllRegisteredAccounts());

        return new ModelAndView("Backend/users/allUsers");
    }

    @GetMapping("/admin/transactions")
    public ModelAndView viewTransactions(Model model){

        if(!RDS.isUserLoggedIn())
            return new ModelAndView("redirect:/login");

        model.addAttribute("transactions", RDS.findAllRegisteredTransactions());

        return new ModelAndView("");
    }

    // Posts
    @PostMapping("/add_new_product")
    public String registerNewProduct(@RequestParam("name") String productName, @RequestParam("supplier") String supplier, @RequestParam("description") String productDescription, @RequestParam("price") Float productPrice, @RequestParam("quantity") Integer productInStock){

        if(!RDS.isUserLoggedIn())
            return "redirect:/login";

        if (RDS.getCurrentLoggedUser().getRole() != Permission.ADMIN)
            return "redirect:/login"; // User must be an admin

        try {
            CDS.registerNewProduct(productName, supplier, productDescription, productPrice, productInStock);
            return "redirect:/admin/inventory";
        } catch (Exception exp){
            //
        }

        return "redirect:/admin/inventory";
    }

    // TODO: editProduct
    // TODO: deleteProduct
    // TODO: restockProduct
    // TODO: uploadProductImage
    // TODO: emailUser
    // TODO: suspendUserAccount
    // TODO: makeAdmin
    // TODO: printTransaction
    // TODO: downloadReport
}

@@ -80,16 +80,50 @@
 oller;%0A%0A
+import com.evapps.Entity.Product;%0A
 import c
@@ -738,16 +738,58 @@
 dView;%0A%0A
+import javax.websocket.server.PathParam;%0A%0A
 @Control
@@ -3338,40 +3338,1298 @@
 ry%22;
-%0A    %7D%0A%0A    // TODO: editProduct
+ // TODO: Add error handling%0A    %7D%0A%0A    @PostMapping(%22/edit/product/%7BproductId%7D%22)%0A    public String editProductInformation(@PathParam(%22productId%22) Integer productId, @RequestParam(%22name%22) String productName, @RequestParam(%22supplier%22) String supplier, @RequestParam(%22description%22) String productDescription, @RequestParam(%22price%22) Float productPrice)%7B%0A%0A        if(!RDS.isUserLoggedIn())%0A            return %22redirect:/login%22;%0A%0A        if (RDS.getCurrentLoggedUser().getRole() != Permission.ADMIN)%0A            return %22redirect:/login%22; // User must be an admin%0A%0A        try %7B%0A            Product product = RDS.findRegisteredProduct(productId);%0A%0A            if (!productName.equals(%22%22))%0A                product.setProductName(productName);%0A%0A            if (!supplier.equals(%22%22))%0A                product.setSupplier(supplier);%0A%0A            if (!productDescription.equals(%22%22))%0A                product.setProductDescription(productDescription);%0A%0A            if (productPrice != product.getProductPrice())%0A                product.setProductPrice(productPrice);%0A%0A            UDS.updateRegisteredProduct(product);%0A%0A            return %22redirect:/admin/inventory%22;%0A        %7D catch (Exception exp)%7B%0A            //%0A        %7D%0A%0A        return %22redirect:/admin/inventory%22; // TODO: Add error handling%0A    %7D%0A
 %0A   

Add editProductInformation Post