package com.dbnt.kcgfilemanager.controller;

import com.dbnt.kcgfilemanager.model.CategoryRole;
import com.dbnt.kcgfilemanager.model.ModifyRequest;
import com.dbnt.kcgfilemanager.model.UserInfo;
import com.dbnt.kcgfilemanager.service.CategoryRoleService;
import com.dbnt.kcgfilemanager.service.ModifyRequestService;
import com.dbnt.kcgfilemanager.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/info")
public class InfoController {

  private final CategoryRoleService categoryRoleService;
  private final UserInfoService userInfoService;
  private final ModifyRequestService modifyRequestService;

  @GetMapping("/myInfo")
  public ModelAndView myInfo(@AuthenticationPrincipal UserInfo loginUser) {
    ModelAndView mav = new ModelAndView("user/myInfo");
    mav.addObject("loginUser", loginUser);
    if(!loginUser.getUserRole().contains("ADMIN")){
      CategoryRole categoryRole = new CategoryRole();
      categoryRole.setUserSeq(loginUser.getUserSeq());
      mav.addObject("categorySeqList", categoryRoleService.selectCategorySeqListToUser(categoryRole));
    }
    return mav;
  }

  @PutMapping("/passwordModify")
  public String passwordModify(@AuthenticationPrincipal UserInfo loginUser, UserInfo modifyInfo){
    return userInfoService.updatePassword(loginUser, modifyInfo);
  }

  @GetMapping("/modifyRequestWrite")
  public ModelAndView modifyRequestWrite(@AuthenticationPrincipal UserInfo loginUser, ModifyRequest modifyRequest){
    ModelAndView mav = new ModelAndView("user/modifyRequestWrite");
    mav.addObject("loginUser", loginUser);
    if(modifyRequest.getRequestSeq()==null){
      mav.addObject("type", "new");
      mav.addObject("modifyRequest", modifyRequest);
    }else{
      mav.addObject("type", "modify");
      modifyRequest = modifyRequestService.selectModifyRequestByRequestSeq(modifyRequest.getRequestSeq());
      modifyRequest.setCreateName(userInfoService.selectUserInfoByUserId(modifyRequest.getCreateId()).getName());
      if(modifyRequest.getRequestType().equals("categoryRole")){
        String[] categoryAry = modifyRequest.getRequestCategoryAry().split(",");
        List<Integer> requestCategoryList = new ArrayList<>();
        for(String categorySeq : categoryAry){
          requestCategoryList.add(Integer.parseInt(categorySeq.replaceAll(" ", "")));
        }
        modifyRequest.setRequestCategoryList(requestCategoryList);
      }
      mav.addObject("modifyRequest", modifyRequest);
    }
    CategoryRole categoryRole = new CategoryRole();
    categoryRole.setUserSeq(loginUser.getUserSeq());
    mav.addObject("categorySeqList", categoryRoleService.selectCategorySeqListToUser(categoryRole));
    return mav;
  }

  @PostMapping("/saveModifyRequest")
  public Integer saveModifyRequest(ModifyRequest modifyRequest,
                                   @RequestParam(value = "categorySeq", required = false) List<Integer> categorySeqList){
    if(categorySeqList != null && categorySeqList.size()>0){
      String requestCategoryAry = categorySeqList.toString();
      modifyRequest.setRequestCategoryAry(requestCategoryAry.substring(1, requestCategoryAry.length()-1));
    }
    if(modifyRequest.getRequestSeq()!=null){
      return modifyRequestService.updateModifyRequest(modifyRequest);
    }else{
      return modifyRequestService.saveModifyRequest(modifyRequest);
    }
  }

  @PutMapping("/modifyRequestStateChange")
  public Integer modifyRequestStateChange(ModifyRequest modifyRequest){
    return modifyRequestService.modifyRequestStateChange(modifyRequest);
  }

  @GetMapping("/modifyRequestList")
  public ModelAndView modifyRequestList(@AuthenticationPrincipal UserInfo loginUser, ModifyRequest modifyRequest){
    ModelAndView mav = new ModelAndView("user/modifyRequestList");
    if(loginUser.getUserRole().contains("ADMIN")){
      modifyRequest.setUserRole("ADMIN");
    }else{
      modifyRequest.setCreateId(loginUser.getUserId());
      modifyRequest.setUserRole("USER");
    }
    modifyRequest.setQueryInfo();
    mav.addObject("modifyRequestList", modifyRequestService.selectModifyRequestList(modifyRequest));
    modifyRequest.setContentCnt(modifyRequestService.selectModifyRequestListCnt(modifyRequest));
    modifyRequest.setPaginationInfo();
    mav.addObject("searchParams", modifyRequest);
    return mav;
  }

  @GetMapping("/modifyRequestDetail")
  public ModelAndView modifyRequestDetail(@AuthenticationPrincipal UserInfo loginUser, ModifyRequest modifyRequest){
    ModelAndView mav = new ModelAndView("user/modifyRequestDetail");
    ModifyRequest request = modifyRequestService.selectModifyRequestByRequestSeq(modifyRequest.getRequestSeq());
    UserInfo requestUser = userInfoService.selectUserInfoByUserId(request.getCreateId());
    switch (request.getRequestType()){
      case "userInfo":
        mav.addObject("userInfo", requestUser);
        break;
      case "categoryRole":
        String[] categoryAry = request.getRequestCategoryAry().split(",");
        List<Integer> requestCategoryList = new ArrayList<>();
        for(String categorySeq : categoryAry){
          requestCategoryList.add(Integer.parseInt(categorySeq.replaceAll(" ", "")));
        }
        request.setRequestCategoryList(requestCategoryList);
        CategoryRole categoryRole = new CategoryRole();
        categoryRole.setUserSeq(requestUser.getUserSeq());
        mav.addObject("categorySeqList", categoryRoleService.selectCategorySeqListToUser(categoryRole));
        break;
    }
    mav.addObject("modifyRequest", request);
    mav.addObject("loginUser", loginUser);
    return mav;
  }
}
