package com.itsol.quantrivanphong.report.leaveform.controller;

import com.itsol.quantrivanphong.exception.InputException;
import com.itsol.quantrivanphong.report.leaveform.business.LeaveFormBusiness;
import com.itsol.quantrivanphong.report.leaveform.dto.LeaveFormDTO;
import com.itsol.quantrivanphong.model.LeaveForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(path = "/employee")
public class LeaveFormController {
    @Autowired
    LeaveFormBusiness leaveFormBusiness;

    @GetMapping(path = "/leaveform")
    public ResponseEntity<List<LeaveForm>> showAllLeaveForm() {
        return ResponseEntity.ok(leaveFormBusiness.findAll());
    }

    @PostMapping(path = "/{employee_Id}/leavetype/{leavetype_Id}/leaveform", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> insertLeaveForm(@PathVariable int employee_Id, @Valid @RequestBody LeaveFormDTO leaveFormDTO, @PathVariable int leavetype_Id) {
        String message;

        try {
            if (leaveFormDTO.getTitle().trim().equals("")) throw new InputException("Tiêu đề không được để trống");
            if (leaveFormDTO.getContent().trim().equals("")) throw new InputException("Nội dung không được để trống");
            message = leaveFormBusiness.insertLeaveForm(employee_Id, leaveFormDTO, leavetype_Id);
        } catch (InputException e) {
            message = e.getMessage();
        }

        return ResponseEntity.ok(message);
    }

    @DeleteMapping("/{employee_Id}/leaveform/{leaveform_Id}")
    public ResponseEntity<String> deleteLeaveForm(@PathVariable int employee_Id, @Valid @PathVariable int leaveform_Id) {
        return ResponseEntity.ok(leaveFormBusiness.deleteLeaveForm(employee_Id, leaveform_Id));
    }

    @PostMapping(path = "/{employee_Id}/leavetype/{leavetype_Id}/leaveform/{leaveform_Id}", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> updateLeaveForm(@PathVariable int employee_Id, @PathVariable int leavetype_Id, @PathVariable int leaveform_Id, @Valid @RequestBody LeaveFormDTO leaveFormDTO) {
        String message;

        try {
            if (leaveFormDTO.getTitle().trim().equals("")) throw new InputException("Tiêu đề không được để trống");
            if (leaveFormDTO.getContent().trim().equals("")) throw new InputException("Nội dung không được để trống");
            message = leaveFormBusiness.updateLeaveForm(employee_Id, leavetype_Id, leaveform_Id, leaveFormDTO);
        } catch (InputException e) {
            message = e.getMessage();
        }

        return ResponseEntity.ok(message);
    }

    @PostMapping(path = "/{employee_Id}/leavetype/{leavetype_Id}/leaveform/{leaveform_Id}/status", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> updateLeaveFormStatus(@PathVariable int employee_Id, @PathVariable int leavetype_Id, @PathVariable int leaveform_Id) {
        String message;
        message = leaveFormBusiness.updateLeaveFormStatus(employee_Id, leavetype_Id, leaveform_Id);
        return ResponseEntity.ok(message);
    }

    @GetMapping("/{employee_Id}/leaveform")
    public ResponseEntity<List<LeaveForm>> showLeaveFormById(@PathVariable int employee_Id) {
        if (leaveFormBusiness.leaveFormById(employee_Id) != null) {
            return ResponseEntity.ok(leaveFormBusiness.leaveFormById(employee_Id));
        } else {
            return null;
        }
    }
}
