package com.style24.front.biz.web;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.Device;
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.gagaframework.web.parameter.GagaMap;
import com.style24.core.support.env.TscConstants;
import com.style24.front.biz.service.TsfSocialService;
import com.style24.front.support.controller.TsfBaseController;
import com.style24.front.support.security.session.TsfSession;
import com.style24.persistence.TscPageRequest;
import com.style24.persistence.domain.Plan;
import com.style24.persistence.domain.Social;

import lombok.extern.slf4j.Slf4j;

/**
 * 소셜(핫딜) Controller
 * 
 * @author sowon
 * @since 2021. 3. 11
 */
@Controller
@RequestMapping("/social")
@Slf4j
public class TsfSocialController extends TsfBaseController {
	
	@Autowired
	private TsfSocialService socialService;
	/**
	 * 소셜(핫딜) 메인 화면
	 * 
	 * @return
	 * @author sowon
	 * @since 2021. 3. 11
	 */
	@GetMapping("/main/form")
	public ModelAndView socialMainForm(Social social, Device device) throws Exception {
		ModelAndView mav = new ModelAndView();
		
		// 디바이스 set
		social.setFrontGb(TsfSession.getFrontGb());
		social.setCustGb(TsfSession.getCustGb());
		social.setSiteCd(TscConstants.Site.STYLE24.value());
		
		// 소셜(핫딜) 
		mav.addObject("socialInfo", socialService.getSocialInfo(social));
		
		// 소셜(핫딜)-상품목록
		//mav.addObject("socialGoods", socialService.getSocialGoodsList(social));
	
		mav.setViewName(super.getDeviceViewName("social/SocialMainForm"));
		return mav;
	}
	
	/**
	 * 소셜 상품 리스트
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 4. 29
	 */
	@PostMapping("/list")
	@ResponseBody
	public GagaMap getSocialList(@RequestBody Social social) {
		GagaMap result = new GagaMap();
		TscPageRequest pageable = new TscPageRequest((social.getPageNo() > 0 ? social.getPageNo() - 1 : 0), social.getPageSize(), social.getPageUnit());
		social.setFrontGb(TsfSession.getFrontGb());
		social.setCustGb(TsfSession.getCustGb());
		social.setSiteCd(TscConstants.Site.STYLE24.value());
		int totalCnt =  socialService.getSocialGoodsList(social).size();
		pageable.setTotalCount(totalCnt);
		social.setPageable(pageable);
		
		
		
		result.set("paging", social);
		result.set("totalCnt", totalCnt);
		result.set("endRow", pageable.getEndRow());
		result.set("dataList", socialService.getSocialGoodsList(social));

		return result;
	}
	

}
