import { StringCaser } from "@/utils/utils";
import { getType } from "../utils/helpers";

export default (configuration) => {
  //TODO: what does this do?
  return `
package ${configuration.packageName}.network


import com.google.gson.Gson
import ${configuration.packageName}.model.remote.*
import ${configuration.packageName}.network.*
import org.koin.dsl.module
import okhttp3.MultipartBody

val remoteDataSourceModule = module {
    factory { RemoteDataSource(get()) }
}

class RemoteDataSource(
  private val apiService: ApiService
) : BaseDataSource() {

  suspend fun getAllUser() = getResult {
    apiService.getAllUser()
}
suspend fun createRoomRequests( request: CreateRoomRequests) = getResult {
  apiService.createRoomRequests(request)
}
  suspend fun getChats( request: ChatRequest) = getResult {
    apiService.getChats(request)
}

suspend fun getStartPool( user_id: Int) = getResult {
    apiService.getStartPool(user_id)
}

suspend fun getAllRoom( user_id: Int) = getResult {
    apiService.getAllRoom(user_id)
}

suspend fun sendMessageToBot( request: String) = getResult {
  apiService.sendMessageToBot(request)
}

suspend fun sendMessageToBot( request: ChatBotRequest) = getResult {
  apiService.sendMessageToBot(request)
}

suspend fun sendTextMessage( request: ChatTextRequest) = getResult {
    apiService.sendTextMessage(request)
}

 ${buildDataSources(configuration.api)}

}
  `;
};

const buildDataSources = (apis) => {
  let endpoints = [];
  apis.map((api) => {
    endpoints.push(`
      suspend fun ${StringCaser(api.name, {
        casetype: "camelCase",
      })}(${buildFunctionTypedArgs(api)}) = getResult {
        apiService.${StringCaser(api.name, {
          casetype: "camelCase",
        })}(${buildFunctionArgs(api)})
      }`);
  });
  return endpoints.join("\n");
};

const buildFunctionTypedArgs = (api) => {
  const body = buildBody(
    api.inputs.filter((input) => input.type == "body"),
    api
  );
  const paths = buildTypedPaths(
    api.inputs.filter((input) => input.type == "path"),
    api
  );
  const query = buildTypedQueries(
    api.inputs.filter((input) => input.type == "query"),
    api
  );
  const multipart = buildTypedMultipart(
    api.inputs.filter((input) => input.type == "multipart"),
    api
  );
  return `${body} ${paths} ${query} ${multipart}`;
};

const buildFunctionArgs = (api) => {
  const body =
    api.inputs.filter((input) => input.type == "body").length > 0
      ? "request,"
      : "";
  const paths = buildPaths(
    api.inputs.filter((input) => input.type == "path"),
    api
  );
  const query = buildQueries(
    api.inputs.filter((input) => input.type == "query"),
    api
  );
  const multipart = buildMultipart(
    api.inputs.filter((input) => input.type == "multipart"),
    api
  );
  return `${body} ${paths} ${query} ${multipart}`;
};

const buildMultipart = (inputs, api) => {
  if (inputs.length == 0) return "";
  // file: MultipartBody.Part
  return ` ${inputs
    .map((input) => `${StringCaser(input.name, { casetype: "camelCase" })}`)
    .join(", \n")}`;
};

const buildTypedMultipart = (inputs, api) => {
  if (inputs.length == 0) return "";
  //file: MultipartBody.Part
  return ` ${inputs
    .map(
      (input) =>
        ` ${StringCaser(input.name, {
          casetype: "camelCase",
        })}: MultipartBody.Part`
    )
    .join(", \n")}`;
};

const buildPaths = (inputs, api) => {
  if (inputs.length == 0) return "";
  // id: Int
  return ` ${inputs
    .map(
      (input, index) =>
        ` ${StringCaser(input.name, { casetype: "camelCase" })}${
          index === inputs.length - 1 ? "," : ""
        }`
    )
    .join(", \n")}`;
};

const buildTypedPaths = (inputs, api) => {
  if (inputs.length == 0) return "";
  //@Path("id") id: Int
  return ` ${inputs
    .map(
      (input, index) =>
        ` ${StringCaser(input.name, { casetype: "camelCase" })}: ${getType(
          input.dataType
        )}${index === inputs.length - 1 ? "," : ""}`
    )
    .join(", \n")}`;
};

const buildTypedQueries = (inputs, api) => {
  if (inputs.length == 0) return "";
  //chatId: Int
  return ` ${inputs
    .map(
      (input, index) =>
        ` ${StringCaser(input.name, { casetype: "camelCase" })}: ${getType(
          input.dataType
        )}${index === inputs.length - 1 ? "," : ""}`
    )
    .join(", \n")} `;
};

const buildQueries = (inputs, api) => {
  if (inputs.length == 0) return "";
  //chatId
  return ` ${inputs
    .map(
      (input, index) => ` ${StringCaser(input.name, { casetype: "camelCase" })}`
    )
    .join(", \n")} `;
};

const buildBody = (inputs, api) => {
  if (inputs.length == 0) return "";
  //request: TranslateRequest
  const requestName = StringCaser(api.name, { casetype: "PascalCase" });
  return ` request: ${requestName}Request, `;
};
