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

import com.kicc.common.e.i;
import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.stinfo.ItemService;
import com.oqpo.api.service.stinfo.SpplyService;
import com.oqpo.api.web.dto.request.stinfo.item.*;
import com.oqpo.api.web.dto.request.stinfo.spply.SaveSpplyRequest;
import com.oqpo.api.web.dto.request.stinfo.spply.SpplyGridRequest;
import com.oqpo.api.web.dto.request.stinfo.spply.SpplyPopGridRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.SelectResponse;
import com.oqpo.api.web.dto.response.code.CodeSearchListResponse;
import com.oqpo.api.web.dto.response.stinfo.item.BarcodeItemInfoResponse;
import com.oqpo.api.web.dto.response.stinfo.item.ItemClassLevelResponse;
import com.oqpo.api.web.dto.response.stinfo.item.ItemClassMngInfoResponse;
import com.oqpo.api.web.dto.response.stinfo.item.ItemInfoResponse;
import com.oqpo.api.web.dto.response.stinfo.itemUnit.StroeWhsListResponse;
import com.oqpo.api.web.dto.response.stinfo.itemUnit.StroeWishListResponse;
import com.oqpo.api.web.dto.response.stinfo.spply.SpplyInfoResponse;
import com.oqpo.api.web.dto.response.stockmng.BarcodeWhsItemInfoResponse;
import io.swagger.annotations.Api;
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/item")
@Api(tags = {"품목 관리 (ItemController) -- 완료"})
public class ItemController {
    @Autowired
    private ItemService itemService;

    /**
     * 설명 : 품목 팝업 그리드 리스트
     *
     * @param itemPopGridRequest
     * @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> selectPopItemGridList(@RequestBody @Valid ItemPopGridRequest itemPopGridRequest) throws Exception {
        return ResponseEntity.ok(itemService.selectPopItemGridList(itemPopGridRequest.getSBrandId(), itemPopGridRequest.getSStoreId(),
                itemPopGridRequest.getSPodrPssblDvsn(), itemPopGridRequest.getSItemNm(), itemPopGridRequest.getSSpplyId(),
                itemPopGridRequest.getSWhsId(), itemPopGridRequest.getSLocation(), itemPopGridRequest.toDTO(itemPopGridRequest)));
    }

    /**
     * 설명 : 품목 신규팝업 그리드 리스트
     *
     * @param ItemPopGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 품목 신규 팝업 그리드 리스트")
    @PostMapping("/pop-new-grid-list")
    public ResponseEntity<GridResponse> selectPopNewItemGridList(@RequestBody @Valid ItemPopGridRequest itemPopGridRequest) throws Exception {
        return ResponseEntity.ok(itemService.selectPopNewItemGridList(itemPopGridRequest.getSBrandId(), itemPopGridRequest.getSStoreId(), itemPopGridRequest.getSPodrPssblDvsn(), itemPopGridRequest.getSItemNm(), itemPopGridRequest.getSSpplyId(), itemPopGridRequest.getSWhsId(), itemPopGridRequest.getSLocation(), itemPopGridRequest.toDTO(itemPopGridRequest)));
    }

    /**
     * 설명 : 품목 그리드 리스트
     *
     * @param itemGridRequest
     * @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 ItemGridRequest itemGridRequest) throws Exception {
        return ResponseEntity.ok(itemService.selectItemGridList(itemGridRequest.getSBrandId(), itemGridRequest.getSItemStCd(),
                itemGridRequest.getSPodrPssblDvsn(), itemGridRequest.getSItemType(), itemGridRequest.getSItemGubun(),
                itemGridRequest.getSClass1(), itemGridRequest.getSClass2(), itemGridRequest.getSClass3(), itemGridRequest.getSClass4(),
                itemGridRequest.getSItemNm(), itemGridRequest.getUploadYn(), itemGridRequest.toDTO(itemGridRequest)));
    }

    /**
     * 설명 : 공급사 품목 그리드 리스트
     *
     * @param itemGridRequest
     * @return
     * @throws Exception
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 공급사 품목 그리드 리스트")
    @PostMapping("/spply-grid-list")
    public ResponseEntity<GridResponse> spplyDetailGridList(@RequestBody @Valid SpplyItemGridRequest spplyItemGridRequest) throws Exception {
        return ResponseEntity.ok(itemService.selectSpplyItemGridList(spplyItemGridRequest.getSBrandId(), spplyItemGridRequest.getSSpplyId(), spplyItemGridRequest.getSItemStCd(), spplyItemGridRequest.getSPodrPssblDvsn(), spplyItemGridRequest.getSItemNm(), spplyItemGridRequest.toDTO(spplyItemGridRequest)));
    }


    /**
     * 설명 : 품목 정보
     *
     * @param itemId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "brandId", value = "브랜드아이디", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "itemId", value = "품목아이디", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "품목 정보")
    @GetMapping("/info-item")
    public ResponseEntity<ItemInfoResponse> infoItem(@RequestParam(value = "brandId") String brandId, @RequestParam(value = "itemId") String itemId) throws Exception {
        return ResponseEntity.ok(itemService.selectItemInfo(brandId, itemId));
    }

    /**
     * 설명 : 품목 정보 저장
     *
     * @param userId
     * @param saveItemInfolRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "품목 저장")
    @PostMapping("/save-item")
    public ResponseEntity<SaveResponse> saveItem(@ApiIgnore String userId, @RequestBody @Valid SaveItemInfolRequest saveItemInfolRequest) throws Exception {
        itemService.saveItemInfo(userId, saveItemInfolRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 품목 정보 저장
     *
     * @param userId
     * @param saveItemListRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "품목 일괄 저장")
    @PostMapping("/save-item-list")
    public ResponseEntity<SaveResponse> saveItemList(@ApiIgnore String userId, @RequestBody @Valid SaveItemListRequest saveItemListRequest) throws Exception {
        itemService.saveItemList(userId, saveItemListRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 품목분류 그리드 리스트
     *
     * @param itemGridRequest
     * @return
     * @throws Exception
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 품목분류 그리드 리스트")
    @PostMapping("/itemclass-grid-list")
    public ResponseEntity<GridResponse> itemClassGridList(@RequestBody @Valid ItemClassGridRequest itemClassGridRequest) throws Exception {
        return ResponseEntity.ok(itemService.selectItemClassMngGridList(itemClassGridRequest.getSBrandId(), itemClassGridRequest.getSItemClass1(), itemClassGridRequest.getSItemClass2(), itemClassGridRequest.getSItemClass3(), itemClassGridRequest.getSItemClass4(), itemClassGridRequest.getSItemClassNm(), itemClassGridRequest.toDTO(itemClassGridRequest)));
    }


    //selectItemClassMngInfo

    /**
     * 설명 : 품목분류 정보
     *
     * @param itemId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "brandId", value = "브랜드아이디", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "itemClassMgnNo", value = "품목분류번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "품목분류 정보")
    @GetMapping("/info-itemClassMng")
    public ResponseEntity<ItemClassMngInfoResponse> infoItemClassMng(@RequestParam(value = "brandId") String brandId, @RequestParam(value = "itemClassMgnNo") String itemClassMgnNo) throws Exception {
        return ResponseEntity.ok(itemService.selectItemClassMngInfo(brandId, itemClassMgnNo));
    }


    /**
     * 설명 : 품목분류정보저장
     *
     * @param userId
     * @param saveItemClassMngInfolRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "품목분류정보 저장")
    @PostMapping("/save-itemClassMng")
    public ResponseEntity<SaveResponse> saveItemClassMng(@ApiIgnore String userId, @RequestBody @Valid SaveItemClassMngInfolRequest saveItemClassMngInfolRequest) throws Exception {
        itemService.saveItemClassMngInfo(userId, saveItemClassMngInfolRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }


    /**
     * 설명 : 품목 분류 삭제
     *
     * @param userId
     * @param DelItemInfolRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "brandId", value = "브랜드아이디", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "itemClassMgntNo", value = "품목분류번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "품목분류 삭제")
    @GetMapping("/del-itemClassMng")
    public ResponseEntity<SaveResponse> saveItemClassMng(@ApiIgnore String userId, @RequestParam(value = "brandId") String brandId, @RequestParam(value = "itemClassMgntNo") String itemClassMgntNo) throws Exception {
        itemService.delItemClassMngInfo(userId, brandId, itemClassMgntNo);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }


    /**
     * 설명 : 품목분류 레벨 검색
     *
     * @param sCodeNm
     * @param sCodeTpCd
     * @param sUseYn
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "sBrandId", value = "브랜드코드", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sItemLevel", value = "출력level", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sItemClass1", value = "품목분류level1 값", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sItemClass2", value = "품목분류level2 값", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sItemClass3", value = "품목분류level3 값", required = false, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "품목분류 level 검색")
    @GetMapping("/class-level-list")
    public ResponseEntity<ItemClassLevelResponse> searchCodeList(@RequestParam(value = "sBrandId", required = true) String sBrandId,
                                                                 @RequestParam(value = "sItemLevel", required = true) int sItemLevel,
                                                                 @RequestParam(value = "sItemClass1", required = false) String sItemClass1,
                                                                 @RequestParam(value = "sItemClass2", required = false) String sItemClass2,
                                                                 @RequestParam(value = "sItemClass3", required = false) String sItemClass3) throws Exception {
        return ResponseEntity.ok(itemService.searchItemClassList(sBrandId, sItemLevel, sItemClass1, sItemClass2, sItemClass3));
    }

    /**
     * 설명 : 바코드를 통한 창고 품목 정보조회
     *
     * @param brandId
     * @param whsId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "sBrandId", value = "브랜드아이디", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sStoreId", value = "매장아이디", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sBarcode", value = "바코드", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "바코드를 통한 창고 품목 정보조회")
    @GetMapping("/info-barcode-item")
    public ResponseEntity<BarcodeItemInfoResponse> infoBarcodeStock(@RequestParam(value = "sBrandId") String sBrandId, @RequestParam(value = "sStoreId", required = false) String sStoreId, @RequestParam(value = "sBarcode") String sBarcode) throws Exception {
        return ResponseEntity.ok(itemService.selectBarcodeItemMInfo(sBrandId, sStoreId, sBarcode));
    }


    /**
     * 설명 : 위시리스트
     *
     * @param sStoreId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "sStoreId", value = "매장아이디", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "위시리스트목록 검색")
    @GetMapping("/wish-list")
    public ResponseEntity<StroeWishListResponse> searchCodeList(@RequestParam(value = "sStoreId", required = true) String sStoreId) throws Exception {
        return ResponseEntity.ok(itemService.selectPopWishGridList(sStoreId));
    }

    /**
     * 설명 : 위시리스트 품목 팝업 그리드 리스트
     *
     * @param ItemPopWishListGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 품목 위시리스트 팝업 그리드 리스트")
    @PostMapping("/pop-grid-wishlist")
    public ResponseEntity<GridResponse> selectPopItemGridWishList(@RequestBody @Valid ItemPopWishListGridRequest itemPopWishListGridRequest) throws Exception {
        return ResponseEntity.ok(itemService.selectPopItemGridWishList(itemPopWishListGridRequest.getSBrandId(), itemPopWishListGridRequest.getSStoreId(),
                itemPopWishListGridRequest.getSWishListMgntNo(), itemPopWishListGridRequest.toDTO(itemPopWishListGridRequest)));
    }


    /**
     * 설명 : 품목 팝업 그리드 리스트
     *
     * @param itemPopGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "안전재고 팝업 품목 그리드 리스트")
    @PostMapping("/pop-lackstck-grid-list")
    public ResponseEntity<GridResponse> selectPopLackStckItemGridList(@RequestBody @Valid ItemPopLackStckGridRequest itemPopLackStckGridRequest) throws Exception {
        return ResponseEntity.ok(itemService.selectPopLackStockItemGridList(itemPopLackStckGridRequest.getSBrandId(), itemPopLackStckGridRequest.getSStoreId(),
                itemPopLackStckGridRequest.getSWhsId(), itemPopLackStckGridRequest.toDTO(itemPopLackStckGridRequest)));
    }


    /**
     * 설명 : 위시리스트 품목조회
     *
     * @param userId
     * @param
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "위시리스트 품목조회")
    @PostMapping("/select-pop-wishlist-item")
    public ResponseEntity<GridResponse> selectPopWishListItem(@ApiIgnore String userId, @RequestBody @Valid ItemPopReqWishListGridRequest itemPopReqWishListGridRequest
    ) throws Exception {
        return ResponseEntity.ok( itemService.selectPopWishListItem(userId, itemPopReqWishListGridRequest.getWishListMgntNo(), itemPopReqWishListGridRequest.getBrandId(), itemPopReqWishListGridRequest.getStoreId(),  itemPopReqWishListGridRequest.toDTO(itemPopReqWishListGridRequest) ) );
    }


    /**
     * 설명 : 창고리스트
     *
     * @param sStoreId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "sBrandId", value = "브랜드아이디", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sStoreId", value = "매장아이디", required = false, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "창고 목록 검색")
    @GetMapping("/whs-list")
    public ResponseEntity<StroeWhsListResponse> searchCodeList(@RequestParam(value = "sBrandId", required = true) String sBrandId, @RequestParam(value = "sStoreId", required = false) String sStoreId) throws Exception {
        return ResponseEntity.ok(itemService.selectPopWhsGridList(sBrandId, sStoreId));
    }
}
