package com.oqpo.api.web.controller.oper;

import com.oqpo.api.enums.DataMessageCode;
import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.oper.AfflService;
import com.oqpo.api.web.dto.request.oper.affl.AfflGridRequest;
import com.oqpo.api.web.dto.request.oper.affl.AfflShopBaseInfoGridRequest;
import com.oqpo.api.web.dto.request.oper.affl.SaveAfflRequest;
import com.oqpo.api.web.dto.response.DataResponse;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.oper.affl.AfflShopInfoResponse;
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.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/api/affl")
public class AfflController {
    @Autowired
    private AfflService afflService;

    /**
     * 설명 : 가맹점 팝업 그리드 리스트
     *
     * @param afflGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 가맹점 팝업 그리드 리스트")
    @PostMapping("/pop-grid-list")
    public ResponseEntity<GridResponse> afflPopGridList(@ApiIgnore String userId, @ApiIgnore String authTpCd, @RequestBody @Valid AfflGridRequest afflGridRequest) throws Exception {
        return ResponseEntity.ok(afflService.selectPopAfflGridList(userId, authTpCd, afflGridRequest.getAfflShopNm(), afflGridRequest.toDTO(afflGridRequest)) );
    }

    /**
     * 설명 : 가맹점 그리드 리스트
     *
     * @param afflShopBaseInfoGridRequest
     * @return
     * @throws Exception
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 가맹점 그리드 리스트")
    @PostMapping("/detail-grid-list")
    public ResponseEntity<GridResponse> detailGridList(@RequestBody @Valid AfflShopBaseInfoGridRequest afflShopBaseInfoGridRequest) throws Exception {
        return ResponseEntity.ok(afflService.selectAfflShopGridList( afflShopBaseInfoGridRequest.getFromDt(), afflShopBaseInfoGridRequest.getToDt(), afflShopBaseInfoGridRequest.getSAfflShopNm(), afflShopBaseInfoGridRequest.getSStCd(),  afflShopBaseInfoGridRequest.toDTO(afflShopBaseInfoGridRequest)));
    }


    /**
     * 설명 : 가맹정 정보
     *
     * @param afflShopId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "afflShopId", value = "가맹점번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "가맹점 정보")
    @GetMapping("/info-affl")
    public ResponseEntity<AfflShopInfoResponse> infoUser(@RequestParam(value = "afflShopId") String afflShopId) throws Exception {
        return ResponseEntity.ok(afflService.selectAfflShopInfo(afflShopId));
    }


    /**
     * 설명 : 가맹점 정보 저장
     *
     * @param userId
     * @param SaveAfflRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "가맹점정보 저장")
    @PostMapping("/save-affi")
    public ResponseEntity<SaveResponse> saveCode(@ApiIgnore String userId, @RequestBody @Valid SaveAfflRequest saveAfflRequest) throws Exception {
        afflService.saveAfflList(userId, saveAfflRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }


    /**
     * 설명 : 가맹 key 중복체크
     *
     * @param afflAbbr
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "afflAbbr", value = "가맹Key", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "가맹Key 중복 체크")
    @GetMapping("/check-duplicate")
    public ResponseEntity<DataResponse> checkDuplicate(@RequestParam(value = "afflAbbr") String afflAbbr) throws Exception {
        String dupYn = afflService.selectAfflAbbrDupYn(afflAbbr);
        DataMessageCode message = null;
        if ("Y".equals(dupYn)) {
            message = DataMessageCode.DUPLICATE_YES;
        } else {
            message = DataMessageCode.DUPLICATE_NO;
        }
        return ResponseEntity.ok(DataResponse.toDTO(message));
    }

    /**
     * 설명 : 가맹점 패스워드 초기화
     *
     * @param userId
     * @param
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "afflShopId", value = "가맹점아이디", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "가맹점 패스워드 초기화")
    @GetMapping("/inti-afflShopPw")
    public ResponseEntity<SaveResponse> initAfflShopPw(@RequestParam(value = "afflShopId") String afflShopId) throws Exception {
        afflService.updateInitUserPw(afflShopId);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.INIT_PW_OK));
    }

}
