package com.markbudai.openfleet.controller;

import com.markbudai.openfleet.services.EmployeeService;
import com.markbudai.openfleet.exception.IdException;
import com.markbudai.openfleet.exception.NullException;
import com.markbudai.openfleet.framework.builder.EmployeeBuilder;
import com.markbudai.openfleet.model.Employee;
import com.markbudai.openfleet.pojo.Payout;
import com.markbudai.openfleet.services.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;

import java.time.LocalDate;

/**
 * Created by Mark on 2017. 04. 15..
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    private static Logger logger = LoggerFactory.getLogger(EmployeeController.class);

    private EmployeeService employeeService;
    private EmployeeBuilder builder;
    private PaymentService paymentService;

    private static String viewPrefix = "employee/";

    @Autowired
    public EmployeeController(EmployeeService provider, EmployeeBuilder builder, PaymentService paymentService){
        this.employeeService = provider;
        this.builder = builder;
        this.paymentService = paymentService;
        logger.debug("{} created.",this.getClass());
    }

    @RequestMapping("/list")
    public String listEmployees(Model model){
        logger.debug("Serving /employee/list");
        model.addAttribute("employeeList", employeeService.getAllEmployees());
        model.addAttribute("path","/employee/list");
        model.addAttribute("title","Employees");
        return viewPrefix+"listEmployees";
    }

    @RequestMapping("/new")
    public String newEmployeeForm(Model model){
        logger.debug("Serving /employee/new");
        model.addAttribute("title","New Employee Form");
        return viewPrefix+"employeeDetails";
    }

    @PostMapping("/add")
    public String employeeSubmit(Model model, WebRequest request){
        Employee e = builder.buildFromWebRequest(request);
        logger.debug("Adding Employee {} to the database.",e);
        if(e.getId() != 0){
            employeeService.updateEmployee(e);
        } else{
            employeeService.addEmployee(e);
        }
        return listEmployees(model);
    }

    @RequestMapping("/edit")
    public String editEmployee(@RequestParam("id") long id, Model model){
        Employee e = employeeService.getEmployeeById(id);
        logger.debug("Giving Employee {} for edit.",e);
        model.addAttribute("employee",e);
        model.addAttribute("title","Employee");
        return viewPrefix+"employeeDetails";
    }

    @RequestMapping("/delete")
    public String deleteEmployee(@RequestParam("id") long id, Model model){
        logger.debug("Removing Employee with id {}",id);
        employeeService.fireEmployee(id);
        return listEmployees(model);
    }

    @RequestMapping("/")
    public String employeeDetail(@RequestParam("id") long id, Model model){
        return editEmployee(id,model);
    }

    @RequestMapping("/payment")
    public String employeePaymentsList(@RequestParam("id") long id, Model model){
        if(id == 0) throw new IdException(id);
        LocalDate today = LocalDate.now();
        model.addAttribute("title","Payments");
        Employee employee = employeeService.getEmployeeById(id);
        if(employee == null) throw new NullException(Employee.class);
        logger.debug("/employee/payment Employee: {}",employee);
        model.addAttribute("employee",employee);
        logger.debug("/employee/payment Going for Payout object...");
        Payout payout = paymentService.getPayout(employee,today.getYear(),today.getMonthValue());
        if(payout == null) throw new NullException(Payout.class);
        logger.debug("/employee/payment Payout: {}",payout);
        model.addAttribute("payout",payout);
        return viewPrefix+"employeePayments";
    }
}

@@ -1289,23 +1289,8 @@
 %22;%0A%0A
-    @Autowired%0A
     
@@ -1319,131 +1319,293 @@
 ler(
-EmployeeService provider, EmployeeBuilder builder, PaymentService paymentService)%7B%0A        this.employeeService = provider;
+)%7B%0A        logger.trace(%22%7B%7D created.%22,this.getClass());%0A    %7D%0A%0A    @Autowired%0A    public void setEmployeeService(EmployeeService employeeService)%7B%0A        this.employeeService = employeeService;%0A    %7D%0A%0A    @Autowired%0A    public void setEmployeeBuilder(EmployeeBuilder employeeBuilder)%7B
 %0A   
@@ -1624,17 +1624,25 @@
 ilder = 
-b
+employeeB
 uilder;%0A
@@ -1649,18 +1649,61 @@
     
+%7D%0A%0A
     
-this.p
+@Autowired%0A    public void setPaymentService(P
 ayme
@@ -1711,18 +1711,16 @@
 tService
- =
  payment
@@ -1722,25 +1722,26 @@
 ymentService
-;
+)%7B
 %0A        log
@@ -1741,51 +1741,44 @@
     
-logger.debug(%22%7B%7D created.%22,this.getClass())
+this.paymentService = paymentService
 ;%0A  
@@ -3384,155 +3384,8 @@
  %7D%0A%0A
-    @RequestMapping(%22/%22)%0A    public String employeeDetail(@RequestParam(%22id%22) long id, Model model)%7B%0A        return editEmployee(id,model);%0A    %7D%0A%0A
     

Create setters for Dependency Injection