package com.icomsys.main_vm.biz.common.system.controller;

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.advice.excep.CustomNotFoundException;
import com.icomsys.main_vm.biz.common.login.res.UserVo;
import com.icomsys.main_vm.biz.common.login.service.LoginService;
import com.icomsys.main_vm.biz.common.system.service.SystemServiceGroupService;
import com.icomsys.main_vm.biz.common.system.vo.*;
import com.icomsys.main_vm.common.code.enumresource.RoleResource;
import com.querydsl.core.QueryResults;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin/common/system")
public class SystemServiceGroupController {

    private final LoginService loginService;
    private final SystemServiceGroupService systemServiceGroupService;

    /**
     * @Name 서비스그룹 랜딩페이지
     * @Description 시스템운영자는 고객사리스트 -> 상위서비스리스트, 유저어드민은, 현재 사용중인 메인 서비스의 상세페이지 랜더링
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @GetMapping("/serviceGroup/manage.do")
    public String ServiceGroupView() {
        UserVo uv = loginService.getUserVo();
        if (uv.getRoleVal().equals(RoleResource.SYSTEMADMIN.getName())) {
            return "/adm/common/system/serviceGroupList";
        } else {
            return "/adm/common/system/serviceGroupView";
        }
    }

    /**
     * @Name 서비스그룹 리스트
     * @Description 시스템운영자는 고객사리스트 -> 상위서비스리스트 데이터 조회
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @PostMapping("/serviceGroup/list")
    @ResponseBody
    public DslDataTableRes<SystemServiceGroupRes> ServiceGroupList(@RequestBody SystemServiceGroupReq dto) {
        QueryResults<SystemServiceGroupRes> result = systemServiceGroupService.ServiceGroupList(dto);
        return new DslDataTableRes<SystemServiceGroupRes>(result);
    }

    /**
     * @Name 서비스그룹 리스트
     * @Description 시스템운영자는 고객사리스트 -> 상위서비스리스트 데이터 조회
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @GetMapping("/serviceGroup/view")
    public String ServiceGroupView(@RequestParam(name = "serviceGroup", required = false) String serviceGroup) {
        return "/adm/common/system/serviceGroupView";
    }

    /**
     * @Name 시나리오그룹 추가 버튼 API
     * @Description 추가할수있는 서비스그룹 검색
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @GetMapping("/serviceGroup/select")
    @ResponseBody
    public SystemServiceGroupViewRes ServiceGroupSelect(@RequestParam(name = "serviceGroup", required = false) String serviceGroup) {
        if(serviceGroup ==null || serviceGroup.equals("")){
            return systemServiceGroupService.ServiceGroupSelect();
        }
        else{
            return systemServiceGroupService.ServiceGroupSelect(serviceGroup);
        }
    }

    /**
     * @Name 선택된 시나리오그룹의 권한
     * @Description 선택된 서비스그룹의 권한리스트
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @GetMapping("/serviceGroup/selectPolicy")
    @ResponseBody
    public List<String> ServiceGroupSelectPolicy(@RequestParam(name = "serviceGroup", required = true) String serviceGroup) {
      return systemServiceGroupService.SelectPolicy(serviceGroup);
    }

    /**
     * @Name 시나리오그룹의 변경값 업데이트
     * @Description 수정된 서비스그룹의 데이터를 업데이트함
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @PostMapping("/serviceGroup/update")
    @ResponseBody
    public void ServiceGroupUpdate(@RequestBody SystemServiceGroupUpdateReq dto) throws CustomNotFoundException {
        systemServiceGroupService.ServiceGroupUpdate(dto);
    }

    /**
     * @Name 시나리오그룹추가 저장 API
     * @Description 시나리오 그룹의 저장 API
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @PostMapping("/serviceGroup/insert")
    @ResponseBody
    public void ServiceGroupInsert(@RequestBody SystemServiceGroupUpdateReq dto) throws CustomNotFoundException {
        systemServiceGroupService.ServiceGroupInsert(dto);
    }

    /**
     * @Name 서비스그룹 중복확인버튼
     * @Description 해당 서비스그룹을 저장할수 있는지 확인하는 API
     * @Author JangWonSeok
     * @CreateDate 2023. 03. 09.
     */
    @PostMapping("/serviceGroup/doubleCheck")
    @ResponseBody
    public String ServiceGroupDoubleCheck(@RequestBody SystemServiceGroupUpdateReq dto) {
        log.info("ServiceGroupDoubleCheck Controller - {}", new Gson().toJson(dto));
        return systemServiceGroupService.ServiceGroupDoubleCheck(dto);
    }


}
