import { URLS } from "../api/apiConfig";
import {
  runDelayedAction,
  setTokenRefreshing,
  TAxios,
  TAxiosAdvancedPromise,
} from "../api/TAxios";
import * as lunaSend from "../lunaSend";
import { types } from "./actionTypes";
import { changeLocalSettings } from "./commonActions";
import { fetchCurrentUserHomeTerms } from "./homeActions";

const MAX_RETRY_COUNT = 3;
const RETRY_DELAY = 2000; // 2 seconds

// IF-LGSP-000 인증코드 요청
export const getAuthenticationCode = () => (dispatch, getState) => {
  setTokenRefreshing(true);
  const onSuccess = (response) => {
    console.log("getAuthenticationCode onSuccess: ", response.data);
    const accessToken = response.data.data.accessToken;
    const refreshToken = response.data.data.refreshToken ?? null;

    dispatch(changeLocalSettings({ accessToken, refreshToken }));
    setTokenRefreshing(false);
    runDelayedAction(dispatch, getState);
  };

  const onFail = (error) => {
    console.error("getAuthenticationCode onFail: ", error);
    setTokenRefreshing(false);
  };

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_AUTHENTICATION_CODE,
    {},
    {},
    onSuccess,
    onFail,
    true
  );
};

// IF-LGSP-001 디바이스 등록 및 약관 동의
export const registerDevice =
  (params, onSuccessCallback, onFailCallback) => (dispatch, getState) => {
    const { agreeTerms } = params;

    const onSuccess = (response) => {
      console.log("registerDevice onSuccess: ", response.data);

      dispatch({
        type: types.REGISTER_DEVICE,
        payload: response.data.data,
        retCode: response.data.retCode,
      });
      dispatch(getAuthenticationCode());
      dispatch(fetchCurrentUserHomeTerms());
      if (onSuccessCallback) {
        onSuccessCallback(response.data);
      }
    };

    const onFail = (error) => {
      console.error("registerDevice onFail: ", error);
      if (onFailCallback) {
        onFailCallback(error);
      }
    };

    TAxios(
      dispatch,
      getState,
      "post",
      URLS.REGISTER_DEVICE,
      {},
      { agreeTerms },
      onSuccess,
      onFail,
      true
    );
  };

// 디바이스 부가 정보 저장 (IF-LGSP-002)
export const registerDeviceInfo = (params) => (dispatch, getState) => {
  const { evntTpCd, evntId, evntApplcnFlag, entryMenu, mbphNo } = params;

  const onSuccess = (response) => {
    console.log("registerDeviceInfo onSuccess: ", response.data);

    dispatch({
      type: types.REGISTER_DEVICE_INFO,
      payload: response.data,
      retCode: response.data.retCode,
    });
  };

  const onFail = (error) => {
    console.error("registerDeviceInfo onFail: ", error);
  };

  TAxios(
    dispatch,
    getState,
    "post",
    URLS.REGISTER_DEVICE_INFO,
    {},
    { evntTpCd, evntId, evntApplcnFlag, entryMenu, mbphNo },
    onSuccess,
    onFail
  );
};

// 디바이스 부가 정보 조회 IF-LGSP-003
export const getDeviceAdditionInfo = () => (dispatch, getState) => {
  const onSuccess = (response) => {
    console.log("getDeviceAdditionInfo onSuccess: ", response.data);

    dispatch({
      type: types.GET_DEVICE_INFO,
      payload: response.data,
    });
  };

  const onFail = (error) => {
    console.error("getDeviceAdditionInfo onFail: ", error);
  };

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_DEVICE_INFO,
    {},
    {},
    onSuccess,
    onFail
  );
};

// 인증번호 재요청 IF-LGSP-096
export const getReAuthenticationCode = () => (dispatch, getState) => {
  setTokenRefreshing(true);
  const onSuccess = (response) => {
    console.log("getReAuthenticationCode onSuccess: ", response.data);
    const accessToken = response.data.data.accessToken;
    dispatch(changeLocalSettings({ accessToken }));
    setTokenRefreshing(false);
    runDelayedAction(dispatch, getState);
  };

  const onFail = (error) => {
    console.error("getReAuthenticationCode onFail: ", error);
    setTokenRefreshing(false);
  };

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_RE_AUTHENTICATION_CODE,
    {},
    {},
    onSuccess,
    onFail,
    true
  );
};

export const clearRegisterDeviceInfo = () => ({
  type: types.CLEAR_REGISTER_DEVICE_INFO,
});
