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

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.advice.excep.CustomBadRequestException;
import com.icomsys.main_vm.biz.advice.excep.CustomNoSuchFieldException;
import com.icomsys.main_vm.biz.advice.excep.CustomNotFoundException;
import com.icomsys.main_vm.biz.common.common.service.FileService;
import com.icomsys.main_vm.biz.common.login.CinnamonToken;
import com.icomsys.main_vm.biz.common.login.req.LoginReq;
import com.icomsys.main_vm.biz.common.login.req.MainOprReq;
import com.icomsys.main_vm.biz.common.login.req.PwdUpdateReq;
import com.icomsys.main_vm.biz.common.login.res.LoginCheckReq;
import com.icomsys.main_vm.biz.common.login.res.OprmngCodeRes;
import com.icomsys.main_vm.biz.common.login.res.ServiceGroupRes;
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.common.code.MenuVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.*;

@Controller
@Slf4j
@RequiredArgsConstructor
public class LoginController {
    /**
     * 메인 컨트롤러
     */
    private final LoginService loginService;
    private final FileService fileService;
    private final HttpServletRequest httpServletRequest;

    @GetMapping(value = {"/adm/main/egovLoginUsr.do", "/"})
    public String loginUsrView() {
        loginService.Logout();
        return "adm/signin/signin";
    }

    @GetMapping(value = "/adm/main/changePwd")
    public String changePwd() {
        return "adm/signin/changePwd";
    }

    @PostMapping(value = "/adm/main/pwd/update")
    public ResponseEntity PwdUpdate(@RequestBody PwdUpdateReq dto) throws CustomNotFoundException, CustomBadRequestException {
        return loginService.PwdUpdate(dto);
    }


    @PostMapping("/adm/main/check")
    @ResponseBody
    public ResponseEntity loginCheck(@RequestBody LoginCheckReq dto) throws CustomNotFoundException, CustomBadRequestException {
        return loginService.loginCheck(dto);
    }

    @RequestMapping(value = "/adm/main/actionSecurityLogin.do")
    public String actionSecurityLogin(@Valid LoginReq loginReq, ModelMap model) {
        return loginService.LoginValidService(loginReq, model);
    }

    @PostMapping("/adm/tokenLogin")
    @ResponseBody
    public CinnamonToken tokenLogin(@RequestBody LoginCheckReq dto) {
        String id = dto.getUserId();
        String pwd = dto.getPassword();
        return loginService.tokenLogin(dto);
    }


    @RequestMapping(value = "/adm/main/actionMain.do")
    public String actionMain(ModelMap model) {
        return loginService.actionMain(model);
    }

    @GetMapping("/adm/main/actionLogout.do")
    public String Logout() {
        loginService.Logout();
        return "adm/signin/signin";
    }

    @GetMapping(value = "/adm/menu/getLnbMenu.do")
    public String selectMainMenuLeft(ModelMap model) {
        if (loginService.getUserVo() == null) {
            return "adm/signin/signin";
        }
        List<MenuVo> menuVoList = loginService.selectMainMenuLeft();
        model.addAttribute("list_menulist", menuVoList);
        return "adm/include/getLnbMenu";
    }

    @GetMapping("/adm/menu/serviceGroup")
    public List<ServiceGroupRes> getServiceGroup() throws CustomNoSuchFieldException {
        if (loginService.getUserVo() == null) {
            throw new CustomNoSuchFieldException();
        }
        return loginService.getServiceGroup();
    }

    @PostMapping("/adm/menu/serviceGroup/update")
    public void LastUserServiceGroupUpdate(Long userSeq, String updateService) throws CustomNotFoundException, CustomNoSuchFieldException {
        if (loginService.getUserVo() == null) {
            throw new CustomNoSuchFieldException();
        }
        loginService.LastUserServiceGroupUpdate(userSeq, updateService);
    }

    /**
     * 멀티서치용 oprMngCode
     * 내가가지고있는 서비스그룹을보여줌
     *
     * @param serviceType
     * @return
     * @throws CustomNoSuchFieldException
     */
    @GetMapping("/adm/menu/oprmng")
    @ResponseBody
    public List<OprmngCodeRes> getOprmngCode(@RequestParam(name = "serviceType", required = false) String serviceType,
                                             @RequestParam(name = "menuval", required = false) String menuval) throws CustomNoSuchFieldException {
        if (loginService.getUserVo() == null) {
            throw new CustomNoSuchFieldException();
        }
        log.info("getOPR INIT");
        return loginService.getOprmngCode(serviceType, menuval);
    }

    /**
     * 어드민용 oprMngCode
     * 서비스그룹생성용
     * 해당서비스그룹의 코드를 보여줌
     *
     * @param serviceType
     * @return
     * @throws CustomNoSuchFieldException
     */
    @GetMapping("/adm/menu/oprmng/admin")
    @ResponseBody
    public List<OprmngCodeRes> getOprmngCodeAdmin(@RequestParam(name = "serviceType", required = false) String serviceType) throws CustomNoSuchFieldException {
        if (loginService.getUserVo() == null) {
            throw new CustomNoSuchFieldException();
        }
        log.info("getOPR INIT");
        return loginService.getOprmngCodeAdmin(serviceType);
    }

    @GetMapping("/adm/menu/main/oprmng")
    @ResponseBody
    public ResponseEntity getMainOprmngCode() throws CustomNoSuchFieldException, NoSuchFieldException {
        //List<OprmngCodeRes>

        if (httpServletRequest.getSession() == null) {
            log.info("SESSION NULL");
            return ResponseEntity.badRequest().build();
        }
        if (loginService.getUserVo() == null) {
            log.info("getMainOprmngCode NULL");
            return ResponseEntity.badRequest().build();
        }
        log.info("getOPR INIT");
        return ResponseEntity.ok(loginService.getMainOprmngCode());
    }

    @PostMapping("/adm/menu/main/oprmng/update")
    @ResponseBody
    public void getMainOprmngCodeUpdate(@RequestBody MainOprReq dto) throws CustomNoSuchFieldException, CustomNotFoundException {
        if (loginService.getUserVo() == null) {
            throw new CustomNoSuchFieldException();
        }
        log.info("getOPR INIT - {}", new Gson().toJson(dto));
        loginService.getMainOprmngCodeUpdate(dto);
    }


    @GetMapping("/adm/menu/jstree/policy/parent")
    @ResponseBody
    public Map<String, String> jstreeParentConvertToPolicy() {
        return loginService.jstreeParentConvertToPolicy();
    }

    @GetMapping("/adm/menu/role")
    @ResponseBody
    public String getRole() {
        return loginService.getUserVo().getRoleVal();
    }


    @GetMapping("/adm/main/pdfdownload")
    @ResponseBody
    public ResponseEntity<Resource> ResourceDownload(@RequestParam(name = "name") String name) throws IOException, CustomNotFoundException {
        return fileService.PdfDownload(name);
    }

    @GetMapping("/adm/main/uv")
    @ResponseBody
    public UserVo getUserVo() throws CustomNoSuchFieldException {
        if (loginService.getUserVo() == null) {
            throw new CustomNoSuchFieldException();
        } else {
            return loginService.getUserVo();
        }
    }

}


