package com.oqpo.api.web.controller;

import com.oqpo.api.service.SessionService;
import com.oqpo.api.web.dto.response.session.HashedResponse;
import com.oqpo.api.web.dto.response.session.MsessionResponse;
import com.oqpo.api.web.dto.response.session.SessionBrandListResponse;
import com.oqpo.api.web.dto.response.session.SessionResponse;
import com.oqpo.api.web.dto.response.stinfo.brand.BsnsBrandListResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@RequestMapping("/api/session")
public class SessionController {
    @Autowired
    private SessionService sessionService;

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

    /**
     * 설명 : Hashed 키 전송
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "Hashed 키 전송!")
    @GetMapping("/hashed")
    public ResponseEntity<HashedResponse> hashed() throws Exception {
        return ResponseEntity.ok(HashedResponse.toDTO(hashed));
    }

    /**
     * 설명 : Jwt Token 만료
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @CacheEvict(value = {"codeList", "authMenuList", "actionList"}, allEntries = true)
    @ApiOperation(value = "Jwt Token 만료")
    @PatchMapping("/expire")
    public ResponseEntity<Void> expire(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return new ResponseEntity<Void>(HttpStatus.OK);
    }

    /**
     * 설명 : Hashed 키 전송 (via Jwt)
     *
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "Hashed 키 전송(via Jwt)")
    @GetMapping("/hashedKey")
    public ResponseEntity<HashedResponse> hashedKey() throws Exception {
        return ResponseEntity.ok(HashedResponse.toDTO(hashed));
    }

    /**
     * 설명 : 메뉴 전체 트리 리스트
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "기본정보 전송")
    @GetMapping("/data")
    public ResponseEntity<SessionResponse> data(HttpServletRequest request) throws Exception {
        return ResponseEntity.ok(sessionService.data(request));
    }

    /**
     * 설명 : 모바일 데이타 정보
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "모바일 기본정보 전송")
    @GetMapping("/data-app")
    public ResponseEntity<SessionResponse> mdata(HttpServletRequest request) throws Exception {
        return ResponseEntity.ok(sessionService.mdata(request));
    }


    /**
     * 설명 : 모바일 사업자번호 브랜드리스트
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bsnsRegNo", value = "사업자 번호", required = false, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "사업자번호 브랜드리스트")
    @GetMapping("/bsnsbrand")
    public ResponseEntity<SessionBrandListResponse> getBsnsBrandList(@RequestParam(value = "bsnsRegNo", required = true) String bsnsRegNo) throws Exception {
        return ResponseEntity.ok(sessionService.bsnsBrandList(bsnsRegNo));
    }


}
