#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <jni.h>
#include <android/log.h>
#include "IAP.h"
#include "AndroidApp.h"

//void (*)(EIAPValidationResult, int)

fn_IAP_Fun fn_IAP_Fun_5CFFDC = nullptr;
JNIEnv *JEIEnv_5CFFE0 = nullptr;
idp_product_info *iap_product_5CFFE4 = nullptr;
int dword_5CFFE8 = 0;
int max_products_5CFFEC = 0;

jclass jclass_FTTIAP = nullptr;
jmethodID FTTIAP_SetPublicKeyAndSkuCount_5CFFF0 = nullptr;
jmethodID FTTIAP_AddSku_5CFFF4 = nullptr;
jmethodID FTTIAP_QueryInventoryAsync_5CFFF8 = nullptr;
jmethodID FTTIAP_GetStatusString_5D0000 = nullptr;
jmethodID FTTIAP_SetStatusString_5CFFFC = nullptr;
jmethodID FTTIAP_ResetStatus_5D0004 = nullptr;
jmethodID FTTIAP_HasPurchased_5D0008 = nullptr;
jmethodID FTTIAP_Consume_5D000C = nullptr;
jmethodID FTTIAP_Buy_5D0010 = nullptr;
jmethodID FTTIAP_Init_5D0014 = nullptr;
jmethodID FTTIAP_GetPrice_5D0018 = nullptr;
jmethodID FTTIAP_SkuEnabled_5D001C = nullptr;
jmethodID FTTIAP_AllPurchasesDisabled_5D0024 = nullptr;
jmethodID FTTIAP_GetIAPStatusCount_5D0028 = nullptr;
jmethodID FTTIAP_GetUserData_5D0020 = nullptr;

const char *EAIAP_Stat_329BF0[] = {
    "EAIAP_NONE",
    "EAIAP_PRODUCTREQUEST",
    "EAIAP_PRODUCTREQUEST_FAILED",
    "EAIAP_PRODUCTREQUEST_SUCCESS",
    "EAIAP_PURCHASE",
    "EAIAP_PURCHASE_SUCCESS",
    "EAIAP_PURCHASE_FAILED",
    "EAIAP_CONSUME",
    "EAIAP_CONSUME_FAILED",
    "EAIAP_CONSUME_SUCCESS"};

//---------------------------------------------------------------------------------------------------------------------------------------------
//002740F8
int IAP_BuyProduct(EIAPValidationResult eis, fn_IAP_Fun fn, bool b) {
  int dret = 0;
  EIAPValidationResult v7 = EIAPValidationResult_0;
  IAP_SetStatus(EAIAP_PURCHASE);
  if (IAP_GetProductPurchased(eis)) {
    if (b) {
      IAP_SetStatus(EAIAP_PURCHASE_SUCCESS);
      v7 = (EIAPValidationResult)6;
    } else {
      IAP_SetStatus(EAIAP_PURCHASE_FAILED);
      dret = 0;
      v7 = (EIAPValidationResult)7;
    }
    fn(v7, 0);
  } else if (IAP_PurchaseProduct(eis)) {
    dret = 1;
    fn_IAP_Fun_5CFFDC = fn;
  } else {
    dret = 0;
  }
  return dret;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00274218
int IAP_CacheProductLocalPrices() {
  int dr4 = 0;
  while (dr4 < dword_5CFFE8) {
    //loc_274222
    IAP_StoreProductLocalisedPrice(dr4);
    dr4++;
  }
  return dr4;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//002741A8
bool IAP_CheckAndConsume(char *a1) {
  JNIEnv *env = AndroidApp_GetJNI();
  jclass cFTTIAP = AndroidApp_FindFTTJavaClass("FTTIAP");
  jstring s = env->NewStringUTF(a1);
  jmethodID jmid = env->GetStaticMethodID(cFTTIAP, "CheckPurchaseAndConsume", "(Ljava/lang/String;)Z");
  jboolean b = env->CallStaticBooleanMethod(cFTTIAP, jmid, s);

  env->DeleteLocalRef(s);
  env->DeleteLocalRef(cFTTIAP);
  return b != false;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00274088
const char *IAP_GetIncompletePurchase() {
  const char *dret = nullptr;
  jclass cFTTIAP = AndroidApp_FindFTTJavaClass("FTTIAP");
  jmethodID jmid = JEIEnv_5CFFE0->GetStaticMethodID(cFTTIAP, "GetIncompletePurchase", "()Ljava/lang/String;");
  jobject obj = JEIEnv_5CFFE0->CallStaticObjectMethod(cFTTIAP, jmid);
  if (obj != nullptr) {
    dret = JEIEnv_5CFFE0->GetStringUTFChars((jstring)obj, nullptr);
  } else {
    dret = nullptr;
  }

  JEIEnv_5CFFE0->DeleteLocalRef(obj);
  JEIEnv_5CFFE0->DeleteLocalRef(cFTTIAP);
  return dret;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273F70
int IAP_GetMaxProducts() {
  return max_products_5CFFEC;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273F64
int IAP_GetProductCount() {
  return dword_5CFFE8;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273F34
int IAP_GetProductIndex(char *s) {
  int dr4 = 0;
  idp_product_info *pInfo = &iap_product_5CFFE4[0];
  while (dr4 < dword_5CFFE8) {
    //loc_273F44
    if (strcmp(pInfo->name_0, s) == 0)
      return dr4;
    pInfo++;
    dr4++;
  }

  return -1;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273F20
idp_product_info *IAP_GetProductInfo(int index) {
  return &iap_product_5CFFE4[index];
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00274034
bool IAP_GetProductPurchased(char *a1) {
  jstring s = JEIEnv_5CFFE0->NewStringUTF(a1);
  jclass cFTTIAP = AndroidApp_FindFTTJavaClass("FTTIAP");
  jboolean b = JEIEnv_5CFFE0->CallStaticBooleanMethod(cFTTIAP, FTTIAP_HasPurchased_5D0008, s);
  JEIEnv_5CFFE0->DeleteLocalRef(s);
  JEIEnv_5CFFE0->DeleteLocalRef(cFTTIAP);
  return b != 0;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273C00
bool IAP_GetProductPurchased(int a1) {
  return IAP_GetProductPurchased(iap_product_5CFFE4[a1].name_0);
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273C68
int IAP_GetStatus() {
  unsigned int v0 = 0;
  const char *v1 = (const char *)IAP_GetStatusString();
  while (v0 <= sizeof(EAIAP_Stat_329BF0)) {
    if (!strcmp(v1, EAIAP_Stat_329BF0[v0]))
      return v0;
    ++v0;
  }
  return 0;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273C18
const char *IAP_GetStatusString() {
  jclass cFTTIAP = AndroidApp_FindFTTJavaClass("FTTIAP");
  jobject obj = JEIEnv_5CFFE0->CallStaticObjectMethod(cFTTIAP, FTTIAP_GetStatusString_5D0000);
  const char *p = JEIEnv_5CFFE0->GetStringUTFChars((jstring)obj, nullptr);
  JEIEnv_5CFFE0->DeleteLocalRef(obj);
  JEIEnv_5CFFE0->DeleteLocalRef(cFTTIAP);
  return p;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273CE0
void IAP_Init(char const *pkey, int count) {
  return;
  JEIEnv_5CFFE0 = AndroidApp_GetJNI();
  jclass_FTTIAP = AndroidApp_FindFTTJavaClass("FTTIAP");
  FTTIAP_SetPublicKeyAndSkuCount_5CFFF0 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "SetPublicKeyAndSkuCount", "(Ljava/lang/String;I)V");
  FTTIAP_AddSku_5CFFF4 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "AddSku", "(Ljava/lang/String;)V");
  FTTIAP_QueryInventoryAsync_5CFFF8 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "QueryInventoryAsync", "()V");
  FTTIAP_GetStatusString_5D0000 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "GetStatusString", "()Ljava/lang/String;");
  FTTIAP_SetStatusString_5CFFFC = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "SetStatusString", "(Ljava/lang/String;)V");
  FTTIAP_ResetStatus_5D0004 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "ResetStatus", "()V");
  FTTIAP_HasPurchased_5D0008 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "HasPurchased", "(Ljava/lang/String;)Z");
  FTTIAP_Consume_5D000C = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "Consume", "(Ljava/lang/String;)V");
  FTTIAP_Buy_5D0010 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "Buy", "(Ljava/lang/String;)V");
  FTTIAP_Init_5D0014 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "Init", "()V");
  FTTIAP_GetPrice_5D0018 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "GetPrice", "(Ljava/lang/String;)Ljava/lang/String;");
  FTTIAP_SkuEnabled_5D001C = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "SkuEnabled", "(Ljava/lang/String;)Z");
  FTTIAP_AllPurchasesDisabled_5D0024 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "AllPurchasesDisabled", "()Z");
  FTTIAP_GetIAPStatusCount_5D0028 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "GetIAPStatusCount", "()I");
  FTTIAP_GetUserData_5D0020 = JEIEnv_5CFFE0->GetStaticMethodID(jclass_FTTIAP, "GetUserData", "()V");

  iap_product_5CFFE4 = new idp_product_info[count];
  dword_5CFFE8 = 0;
  max_products_5CFFEC = count;
  jstring skey = JEIEnv_5CFFE0->NewStringUTF(pkey);

  JEIEnv_5CFFE0->CallStaticVoidMethod(jclass_FTTIAP, FTTIAP_SetPublicKeyAndSkuCount_5CFFF0, skey, count);
  JEIEnv_5CFFE0->DeleteLocalRef(skey);
  JEIEnv_5CFFE0->DeleteLocalRef(jclass_FTTIAP);
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273BA8
int IAP_PurchaseProduct(int a1) {
  if (IAP_GetProductPurchased(a1))
    return 0;

  JEIEnv_5CFFE0 = AndroidApp_GetJNI();
  jstring s = JEIEnv_5CFFE0->NewStringUTF(iap_product_5CFFE4[a1].name_0);
  jclass cFTTIAP = AndroidApp_FindFTTJavaClass("FTTIAP");
  JEIEnv_5CFFE0->CallStaticVoidMethod(cFTTIAP, FTTIAP_Buy_5D0010, s);
  JEIEnv_5CFFE0->DeleteLocalRef(cFTTIAP);
  return 1;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00274178
void IAP_RefreshPurchases() {
  jclass cFTTIAP = AndroidApp_FindFTTJavaClass("FTTIAP");
  JEIEnv_5CFFE0->CallStaticVoidMethod(cFTTIAP, FTTIAP_QueryInventoryAsync_5CFFF8);
  JEIEnv_5CFFE0->DeleteLocalRef(cFTTIAP);
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273C94
void IAP_SetStatus(EAndroidIAPState es) {
  jclass cFTTIAP = AndroidApp_FindFTTJavaClass("FTTIAP");
  const char *p = EAIAP_Stat_329BF0[es];
  jstring s = JEIEnv_5CFFE0->NewStringUTF(p);
  JEIEnv_5CFFE0->CallStaticVoidMethod(cFTTIAP, FTTIAP_SetStatusString_5CFFFC, s);
  JEIEnv_5CFFE0->DeleteLocalRef(cFTTIAP);
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273F7C
int IAP_SkuEnabled(int a) {
  return 1;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00274148
void IAP_Start() {
  jclass cFTTIAP = AndroidApp_FindFTTJavaClass("FTTIAP");
  JEIEnv_5CFFE0->CallStaticVoidMethod(cFTTIAP, FTTIAP_Init_5D0014);
  JEIEnv_5CFFE0->DeleteLocalRef(cFTTIAP);
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00273B20
void IAP_StoreProductLocalisedPrice(int index) {
  jstring sname = JEIEnv_5CFFE0->NewStringUTF(iap_product_5CFFE4[index].name_0);
  jclass cFTTIAP = AndroidApp_FindFTTJavaClass("FTTIAP");

  jobject d = JEIEnv_5CFFE0->CallStaticObjectMethod(cFTTIAP, FTTIAP_GetPrice_5D0018);
  if (d) {
    const char *pS = JEIEnv_5CFFE0->GetStringUTFChars(sname, nullptr);
    UTF8ToUnicode(iap_product_5CFFE4[index].wbuf_24, pS, 16);
    JEIEnv_5CFFE0->ReleaseStringUTFChars((jstring)d, pS);
    JEIEnv_5CFFE0->DeleteLocalRef(sname);
  }
  //loc_273B92

  JEIEnv_5CFFE0->DeleteLocalRef(cFTTIAP);
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//00274238
void IAP_Update() {
  //empty
}
//---------------------------------------------------------------------------------------------------------------------------------------------
