package com.interplug.qcast.biz.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.interplug.qcast.biz.login.dto.JoinUser;
import com.interplug.qcast.biz.login.dto.LoginUser;
import com.interplug.qcast.biz.login.dto.UserLoginResponse;
import com.interplug.qcast.biz.login.dto.UserPassword;
import com.interplug.qcast.biz.login.dto.UserResponse;
import com.interplug.qcast.config.Exception.ErrorCode;
import com.interplug.qcast.config.Exception.QcastException;
import com.interplug.qcast.config.message.Messages;
import com.interplug.qcast.util.DefaultResponse;
import com.interplug.qcast.util.InterfaceQsp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class LoginService {
  // @Autowired LoginMapper loginMapper;
  private final LoginMapper loginMapper;
  private final InterfaceQsp interfaceQsp;

  @Value("${qsp.url}")
  private String qspUrl;

  @Autowired
  Messages message;

  /**
   * 로그인 처리
   * 
   * @param loginUser LoginUser
   * @return UserLoginResponse
   * @throws Exception
   */
  public UserLoginResponse getLogin(LoginUser loginUser) throws Exception {
    if (loginUser.getLoginId() == null || "".equals(loginUser.getLoginId()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Login Id"));

    if (loginUser.getPwd() == null || "".equals(loginUser.getPwd()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Password"));

    UserLoginResponse userLoginResponse = null;

    // QSP API Call..
    // Q.CAST 로그인
    // post
    // /api/user/login
    String strResponse =
        interfaceQsp.callApi(HttpMethod.POST, qspUrl + "/api/user/login", loginUser);
    if (!"".equals(strResponse)) {
      ObjectMapper om =
          new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      userLoginResponse = om.readValue(strResponse, UserLoginResponse.class);
    }

    return userLoginResponse;
  }

  public UserResponse getUser(LoginUser loginUser) {
    return loginMapper.getUser(loginUser);
  }

  /**
   * 가입 신청 등록
   * 
   * @param joinUser JoinUser
   * @return DefaultResponse
   * @throws Exception
   */
  public DefaultResponse joinUser(JoinUser joinUser) throws Exception {
    if (joinUser.getStoreQcastNm() == null || "".equals(joinUser.getStoreQcastNm()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Store QCast Name"));

    if (joinUser.getStoreQcastNmKana() == null || "".equals(joinUser.getStoreQcastNmKana()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Store QCast Name Kana"));

    if (joinUser.getPostCd() == null || "".equals(joinUser.getPostCd()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Postal Code"));

    if (joinUser.getAddr() == null || "".equals(joinUser.getAddr()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Address"));

    if (joinUser.getTelNo() == null || "".equals(joinUser.getTelNo()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Telephone"));

    if (joinUser.getFax() == null || "".equals(joinUser.getFax()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Fax"));

    if (joinUser.getUserInfo().getUserId() == null || "".equals(joinUser.getUserInfo().getUserId()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "User Id"));

    if (joinUser.getUserInfo().getUserNm() == null || "".equals(joinUser.getUserInfo().getUserNm()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Name"));

    // if (joinUser.getUserInfo().getUserNmKana() == null
    // || "".equals(joinUser.getUserInfo().getUserNmKana()))
    // throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
    // message.getMessage("common.message.required.data", "Name Kana"));

    if (joinUser.getUserInfo().getTelNo() == null || "".equals(joinUser.getUserInfo().getTelNo()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Telephone"));

    if (joinUser.getUserInfo().getFax() == null || "".equals(joinUser.getUserInfo().getFax()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Fax"));

    if (joinUser.getUserInfo().getEmail() == null || "".equals(joinUser.getUserInfo().getEmail()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "E-Mail"));

    // QSP API Call..
    // Q.CAST 신규 사용자 요청
    // post
    // /api/user/newUserIdReq
    DefaultResponse defaultResponse = null;
    String strResponse =
        interfaceQsp.callApi(HttpMethod.POST, qspUrl + "/api/user/newUserIdReq", joinUser);
    if (!"".equals(strResponse)) {
      ObjectMapper om =
          new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      defaultResponse = om.readValue(strResponse, DefaultResponse.class);
    }

    return defaultResponse;
  }

  /**
   * 비밀번호 초기화
   * 
   * @param userPassword UserPassword
   * @return DefaultResponse
   * @throws Exception
   */
  public DefaultResponse initPassword(UserPassword userPassword) throws Exception {
    if (userPassword.getLoginId() == null || "".equals(userPassword.getLoginId()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Login Id"));

    if (userPassword.getEmail() == null || "".equals(userPassword.getEmail()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "E-Mail"));

    // QSP API Call..
    // Q.CAST 비밀번호 초기화
    // post
    // /api/user/initUserPwd
    DefaultResponse defaultResponse = null;
    String strResponse =
        interfaceQsp.callApi(HttpMethod.POST, qspUrl + "/api/user/initUserPwd", userPassword);
    if (!"".equals(strResponse)) {
      ObjectMapper om =
          new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      defaultResponse = om.readValue(strResponse, DefaultResponse.class);
    }

    return defaultResponse;
  }

  /**
   * 비밀번호 변경
   * 
   * @param userPassword UserPassword
   * @return DefaultResponse
   * @throws Exception
   */
  public DefaultResponse changePassword(UserPassword userPassword) throws Exception {
    if (userPassword.getLoginId() == null || "".equals(userPassword.getLoginId()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Login Id"));

    if (userPassword.getChgType() == null || "".equals(userPassword.getChgType()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Change Type (I :Init , C: Change)"));

    if (userPassword.getChgPwd() == null || "".equals(userPassword.getChgPwd()))
      throw new QcastException(ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "Change Password"));

    // QSP API Call..
    // Q.CAST 비밀번호 변경
    // post
    // /api/user/userPwdChg
    DefaultResponse defaultResponse = null;
    String strResponse =
        interfaceQsp.callApi(HttpMethod.POST, qspUrl + "/api/user/userPwdChg", userPassword);
    if (!"".equals(strResponse)) {
      ObjectMapper om =
          new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      defaultResponse = om.readValue(strResponse, DefaultResponse.class);
    }

    return defaultResponse;
  }
}
