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

import com.itsol.quantrivanphong.report.issue.common.SystemConstants;
import com.itsol.quantrivanphong.manager.project.project.bussiness.ProjectBussiness;
import com.itsol.quantrivanphong.manager.project.project.dto.ProjectDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/quan-tri")
public class ProjectController {


    @Autowired
    private ProjectBussiness projectBussiness;

    @GetMapping(value = "/danh-sach-du-an/{offset}/{limit}")
    public ResponseEntity<List<ProjectDTO>> getProjectPage(@PathVariable("offset") Integer offset,@PathVariable("limit") Integer limit){
        return ResponseEntity.ok(projectBussiness.findProjectPage(null,null,null,null,offset,limit));
    }

    @GetMapping(value = "/danh-sach-du-an")
    public ResponseEntity<List<ProjectDTO>> getAllProject(){
        return ResponseEntity.ok(projectBussiness.findAllProject());
    }
    @GetMapping(value = "/chi-tiet-du-an/{id}",produces = SystemConstants.TYPE_JSON)
    public ResponseEntity<ProjectDTO> findProjectId(@PathVariable Integer id){
        ProjectDTO projectDTO = projectBussiness.findByProjectId(id);
        return ResponseEntity.ok(projectDTO);
    }
    @PostMapping(value = "/them-du-an",consumes = SystemConstants.TYPE_JSON)
    public ResponseEntity saveProject(@RequestBody ProjectDTO projectDTO){
        String views = projectBussiness.saveProject(projectDTO);
        return ResponseEntity.ok(new WrapperResult(200, views));
    }
    @PutMapping(value = "/sua-du-an",consumes = SystemConstants.TYPE_JSON)
    public ResponseEntity updateProject(@RequestBody ProjectDTO projectDTO){
        String views = projectBussiness.updateProject(projectDTO);
        return ResponseEntity.ok(new WrapperResult(200, views));
    }
    @DeleteMapping(value = "/xoa-danh-sach-du-an",consumes = SystemConstants.TYPE_JSON)
    public ResponseEntity deleteProject(@RequestBody ProjectDTO projectDTO){
        String views ="";
        if(projectDTO.getIds()!=null){
         views = projectBussiness.deleteProject(projectDTO.getIds());
        }else{
            views="không tồn tại";
        }
        return ResponseEntity.ok(views);
    }
    @DeleteMapping(value = "/xoa-du-an",consumes = SystemConstants.TYPE_JSON)
    public ResponseEntity deleteProjectById(@RequestBody ProjectDTO projectDTO){
        String views ="";
        if(projectDTO.getId()!=null){
            views = projectBussiness.deleteProjectById(projectDTO.getId());
        }else{
            views="không tồn tại";
        }
        return ResponseEntity.ok(new WrapperResult(200, views));
    }

    static class WrapperResult{
        private int status;
        private String message;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public WrapperResult(int status, String message) {
            this.status = status;
            this.message = message;
        }
    }
}
