package com.style24.front.biz.web;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.style24.core.biz.service.TscEnvsetService;
import com.style24.core.biz.service.TscKakaotalkService;
import com.style24.core.support.env.TscConstants;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.front.biz.service.TsfCustomerService;
import com.style24.front.biz.service.TsfDisplayService;
import com.style24.front.support.controller.TsfBaseController;
import com.style24.front.support.security.session.TsfSession;
import com.style24.persistence.domain.Customer;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 앱 관련 Controller
 *
 * @author jsshin
 * @since 2021. 05. 21
 */
@Controller
@RequestMapping("/app")
@Slf4j
public class TsfAppController extends TsfBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TsfCustomerService customerService;

	@Autowired
	private TscEnvsetService envsetService;

	@Autowired
	private TscKakaotalkService kakaoService;

	@Autowired
	private TsfDisplayService displayService;

	/**
	 * 앱 설정 화면
	 *
	 * @author jsshin
	 * @since 2020. 5. 11
	 */
	@GetMapping("/setting/form")
	public ModelAndView getSettingForm() {
		ModelAndView mav = new ModelAndView();

		// 정책에 등록된 앱버전
		if ("I".equals(TsfSession.getAttribute("osType"))) {
			mav.addObject("regAppVersion", envsetService.getIosAppVersion(TscConstants.Site.STYLE24.value()));
		} else if ("A".equals(TsfSession.getAttribute("osType"))) {
			mav.addObject("regAppVersion", envsetService.getAosAppVersion(TscConstants.Site.STYLE24.value()));
		}

		// 앱푸시수신동의 가져오기
		mav.addObject("appAgreeYn", customerService.getAppAgreeYn());
		mav.addObject("appMkAgreeYn", customerService.getAppMkAgreeYn());
		mav.addObject("isLogin", TsfSession.isLogin());
		mav.setViewName("mob/app/SettingFormMob");
		return mav;
	}

	/**
	 * 앱푸시 수신동의
	 *
	 * @param  customer - 수신동의 여부
	 * @author jsshin
	 * @since 2021. 05. 20
	 */
	@PostMapping("/appagree/update")
	@ResponseBody
	public GagaMap updateAppAgreeYn(@RequestBody Customer customer) {
		GagaMap result = new GagaMap();
		int resultCnt = customerService.updateAppAgreeYn(customer.getAppAgreeYn());
		result.setInt("resultCnt", resultCnt);
		return result;
	}

	/**
	 * 앱마케팅 수신동의
	 *
	 * @param  customer - 수신동의 여부
	 * @author jsshin
	 * @since 2021. 05. 20
	 */
	@PostMapping("/mkagree/update")
	@ResponseBody
	public GagaMap updateMkAgreeYn(@RequestBody Customer customer) {
		GagaMap result = new GagaMap();
		int resultCnt = customerService.updateMkAgreeYn(customer.getMkAgreeYn());
		result.setInt("resultCnt", resultCnt);
		return result;
	}

	/**
	 * 앱푸시, 앱마케팅 수신동의
	 *
	 * @param  customer - 수신동의 여부
	 * @author jsshin
	 * @since 2021. 06. 22
	 */
	@PostMapping("/push/update")
	@ResponseBody
	public GagaMap updateAppPush(@RequestBody Customer customer) {
		GagaMap result = new GagaMap();
		int appResultCnt = customerService.updateAppAgreeYn(customer.getAppAgreeYn());
		int mkResultCnt = customerService.updateMkAgreeYn(customer.getMkAgreeYn());
		result.setInt("mkResultCnt", mkResultCnt);
		result.setInt("appResultCnt", appResultCnt);
		return result;
	}

	/**
	 * 앱알림 화면
	 *
	 * @author jsshin
	 * @since 2020. 5. 11
	 */
	@GetMapping("/notice/form")
	public ModelAndView getNoticeForm() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("isLogin", TsfSession.isLogin());
		mav.setViewName("mob/app/NoticeFormMob");

		return mav;
	}

	/**
	 * 스플래시이미지URL 조회
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 21
	 */
	@GetMapping("/splash/image/url")
	@ResponseBody
	public String getSplashImageUrl() {
		return displayService.getSplashImageUrl();
	}

	/**
	 * 앱다운로드URL 전송
	 * @param cellPhnno - 휴대전화번호
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 21
	 */
	@PostMapping("/download/url/send")
	@ResponseBody
	public GagaResponse sendAppDownloadUrl(@RequestParam(value = "cellPhnno") String cellPhnno) {
		kakaoService.sendAppDownloadUrl(cellPhnno);
		return super.ok(message.getMessage("SUCC_0005"));
	}

}
