package com.style24.admin.biz.web;

import java.util.Collection;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.rest.server.GagaResponse;

import com.style24.persistence.domain.Withdraw;
import com.style24.admin.biz.service.TsaDeliveryService;
import com.style24.admin.biz.service.TsaRendererService;
import com.style24.admin.biz.service.TsaWithdrawService;
import com.style24.admin.support.controller.TsaBaseController;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.support.message.TscMessageByLocale;

/**
 * 회수관리 Controller
 * 
 * @author moon
 * @since 2020. 11. 16
 */
@Controller
@RequestMapping("/withdraw")
@Slf4j
public class TsaWithdrawController extends TsaBaseController {

	@Autowired
	private TsaWithdrawService withdrawService;

	@Autowired
	private TsaRendererService rendererService;

	@Autowired
	private TsaDeliveryService deliveryService;

	@Autowired
	private TscMessageByLocale message;

	/**
	 * 회수등록 화면
	 * 
	 * @return
	 * @throws Exception
	 * @author rladbwnd5
	 * @since 2020. 02. 10
	 */
//	@RequestMapping(value = "/register/form")
//	public ModelAndView withdrawRegisterForm() throws Exception {
//		ModelAndView mav = new ModelAndView();
//
//		// 택배사명 목록
//		mav.addObject("shipCompanyList", rendererService.getShipCompanyList());
//
//		// 회수요청사유
//		mav.addObject("wdReqReasonList", rendererService.getAvailCommonCodeList("G688"));
//
//		mav.setViewName("withdraw/WithdrawRegisterTriForm");
//
//		return mav;
//	}

	/**
	 * 회수등록 - 조회(송장번호용)
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author rladbwnd5
	 * @since 2020. 02. 11
	 */
//	@PostMapping("/register/invoicelist")
//	@ResponseBody
//	public Collection<Withdraw> getWithdrawRegisterInvoiceList(@RequestBody Withdraw withdraw) {
//		Collection<Withdraw> dataList = withdrawService.getWithdrawRegisterInvoiceList(withdraw);
//		return dataList;
//	}

	/**
	 * 회수등록 - 조회(상품코드용)
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author rladbwnd5
	 * @since 2020. 02. 12
	 */
//	@PostMapping("/register/goodslist")
//	@ResponseBody
//	public Collection<Withdraw> getWithdrawRegisterGoodsList(@RequestBody Withdraw withdraw) {
//
//		log.debug("goodsCd = {} ", withdraw.getGoodsCd());
//		Collection<Withdraw> dataList = withdrawService.getWithdrawRegisterGoodsList(withdraw);
//		return dataList;
//	}

	/**
	 * 회수등록 - 등록처리
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author rladbwnd5
	 * @since 2020. 02. 12
	 */
//	@PostMapping("/register/create")
//	@ResponseBody
//	public GagaResponse createWithdrawInfo(@RequestBody Collection<Withdraw> withdrawList) {
//		Withdraw withdraw = withdrawList.iterator().next();
//		String goodsCd = withdraw.getGoodsCd();
//		withdraw.setGoodsCd(null);
//		//withdraw.setOrdNo(null);
//		Collection<Withdraw> withdrawCount = withdrawService.getWithdrawRegisterList(withdraw);
//		withdraw.setGoodsCd(goodsCd);
//
//		if (CollectionUtils.isEmpty(withdrawCount)) {
//			withdrawService.createWithdrawInfo(withdrawList);
//		} else {
//			return super.error("이미 등록된 회수송장번호가 존재합니다.");
//		}
//
//		log.debug("withdraw = {}", withdrawList);
//
//		return super.ok(message.getMessage("SUCC_0001"));
//	}

	/**
	 * 회수송장번호 변경 팝업 화면
	 * 
	 * @return
	 * @throws Exception
	 * @author rladbwnd5
	 * @since 2020. 02. 27
	 */
//	@RequestMapping(value = "/register/wdinvoicenochange/form")
//	public ModelAndView wdInvoiceNoChangeForm() throws Exception {
//		ModelAndView mav = new ModelAndView();
//
//		mav.setViewName("withdraw/WdInvoiceNoChange");
//
//		return mav;
//	}

	/**
	 * 회수송장번호 변경
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author rladbwnd5
	 * @since 2020. 02. 27
	 */
//	@PostMapping("/register/withdrawinvoiceno/update")
//	@ResponseBody
//	public GagaResponse updateRegisterWdInvoiceNo(@RequestBody Withdraw withdraw) {
//		withdrawService.updateRegisterWdInvoiceNo(withdraw);
//		return super.ok(message.getMessage("SUCC_0002"));
//	}

	/**
	 * 환불관리 목록 화면
	 * 
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2020. 11. 16
	 */
	@RequestMapping(value = "/refund/form")
	public ModelAndView withdrawRefundForm() throws Exception {
		ModelAndView mav = new ModelAndView();

		// 사이트구분
		mav.addObject("siteCdList", rendererService.getAvailCommonCodeList("G000"));

		mav.setViewName("withdraw/RefundRegisterListForm");

		return mav;
	}

	/**
	 * 환불관리 목록
	 * 
	 * @param  withdraw
	 * @return Collection<Withdraw>
	 * @author moon
	 * @since 2020. 11. 16
	 */
	@PostMapping("/refund/list")
	@ResponseBody
	public Collection<Withdraw> getRefundList(@RequestBody Withdraw withdraw) {
		return withdrawService.getRefundList(withdraw);
	}

	/**
	 * 환불관리 회수 데이터 삭제처리
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2020. 11. 16
	 */
	@PostMapping(value = "/refund/Withdraw/delete")
	@ResponseBody
	public GagaResponse deleteWithdrawInfo(@RequestBody Withdraw withdraw) {
		withdrawService.deleteWithdrawInfo(withdraw);
		return super.ok(message.getMessage("SUCC_0003"));
	}

	/**
	 * 회수등록 팝업 화면
	 * 
	 * @param
	 * @return 회수등록 팝업 화면
	 * @throws Exception
	 * @author rladbwnd5
	 * @since 2020. 03. 03
	 */
//	@GetMapping("/register/popup/form")
//	@ResponseBody
//	public ModelAndView withdrawRegisterPopupForm(Withdraw withdraw) {
//		ModelAndView mav = new ModelAndView();
//
//		mav.addObject("wdInvoiceNo", withdraw.getWdInvoiceNo());
//
//		mav.addObject("goodsCd", withdraw.getGoodsCd());
//
//		mav.addObject("sizeCd", withdraw.getSizeCd());
//
//		if (StringUtils.isNotBlank(withdraw.getOrdDtlNo().toString())) {
//			mav.addObject("ordDtlNo", withdraw.getOrdDtlNo());
//		}
//
//		// 주문상세 상태
//		mav.addObject("ordDtlStatList", rendererService.getAvailCommonCodeList("G013"));
//
//		mav.setViewName("withdraw/WithdrawRegisterPopupForm");
//		return mav;
//	}


	/**
	 * 회수등록 팝업 목록
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author rladbwnd5
	 * @since 2020. 03. 03
	 */
//	@PostMapping("/register/popup/list")
//	@ResponseBody
//	public Collection<Withdraw> getWithdrawRegisterPopupList(@RequestBody Withdraw withdraw) {
//		Collection<Withdraw> dataList = withdrawService.getWithdrawRegisterPopupList(withdraw);
//		return dataList;
//	}


	/**
	 * 회수등록팝업 주문매핑(송장번호 수정)
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author rladbwnd5
	 * @since 2020. 03. 03
	 */
//	@PostMapping("/register/popup/wdinvoiceno/update")
//	@ResponseBody
//	public GagaResponse updateOrderChangeWdInvoiceNo(@RequestBody Withdraw withdraw) {
//		withdrawService.updateOrderChangeWdInvoiceNo(withdraw);
//		return super.ok(message.getMessage("SUCC_0002"));
//	}

	/**
	 * 회수등록팝업 주문매핑(주문상세일련번호 수정)
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author rladbwnd5
	 * @since 2020. 03. 03
	 */
//	@PostMapping("/register/popup/orddtlno/update")
//	@ResponseBody
//	public GagaResponse updateWithdrawOrdDtlNo(@RequestBody Withdraw withdraw) {
//		withdrawService.updateWithdrawOrdDtlNo(withdraw);
//		return super.ok(message.getMessage("SUCC_0002"));
//	}

	/**
	 * 환불관리 상세 화면
	 * 
	 * @param ordNo
	 * @return 회수 상세 화면
	 * @throws Exception
	 * @author moon
	 * @since 2020. 11. 16
	 */
	@GetMapping("/refund/detail/form")
	@ResponseBody
	public ModelAndView refundDetailForm(Withdraw withdraw) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("ordChgSq", withdraw.getOrdChgSq());

		// 남은 상품 정보
		mav.addObject("remainGoodsInfo", withdrawService.getRemainGoodsInfo(withdraw));

		// 남은 상품 목록
		mav.addObject("remainGoodsList", withdrawService.getWithdrawRemainOrderDetailList(withdraw));

		// 장바구니 쿠폰 정보
		mav.addObject("couponInfo", withdrawService.getCouponInfo(withdraw));

		// 주문전체 금액
		mav.addObject("orderDetailSumAmt", withdrawService.getOrderDetailSumAmt(withdraw.getOrdNo().toString()));

		//
		mav.addObject("sumQty", withdrawService.getSumChgQtyWdQty(withdraw));

		// TODO: 아래 확인후 구현 (.. ) ( ..)
		//mav.addObject("ordChgCnt", deliveryService.getOrdChgCnt(withdraw.getOrdNo()));
		mav.setViewName("withdraw/WithdrawDetailForm");
		return mav;
	}

	/**
	 * 환불관리상세 정보
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2020. 11. 16
	 */
	@PostMapping("/detail/list")
	@ResponseBody
	public Collection<Withdraw> getRefundDetailList(@RequestBody Withdraw withdraw) {
		Collection<Withdraw> dataList = withdrawService.getRefundDetailList(withdraw);
		return dataList;
	}

	/**
	 * 환불관리상세 - 회수처리
	 * 
	 * @param withdraw
	 * @return GagaResponse
	 * @author moon
	 * @since 2020. 11. 16
	 */
	@PostMapping("/detail/confirm")
	@ResponseBody
	public GagaResponse refundDetailConfirm(@RequestBody Withdraw withdraw) {
		withdrawService.refundDetailConfirm(withdraw);
		return super.ok(message.getMessage("SUCC_0004"));
	}

	/**
	 * 회수송장전송목록 화면
	 * 
	 * @return ModelAndView
	 * @throws Exception
	 * @author Daehyoung
	 * @since 2020. 02. 25
	 */
//	@RequestMapping(value = "/invoice/send/list/form")
//	public ModelAndView invoiceSendListForm() throws Exception {
//		ModelAndView mav = new ModelAndView();
//
//		// 사이트구분
//		mav.addObject("siteCdList", rendererService.getAvailCommonCodeList("G000"));
//
//		mav.setViewName("withdraw/InvoiceSendListForm");
//
//		return mav;
//	}

	/**
	 * 환불완료 화면
	 * 
	 * @return ModelAndView
	 * @throws Exception
	 * @author yujung
	 * @since 2020. 05. 29
	 */
//	@RequestMapping(value = "/refundcomplete/list/form")
//	public ModelAndView refundCompleteListListForm() throws Exception {
//		ModelAndView mav = new ModelAndView();
//
//		mav.setViewName("withdraw/RefundCompleteListForm");
//
//		return mav;
//	}

	/**
	 * 환불완료 목록
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author yujung
	 * @since 2020. 05. 29
	 */
//	@PostMapping("/refundcomplete/list")
//	@ResponseBody
//	public Collection<Withdraw> getRefundCompleteList(@RequestBody Withdraw withdraw) {
//		Collection<Withdraw> dataList = withdrawService.getRefundCompleteList(withdraw);
//		return dataList;
//	}

}
