package com.style24.admin.biz.web;

import java.util.Collection;

import org.apache.commons.lang3.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 com.fasterxml.jackson.databind.ObjectMapper;
import com.style24.admin.biz.service.TsaRendererService;
import com.style24.admin.biz.service.TsaSettleService;
import com.style24.admin.support.controller.TsaBaseController;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.persistence.domain.AflinkFee;
import com.style24.persistence.domain.DelvFeeSettle;
import com.style24.persistence.domain.GoodsSettle;

import lombok.extern.slf4j.Slf4j;

/**
 * 정산 Controller
 *
 * @author jaewonHo
 * @since 2020. 10. 22
 */
@Controller
@RequestMapping("/settle")
@Slf4j
public class TsaSettleController extends TsaBaseController {

	@Autowired
	private TsaSettleService settleService;

	@Autowired
	private TsaRendererService rendererService;

	@Autowired
	private ObjectMapper mapper;

	/**
	 * 상품정산 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 22
	 */
	@GetMapping("/goods/form")
	public ModelAndView goodsSettleForm() {
		ModelAndView mav = new ModelAndView();

		// 몰구분
		mav.addObject("mallGbList", rendererService.getCommonCodeList("G011"));

		// 외부몰벤더 콤보박스 목록
		mav.addObject("vendorList", rendererService.getCommonCodeList("G003"));

		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TsaSession.getInfo().getSupplyCompCd()));

		// 유통구분
		mav.addObject("distributionGbList", rendererService.getCommonCodeList("G065"));

		mav.setViewName("settle/GoodsSettleForm");

		return mav;
	}

	/**
	 * 상품정산 목록
	 * @param goodsSettle - 상품정산 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 7. 5
	 */
	@PostMapping("/goods/list")
	@ResponseBody
	public Collection<GoodsSettle> getGoodsSettleList(@RequestBody GoodsSettle goodsSettle) {
		if (!StringUtils.isBlank(goodsSettle.getSupplyCompList())) {
			try {
				String[] arrSupplyComp = mapper.readValue(goodsSettle.getSupplyCompList(), String[].class);
				goodsSettle.setMultiSupplyComp(arrSupplyComp);
			} catch (Exception e) {
				throw new IllegalStateException("업체코드 검색중 오류로 인해 조회되지 않았습니다.");
			}
		}

		if (!StringUtils.isBlank(goodsSettle.getBrandList())) {
			try {
				String[] arrBrandCd = mapper.readValue(goodsSettle.getBrandList(), String[].class);
				goodsSettle.setMultiBrand(arrBrandCd);
			} catch (Exception e) {
				throw new IllegalStateException("브랜드코드 검색중 오류로 인해 조회되지 않았습니다.");
			}
		}

		return settleService.getGoodsSettleList(goodsSettle);
	}

	/**
	 * 배송비정산 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 22
	 **/
	@GetMapping("/delivery/fee/form")
	public ModelAndView deliveryFeeSettleForm() {
		ModelAndView mav = new ModelAndView();

		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TsaSession.getInfo().getSupplyCompCd()));

		// 유통구분
		mav.addObject("distributionGbList", rendererService.getCommonCodeList("G065"));

		mav.setViewName("settle/DeliveryFeeSettleForm");

		return mav;
	}

	/**
	 * 배송비정산 목록
	 * @param delvFeeSettle - 배송비정산 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 7. 6
	 */
	@PostMapping("/delivery/fee/list")
	@ResponseBody
	public Collection<DelvFeeSettle> getDeliveryFeeSettleList(@RequestBody DelvFeeSettle delvFeeSettle) {
		if (!StringUtils.isBlank(delvFeeSettle.getSupplyCompList())) {
			try {
				String[] arrSupplyComp = mapper.readValue(delvFeeSettle.getSupplyCompList(), String[].class);
				delvFeeSettle.setMultiSupplyComp(arrSupplyComp);
			} catch (Exception e) {
				e.printStackTrace();
				throw new IllegalStateException("업체코드 검색중 오류로 인해 조회되지 않았습니다.");
			}
		}

		if (!StringUtils.isBlank(delvFeeSettle.getBrandList())) {
			try {
				String[] arrBrandCd = mapper.readValue(delvFeeSettle.getBrandList(), String[].class);
				delvFeeSettle.setMultiBrand(arrBrandCd);
			} catch (Exception e) {
				e.printStackTrace();
				throw new IllegalStateException("브랜드코드 검색중 오류로 인해 조회되지 않았습니다.");
			}
		}

		return settleService.getDeliveryFeeSettleList(delvFeeSettle);
	}

	/**
	 * 정산확정관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 22
	 */
	@GetMapping("/confirm/form")
	public ModelAndView settleConfirmForm() {
		ModelAndView mav = new ModelAndView();

		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TsaSession.getInfo().getSupplyCompCd()));

		// 유통구분
		mav.addObject("distributionGbList", rendererService.getCommonCodeList("G065"));

		// 은행
		mav.addObject("bankList", rendererService.getCommonCodeList("G940"));

		mav.setViewName("settle/SettleConfirmForm");

		return mav;
	}

	/**
	 * 업체별정산내역 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 22
	 */
	@GetMapping("/supply/company/form")
	public ModelAndView supplyCompanySettleForm() {
		ModelAndView mav = new ModelAndView();

		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TsaSession.getInfo().getSupplyCompCd()));

		mav.setViewName("settle/SupplyCompanySettleForm");

		return mav;
	}

	/**
	 * PG입금정산 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 22
	 */
	@GetMapping("/pg/deposit/form")
	public ModelAndView paygateDepositForm() {
		ModelAndView mav = new ModelAndView();

		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TsaSession.getInfo().getSupplyCompCd()));

		// PG구분
		mav.addObject("pgGbList", rendererService.getCommonCodeList("G015"));

		// 결제수단
		mav.addObject("payMeansList", rendererService.getCommonCodeList("G014"));

		mav.setViewName("settle/PgDepositSettleForm");

		return mav;
	}

	/**
	 * 상품권정산 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 22
	 */
	@GetMapping("/giftcard/form")
	public ModelAndView giftcardForm() {
		ModelAndView mav = new ModelAndView();

		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TsaSession.getInfo().getSupplyCompCd()));

		// PG구분
		mav.addObject("pgGbList", rendererService.getCommonCodeList("G015"));

		// 결제수단
		mav.addObject("payMeansList", rendererService.getCommonCodeList("G014"));

		mav.setViewName("settle/GiftcardSettleForm");

		return mav;
	}

	/**
	 * 제휴채널정산 화면
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 20
	 */
	@GetMapping("/aflink/fee/form")
	public ModelAndView afLinkFeeForm() {
		ModelAndView mav = new ModelAndView("settle/AfLinkSettleForm");

		// 제휴채널 목록
		mav.addObject("afChannelList", rendererService.getAvailCommonCodeList("G053"));

		return mav;
	}

	/**
	 * 제휴채널정산 목록
	 * @param aflinkFee - 제휴채널수수료 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 20
	 */
	@PostMapping("/aflink/fee/list")
	@ResponseBody
	public Collection<AflinkFee> getAfLinkFeeList(@RequestBody AflinkFee aflinkFee) {
		return settleService.getAfLinkFeeList(aflinkFee);
	}

}
