package com.style24.admin.biz.web;

import java.util.Collection;

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.style24.admin.biz.service.TsaOcmService;
import com.style24.admin.biz.service.TsaRendererService;
import com.style24.admin.support.controller.TsaBaseController;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.persistence.domain.Extmall;
import com.style24.persistence.domain.ExtmallNoti;
import com.style24.persistence.domain.ExtmallOrigin;
import com.style24.persistence.domain.ExtmallPriceSync;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 영업망관리 Controller
 *
 * @author gagamel
 * @since 2020. 11. 5
 */
@Controller
@RequestMapping("/ocm")
@Slf4j
public class TsaOcmController extends TsaBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TsaOcmService ocmService;

	@Autowired
	private TsaRendererService rendererService;

	/**
	 * 제휴몰관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 5
	 */
	@GetMapping("/extmall/form")
	public ModelAndView extmallForm() {
		ModelAndView mav = new ModelAndView();

		// 제휴몰벤더
		mav.addObject("vendorList", rendererService.getCommonCodeList("G003", "Y", TsaSession.getInfo().getSupplyCompCd()));

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

		mav.setViewName("ocm/ExtmallForm");

		return mav;
	}

	/**
	 * 제휴몰 목록
	 * @param extmall - 제휴몰 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 5
	 */
	@PostMapping("/extmall/list")
	@ResponseBody
	public Collection<Extmall> getExtmallList(@RequestBody Extmall extmall) {
		return ocmService.getExtmallList(extmall);
	}

	/**
	 * 제휴몰 등록/수정 처리
	 * @param extmall - 제휴몰 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 5
	 */
	@PostMapping("/extmall/save")
	@ResponseBody
	public GagaResponse saveExtmall(@RequestBody Extmall extmall) {
		ocmService.saveExtmall(extmall);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 제휴몰고시정보관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 6
	 */
	@GetMapping("/extmall/notiinfo/form")
	public ModelAndView extmallNotiinfoForm() {
		ModelAndView mav = new ModelAndView();

		// 제휴몰벤더
		mav.addObject("vendorList", rendererService.getCommonCodeList("G003", "Y", TsaSession.getInfo().getSupplyCompCd()));

		mav.setViewName("ocm/ExtmallNotiinfoForm");

		return mav;
	}

	/**
	 * 제휴몰고시정보 목록
	 * @param extmallNoti - 제휴몰고시정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 6
	 */
	@PostMapping("/extmall/notiinfo/list")
	@ResponseBody
	public Collection<ExtmallNoti> getExtmallNotiinfoList(@RequestBody ExtmallNoti extmallNoti) {
		return ocmService.getExtmallNotiinfoList(extmallNoti);
	}

	/**
	 * 제휴몰원산지 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 12
	 */
	@GetMapping("/extmall/origin/form")
	public ModelAndView extmallOriginForm() {
		ModelAndView mav = new ModelAndView();

		// 제휴몰벤더
		mav.addObject("vendorList", rendererService.getCommonCodeList("G003", "Y", TsaSession.getInfo().getSupplyCompCd()));

		mav.setViewName("ocm/ExtmallOriginForm");

		return mav;
	}

	/**
	 * 제휴몰원산지 목록
	 * @param extmallOrigin - 제휴몰원산지 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 12
	 */
	@PostMapping("/extmall/origin/list")
	@ResponseBody
	public Collection<ExtmallOrigin> getExtmallOriginList(@RequestBody ExtmallOrigin extmallOrigin) {
		return ocmService.getExtmallOriginList(extmallOrigin);
	}

	/**
	 * 제휴몰가격연계관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 7
	 */
	@GetMapping("/extmall/price/sync/form")
	public ModelAndView extmallPriceSyncForm() {
		ModelAndView mav = new ModelAndView();

		// 제휴몰벤더
		mav.addObject("vendorList", rendererService.getCommonCodeList("G003", "Y", TsaSession.getInfo().getSupplyCompCd()));

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

		mav.setViewName("ocm/ExtmallPriceSyncForm");

		return mav;
	}

	/**
	 * 제휴몰가격연계 목록
	 * @param extmallPriceSync - 제휴몰가격연계 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 7
	 */
	@PostMapping("/extmall/price/sync/list")
	@ResponseBody
	public Collection<ExtmallPriceSync> getExtmallPriceSyncList(@RequestBody ExtmallPriceSync extmallPriceSync) {
		return ocmService.getExtmallPriceSyncList(extmallPriceSync);
	}

	/**
	 * 제휴몰가격연계 등록/수정 처리
	 * @param extmallPriceSyncList - 제휴몰가격연계 목록
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 7
	 */
	@PostMapping("/extmall/price/sync/save")
	@ResponseBody
	public GagaResponse saveExtmallPriceSync(@RequestBody Collection<ExtmallPriceSync> extmallPriceSyncList) {
		if (extmallPriceSyncList == null || extmallPriceSyncList.isEmpty()) {
			throw new IllegalStateException(message.getMessage("FAIL_1001"));
		}

		ocmService.saveExtmallPriceSync(extmallPriceSyncList);
		return super.ok(message.getMessage("SUCC_0001"));
	}

}
