import { DeviceType } from '@ucap-webmessenger/core';
import {
  APIRequest,
  APIResponse,
  APIEncoder,
  APIDecoder,
  ParameterUtil
} from '@ucap-webmessenger/api';
import { SyncMode } from '../types/sync-mode.type';

export interface VersionInfo2Request extends APIRequest {
  userSeq?: number;
  deviceType: DeviceType;
  token?: string;
  companyGroupType: string;
  companyCode: string;
  loginId: string;
}

export interface VersionInfo2Response extends APIResponse {
  protocolCode?: number;
  syncMode?: SyncMode;
  appVersion?: string;
  installUrl?: string;
  serverIp?: string;
  uploadUrl?: string;
  downloadUrl?: string;
  profileUploadUrl?: string;
  profileRoot?: string;
  fileTerm?: number;
  fileAllowSize?: number;
  authIp?: boolean;
  launcherAppVersion?: string;
  launcherInstallUrl?: string;
}

const versionInfo2EncodeMap = {
  userSeq: 'p_user_seq',
  deviceType: 'p_device_type',
  token: 'p_token',
  companyGroupType: 'p_comp_group_type',
  companyCode: 'p_comp_code',
  loginId: 'p_login_id'
};

export const encodeVersionInfo2: APIEncoder<VersionInfo2Request> = (
  req: VersionInfo2Request
) => {
  return ParameterUtil.encode(versionInfo2EncodeMap, req);
};

export const decodeVersionInfo2: APIDecoder<VersionInfo2Response> = (
  res: any
) => {
  return {
    statusCode: res.StatusCode,
    errorMessage: res.ErrorMessage,
    appVersion: res.AppVer,
    authIp: res.AuthIP === 'Y' ? true : false,
    downloadUrl: res.DownloadURL,
    fileAllowSize: Number(res.FileAllowSize),
    fileTerm: Number(res.FileTerm),
    installUrl: res.InstallURL,
    launcherAppVersion: res.LauncherAppVer,
    launcherInstallUrl: res.LauncherInstallURL,
    profileRoot: res.ProfileRoot,
    profileUploadUrl: res.ProfileUploadURL,
    protocolCode: Number(res.ProtocolCD),
    serverIp: res.ServerIP,
    syncMode: res.SyncMode as SyncMode,
    uploadUrl: res.UploadURL
  } as VersionInfo2Response;
};
