package com.utopiaindustries.controller;

import com.utopiaindustries.auth.AdminRole;
import com.utopiaindustries.model.ctp.InventoryAccount;
import com.utopiaindustries.service.InventoryAccountService;
import com.utopiaindustries.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@AdminRole
@RequestMapping( "/inventory-accounts" )
public class InventoryAccountController {

    private final InventoryAccountService inventoryAccountService;

    public InventoryAccountController(InventoryAccountService inventoryAccountService) {
        this.inventoryAccountService = inventoryAccountService;
    }

    @GetMapping
    public String showInventoryAccounts(  @RequestParam( value = "id", required = false ) String id,
                                          @RequestParam( value = "title", required = false) String title,
                                          @RequestParam( value = "active", required = false ) String active,
                                          @RequestParam( value = "created-by", required = false ) String createdBy,
                                          @RequestParam( value = "start-date", required = false ) String startDate,
                                          @RequestParam( value = "end-date", required = false ) String endDate,
                                          @RequestParam( value = "site-id", required = false ) String siteId,
                                          @RequestParam( value = "count", required = false ) Long count,
                                          Model model ){

        model.addAttribute( "accounts", inventoryAccountService.findInventoryAccountsByFilter(id, title, active, createdBy, startDate, endDate, siteId, count) );
        return "inventory-account-list";
    }


    @GetMapping( "/new" )
    public String showNewForm( Model model ){
        model.addAttribute( "account", inventoryAccountService.createNewAccount() );
        model.addAttribute("processes", inventoryAccountService.getAllProcess() );
        return "inventory-account-add";
    }

    @GetMapping( "/edit/{id}" )
    public String showEditForm( @PathVariable long id,
                                Model model ){
        model.addAttribute("account", inventoryAccountService.find(id) );
        model.addAttribute("processes", inventoryAccountService.getAllProcess() );
        return "inventory-account-edit";
    }

    @PostMapping( "/edit" )
    public String saveNewForm( @ModelAttribute InventoryAccount inventoryAccount,
                               RedirectAttributes redirectAttributes ){
        try {
            inventoryAccountService.saveAccount( inventoryAccount );
            redirectAttributes.addFlashAttribute("success", inventoryAccount.getTitle() + "  Successfully Added" );
        } catch ( Exception e ){
            redirectAttributes.addFlashAttribute("error", e.getMessage() );
        }
        return "redirect:/inventory-accounts";
    }

    @PostMapping( "/edit/{id}" )
    public String saveNewForm( @ModelAttribute InventoryAccount inventoryAccount,
                               @PathVariable long id,
                               RedirectAttributes redirectAttributes ){
        try {
            inventoryAccountService.saveAccount( inventoryAccount );
            redirectAttributes.addFlashAttribute("success", inventoryAccount.getTitle() + "  Successfully update" );
        } catch ( Exception e ){
            redirectAttributes.addFlashAttribute("error", e.getMessage() );
        }
        return "redirect:/inventory-accounts";
    }
}
