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

import com.itsol.quantrivanphong.common.InputException;
import com.itsol.quantrivanphong.manage.leaveform.business.LeaveFormBusiness;
import com.itsol.quantrivanphong.manage.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 = "/leave")
public class LeaveFormController {
    @Autowired
    LeaveFormBusiness leaveFormBusiness;

    @GetMapping(path = "/index")
    public ResponseEntity<List<LeaveForm>> showLeaveFormList() {
        return ResponseEntity.ok(leaveFormBusiness.findAllLeaveForm());
    }

    @PostMapping(path = "/insert", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> insertLeaveForm(@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.insertLeaveForm(leaveFormDTO);
        } catch (InputException e) {
            message = e.getMessage();
        }

        return ResponseEntity.ok(message);
    }

    @DeleteMapping("/delete/{idemp}/{idleave}")
    public ResponseEntity<String> deleteLeaveForm(@PathVariable int idemp, @Valid @PathVariable int idleave) {
        return ResponseEntity.ok(leaveFormBusiness.deleteLeaveForm(idemp, idleave));
    }

    @PostMapping(path = "/update", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> updateLeaveForm(@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(leaveFormDTO);
        } catch (InputException e) {
            message = e.getMessage();
        }

        return ResponseEntity.ok(message);
    }

    @PostMapping(path = "/update-status", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> updateLeaveFormStatus(@RequestBody LeaveFormDTO leaveFormDTO) {
        String message;
        message = leaveFormBusiness.updateLeaveFormStatus(leaveFormDTO);
        return ResponseEntity.ok(message);
    }

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