package com.oqpo.api.web.controller;

import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.SigninService;
import com.oqpo.api.util.CryptoUtil;
import com.oqpo.api.web.dto.request.oper.ntice.NticeGridRequest;
import com.oqpo.api.web.dto.request.sign.FindIdRequest;
import com.oqpo.api.web.dto.request.sign.FindPwRequest;
import com.oqpo.api.web.dto.request.sign.SigninRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.oper.ntice.NticeResponse;
import com.oqpo.api.web.dto.response.session.FindIdResponse;
import com.oqpo.api.web.dto.response.session.FindPwResponse;
import com.oqpo.api.web.dto.response.token.JwtResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/api/member")
public class SigninController {
    @Autowired
    private SigninService signinService;

    @Value("${hashed.key}")
    private String hashed;

    /**
     * 설명 : 로그인
     *
     * @param signinRequest
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "로그인")
    @PostMapping("/sign-in")
    public ResponseEntity<JwtResponse> signin(@RequestBody @Valid SigninRequest signinRequest) throws Exception {
        hashed = StringEscapeUtils.unescapeJava(hashed);
        String userId = CryptoUtil.decrypt(signinRequest.getUserId(), hashed);
        String password = CryptoUtil.decrypt(signinRequest.getPassword(), hashed);
        return ResponseEntity.ok(signinService.signin(userId, password));
    }

    /**
     * 설명 : APP 로그인
     *
     * @param signinRequest
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "로그인")
    @PostMapping("/sign-in-app")
    public ResponseEntity<JwtResponse> signinApp(@RequestBody @Valid SigninRequest signinRequest) throws Exception {
        hashed = StringEscapeUtils.unescapeJava(hashed);
        String userId = CryptoUtil.doDecrypt(signinRequest.getUserId(), hashed);
        String password = CryptoUtil.doDecrypt(signinRequest.getPassword(), hashed);
        return ResponseEntity.ok(signinService.signin(userId, password));
    }

    /**
     * 설명 : Swagger 로그인
     *
     * @param signinRequest
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "Swagger 로그인")
    @PostMapping("/sign-in-swagger")
    public ResponseEntity<JwtResponse> signinSwagger(@RequestBody @Valid SigninRequest signinRequest) throws Exception {
        //log.debug("password==>>>{}", passwordEncoder.encode(password));
        return ResponseEntity.ok(signinService.signin(signinRequest.getUserId(), signinRequest.getPassword()));
    }

    /**
     * 설명 : 로그아웃
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @CacheEvict(value = {"codeList", "authMenuList", "actionList"}, allEntries = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "로그아웃")
    @PatchMapping("/sign-out")
    public ResponseEntity<SaveResponse> logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        signinService.logout(request, response);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.LOGOUT_OK));
    }


    /**
     * 설명 : ID 찿기
     *
     * @param findIdRequest
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "ID찿기")
    @PostMapping("/find-id")
    public ResponseEntity<FindIdResponse> findId(@RequestBody @Valid FindIdRequest findIdRequest) throws Exception {
        return ResponseEntity.ok(signinService.selectMemberFindId(findIdRequest.getUserNm(), findIdRequest.getEmail(), findIdRequest.getTelNo()));
    }

    /**
     * 설명 : PW 찿기
     *
     * @param findPwRequest
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "패스워드 찿기")
    @PostMapping("/find-pw")
    public ResponseEntity<FindPwResponse> findPw(@RequestBody @Valid FindPwRequest findPwRequest) throws Exception {
        return ResponseEntity.ok(signinService.selectMemberFindPw(findPwRequest.getUserId(), findPwRequest.getEmail(), findPwRequest.getTelNo()));
    }



}
