/*
 * Copyright 2011- 2020 SDCIT Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package sgc.portal.com.sym.user.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.core.env.Environment;
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.RestController;

import sgc.portal.SgcPortalProperties;
import sgc.portal.com.sym.user.AuthenticationException;
import sgc.portal.com.sym.user.model.Login;
import sgc.portal.com.sym.user.model.LoginAs;
import sgc.portal.com.sym.user.model.PasswordChange;
import sgc.portal.com.sym.user.model.UserDetails;
import sgc.portal.com.sym.user.service.LoginService;
import sgc.portal.security.PasswordEncryptionService;
import sgc.portal.security.annotation.Secured;
import sgc.portal.security.annotation.SecurityPolicy;
import sgc.portal.security.model.BearerTokenWrapper;

/**
 * 로그인 컨트롤러.
 *
 * @author SDCIT
 * @version 1.0.0
 * @since 2020. 7. 5.
 */
@RestController
public class LoginController {
	private static final String LOGIN_PATH = "/login";
	private static final String LOGIN_PATH_AS = "/aslogin";
	private static final String LOGIN_PATH_INSA = "/loginInsa";

	@Autowired
	private LoginService loginService;

	/** 패스워드 암호화 */
	@Autowired
	private PasswordEncryptionService passwordEncryptionService;

	@Autowired
	private SgcPortalProperties props;

	@Autowired
	private MultipartProperties mp;

	@Autowired
	private Environment environment;

	@Autowired
	private BearerTokenWrapper tokenWrapper;

	/**
	 * 로그인을 위한 Skeleton code로 향후 실제 업무에 맞게 수정 필요. yongsang 2020.07.26
	 *
	 * @param login
	 *            the login
	 * @param request
	 *            the requestSecurityPolicy
	 * @return userDetails user details the exception
	 */
	@PostMapping(value = LOGIN_PATH)
	@Secured(policy = SecurityPolicy.NONE)
	public UserDetails login(@RequestBody Login login, HttpServletRequest request) {
		return loginService.login(login, request);
	}

	/**
	 * Logout.
	 *
	 * @param session
	 *            the session
	 */
	@GetMapping(value = "/logout")
	@Secured(policy = SecurityPolicy.NONE)
	public void logout(HttpSession session) {
		loginService.logOut(session);
	}

	/**
	 * Save pasword chang.
	 *
	 * @param passwordChange
	 *            the password chang
	 */
	@PostMapping(value = "/ux/main/savePasswordChange")
	@Secured(policy = SecurityPolicy.NONE)
	public void savePasswordChangePortal(@RequestBody PasswordChange passwordChange) {
		loginService.savePasswordChangePortal(passwordChange);
	}

	/**
	 * Save pasword chang.
	 *
	 * @param passwordChange
	 *            the password chang
	 */
	@PostMapping(value = "/ux/main/savePasswordChangeAs")
	@Secured(policy = SecurityPolicy.NONE)
	public void savePasswordChangeAs(@RequestBody PasswordChange passwordChange) {
		loginService.savePasswordChangeAs(passwordChange);
	}

	/**
	 * Exec password init check.
	 *
	 * @param login
	 *            the login
	 */
	// 비밀번호 초기화
	@PostMapping(value = "/main/execPasswordInitCheck")
	@Secured(policy = SecurityPolicy.NONE)
	public void execPasswordInitCheck(@RequestBody Login login) {
		loginService.execPasswordInitCheck(login);
	}

	/**
	 * 협력업체 아이디 찾기 Exec As Id Find.
	 *
	 * @param login
	 *            the login
	 */
	@PostMapping(value = "/main/execAsIdFind")
	@Secured(policy = SecurityPolicy.NONE)
	public LoginAs execAsIdFind(@RequestBody LoginAs login) {
		return loginService.execAsIdFind(login);
	}

	/**
	 * 협력업체 로그인
	 * 
	 * @param login
	 * @param request
	 * @return
	 */
	@PostMapping(value = LOGIN_PATH_AS)
	@Secured(policy = SecurityPolicy.NONE)
	public UserDetails asLogin(@RequestBody LoginAs login, HttpServletRequest request) {
		return loginService.asLogin(login, request);
	}

	/**
	 * AS Password 초기화
	 * 
	 * @param login
	 * @return
	 */
	@PostMapping(value = "/main/execAsPasswordInit")
	@Secured(policy = SecurityPolicy.NONE)
	public LoginAs execAsPasswordInit(@RequestBody LoginAs login) {
		return loginService.execAsPasswordInit(login);
	}

	/**
	 * 그룹웨어 포탈 로그인
	 * 
	 * @param login
	 * @param request
	 * @return
	 */
	@PostMapping(value = LOGIN_PATH_INSA)
	@Secured(policy = SecurityPolicy.NONE)
	public UserDetails loginInsa(@RequestBody Login login, HttpServletRequest request) {
		return loginService.loginInsa(login, request);
	}

}
