package com.itsol.quantrivanphong.manager.employee.controller;

import com.itsol.quantrivanphong.manager.employee.business.EmployeeBusiness;
import com.itsol.quantrivanphong.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;

@RestController

public class EmployeeController {
    @Autowired
    //Retrieve Employee by id
    private EmployeeBusiness employeeBusiness;

    @RequestMapping(value = "/employee/{id}", method = RequestMethod.GET)
    public ResponseEntity<Employee> getEmployee(@PathVariable("id") int id) {
        System.out.println("Fetching employee with id " + id);
        Employee employee = employeeBusiness.findById(id);
        if (employee == null) {
            System.out.println("Employee with id : " + id + " not found");
            return new ResponseEntity<Employee>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Employee>( employee ,HttpStatus.OK);
    }

    //Retrieve all employee
    @RequestMapping(value = "/list_employee", method = RequestMethod.GET)
    public ResponseEntity<List<Employee>> listAllEmployee() {
        List<Employee> employees = employeeBusiness.findAll();
        if (employees == null) {
            return new ResponseEntity<List<Employee>>(HttpStatus.NO_CONTENT);
        }
        return  new ResponseEntity<List<Employee>>( employees , null ,HttpStatus.OK);
    }

    //Update a Employee
    @RequestMapping(value = "/employee/{id}", method = RequestMethod.PUT)
    public ResponseEntity<Employee> updateEmployee(@PathVariable("id") int id, @RequestBody Employee employee) {
        System.out.println("Update Employee " + id);
        Employee currentEmployee = employeeBusiness.findById(id);
        if (employee == null) {
            System.out.println("Employee with id :" + id + "not found");
            return new ResponseEntity<Employee>(HttpStatus.NOT_FOUND);
        }
        currentEmployee.setUsername(employee.getUsername());
        currentEmployee.setPassword(employee.getPassword());
        currentEmployee.setFirstName(employee.getFirstName());
        currentEmployee.setLastName(employee.getLastName());
        currentEmployee.setEmailAddress(employee.getEmailAddress());
        currentEmployee.setPhoneNumber(employee.getPhoneNumber());
        currentEmployee.setSkype(employee.getSkype());
        currentEmployee.setFacebookProfile(employee.getFacebookProfile());
        currentEmployee.setPosition(employee.getPosition());
        currentEmployee.setHomeTown(employee.getHomeTown());
        currentEmployee.setEducation(employee.getEducation());
        currentEmployee.setSchool(employee.getSchool());
        currentEmployee.setDepartment(employee.getDepartment());
        currentEmployee.setGraduationYear(employee.getGraduationYear());
        currentEmployee.setPicture(employee.getPicture());
        employeeBusiness.save(currentEmployee);
        return new ResponseEntity<Employee>( currentEmployee ,HttpStatus.OK);

    }

    //Create a Employee
    @RequestMapping(value = "/list_employee", method = RequestMethod.POST)
    public ResponseEntity<Void> createEmployee(@RequestBody Employee employee,
                                               UriComponentsBuilder uriComponentsBuilder){
        System.out.println("Create Employee "+ employee.getUsername());
        employeeBusiness.save(employee);
        HttpHeaders httpHeaders = new HttpHeaders();
       httpHeaders.setLocation(uriComponentsBuilder.path("/employee/{id}").buildAndExpand(employee.getId()).toUri());
        return new ResponseEntity<Void>( httpHeaders ,HttpStatus.CREATED);
    }
    //Delete a Employee
    @RequestMapping(value = "/employee/{id}" , method = RequestMethod.DELETE)
    public  ResponseEntity<Employee> deleteEmployee(@PathVariable("id") int id){
        System.out.println("Fetching and deleting Employee with id :" + id );
        Employee employee = employeeBusiness.findById(id);
        if(employee == null) {
            System.out.println("Unable to delete. Employee with id :" + id + "not found");
            return new ResponseEntity<Employee>(HttpStatus.NOT_FOUND);

        }
        employeeBusiness.deleteById(id);
        return  new ResponseEntity<Employee>(HttpStatus.NO_CONTENT);
    }


}
