import appinfo from "../../webos-meta/appinfo.json";
import { handleBypassLink } from "../App/bypassLinkHandler";
import { store } from "../store/store";
import * as Config from "../utils/Config";
import LS2Request from "./LS2Request";

export const getSystemInfo = (
  parameters,
  { onSuccess, onFailure, onComplete }
) => {
  if (
    typeof window === "object" &&
    window.PalmSystem &&
    process.env.REACT_APP_MODE !== "DEBUG"
  ) {
    return new LS2Request().send({
      service: "luna://com.webos.service.tv.systemproperty",
      method: "getSystemInfo",
      subscribe: false,
      parameters: parameters,
      onSuccess,
      onFailure,
      onComplete,
    });
  } else {
    onSuccess({ returnValue: true, sdkVersion: "local" });
    onComplete();
  }
};
export const getDeviceId = (
  parameters,
  { onSuccess, onFailure, onComplete }
) => {
  if (
    typeof window === "object" &&
    window.PalmSystem &&
    process.env.REACT_APP_MODE !== "DEBUG"
  ) {
    return new LS2Request().send({
      service: "luna://com.webos.service.sm",
      method: "deviceid/getIDs",
      subscribe: false,
      parameters: parameters,
      onSuccess,
      onFailure,
      onComplete,
    });
  } else {
    onSuccess({
      returnValue: true,
      idList: [{ idValue: Config.DEBUG_WINDOW_DEVICEID, idtype: "LGUDID" }],
    });
    onComplete();
  }
};
export const getLoginUserData = (
  parameters,
  { onSuccess, onFailure, onComplete }
) => {
  if (
    typeof window === "object" &&
    window.PalmSystem &&
    process.env.REACT_APP_MODE !== "DEBUG"
  ) {
    return new LS2Request().send({
      service: "luna://com.webos.service.accountmanager",
      method: "getLoginID",
      subscribe: false,
      parameters: parameters,
      onSuccess,
      onFailure,
      onComplete,
    });
  } else {
    const serverType = store.getState().localSettings.serverType;

    const userData =
      serverType === "prd"
        ? {
            id: "prdtest@yopmail.com",
            inactiveID: "",
            lastSignInUserNo: "US2412306099093",
            userData: {
              sdpUserNumber: "80995804",
              needToSetNickname: false,
              adultAuthStatus: "needAge",
              serverId: "prdtest@yopmail.com",
              countryCode: "US",
              userNumber: "US2412306099093",
              defaultNick: false,
              isRequiredTerm: false,
              id: "prdtest@yopmail.com",
              userID: "prdtest@yopmail.com",
              serviceList: [
                {
                  svcName: "LG Smart World(TV)",
                  svcCode: "SVC301",
                  joinDate: "02-13-2024",
                  isService: "Y",
                },
                {
                  svcName: "MC OAuth",
                  svcCode: "SVC710",
                  joinDate: "02-13-2024",
                  isService: "Y",
                },
              ],
              profileNick: "LGEUSER0",
              iconNick: "L",
              needToCheckTerms: false,
              accountType: "LGE",
              profileBg: "#7360E7",
            },
          }
        : {
            id: "twin@t-win.kr",
            inactiveID: "",
            lastSignInUserNo: "US2210240095608",
            userData: {
              sdpUserNumber: "80995804",
              needToSetNickname: false,
              adultAuthStatus: "needAge",
              serverId: "twin@t-win.kr",
              countryCode: "US",
              userNumber: "US2210240095608",
              defaultNick: false,
              isRequiredTerm: false,
              id: "twin@t-win.kr",
              userID: "twin@t-win.kr",
              serviceList: [
                {
                  svcName: "LG Smart World(TV)",
                  svcCode: "SVC301",
                  joinDate: "02-13-2024",
                  isService: "Y",
                },
                {
                  svcName: "MC OAuth",
                  svcCode: "SVC710",
                  joinDate: "02-13-2024",
                  isService: "Y",
                },
              ],
              profileNick: "LGEUSER0",
              iconNick: "L",
              needToCheckTerms: false,
              accountType: "LGE",
              profileBg: "#7360E7",
            },
          };

    onSuccess({
      subscribed: false,
      returnValue: true,
      multipleLoginUserData: [],
      ...userData,
    });

    onComplete && onComplete();
  }
};

export const launchMembershipApp = (
  returnPath,
  { onSuccess, onFailure, onComplete }
) => {
  if (
    typeof window === "object" &&
    window.PalmSystem &&
    process.env.REACT_APP_MODE !== "DEBUG"
  ) {
    return new LS2Request().send({
      service: "luna://com.webos.applicationManager",
      method: "launch",
      subscribe: false,
      parameters: {
        id: "com.webos.app.membership",
        params: {
          query: "login",
          id: window.PalmSystem.identifier ?? appinfo.id,
          reLaunch: "Y",
          returnTo: {
            bypass: returnPath,
          },
        },
      },
      onSuccess,
      onFailure,
      onComplete,
    });
  }
};
