#include "CFTTBatchModelManager.h"
#include "CFTTBatchModelBin.h"
#include "CFTTDevice.h"
#include "CFTTGraphicsDevice.h"
#include "CFTTJobQueue.h"
#include "CFTTMaterialManager.h"
#include "CFTTShaderBuiltInConstants.h"
#include "CFTTSkinningTask.h"
#include "TFTTCacheLookup.h"
#include "memctrl.h"

CFTTBatchModelManager *GFXPLAYER_pBatch = nullptr;

struct UseBy_dword_5C9840_Sub {
  CFTTJobQueue *p_cfttjobqueue_0;
  CFTTBatchModelManager *field_4;
  CFTTSkinningTask *field_8;
  int field_c;
  int field_10;
};

struct UseBy_dword_5C9840 {
  int field_0;
  int field_4;
  int field_8;
  int field_c;
  int field_10;
  UseBy_dword_5C9840_Sub useby_dword_5c9840_sub_14[4];
};

MYSTATIC UseBy_dword_5C9840 dword_5C9840;
MYSTATIC CFTTSkinningTask cfttskinningtask_5AA834[1024];

//0020FEFC
//乱写的
MYSTATIC void sub_20FEFC(CFTTJob *p_cfttjob_r0_arg, void *p_v_r1_arg) {
}

//乱写的
struct TFTTBatchVertexShadow {
  int un_0;
};

//乱写的
struct TFTTBatchVertexBump {
  int un_0;
};

//乱写的
struct TFTTBatchVertex {
  int un_0;
};

//00216940
//乱写的
void ProcessBonedShadowVertsCount(uint ui_r0_arg,
                                  CFTTMatrix32 const *p_cfttmatrix32_r1_arg,
                                  float const *p_f_r2_arg,
                                  TFTTBatchVertexShadow *p_tfttbatchvertexshadow_r3_arg,
                                  void const *p_v_arg_0,
                                  uint ui_arg_4) {
  float f_tmp = ui_r0_arg + p_cfttmatrix32_r1_arg->d[0][0] + *p_f_r2_arg +
                p_tfttbatchvertexshadow_r3_arg->un_0 + (int)p_v_arg_0 + ui_arg_4;
  LOGI("ProcessBonedShadowVertsCount f_tmp: %f", f_tmp);
}

//00210CCC
//乱写的
void ProcessBonedVertsBumpCount(uint ui_r0_arg,
                                CFTTMatrix32 const *p_cfttmatrix32_r1_arg,
                                CFTTMatrix32 const *p_cfttmatrix32_r2_arg,
                                TFTTBatchVertexBump *p_tfttbatchvertexbump_r3_arg,
                                void const *p_v_arg_0,
                                uint ui_arg_4,
                                TUV const *p_tuv_arg_8) {
  float f_tmp = ui_r0_arg + p_cfttmatrix32_r1_arg->d[0][0] + p_cfttmatrix32_r2_arg->d[0][0] +
                p_tfttbatchvertexbump_r3_arg->un_0 + (int)p_v_arg_0 + ui_arg_4 + p_tuv_arg_8->x;
  LOGI("ProcessBonedVertsBumpCount f_tmp: %f", f_tmp);
}

//002122EC
//乱写的
void ProcessBonedVertsCount(uint ui_r0_arg,
                            CFTTMatrix32 const *p_cfttmatrix32_r1_arg,
                            CFTTMatrix32 const *p_cfttmatrix32_r2_arg,
                            TFTTBatchVertex *p_tfttbatchvertex_r3_arg,
                            void const *p_v_arg_0,
                            uint ui_arg_4,
                            TUV const *p_tuv_arg_8) {
  float f_tmp = ui_r0_arg + p_cfttmatrix32_r1_arg->d[0][0] + p_cfttmatrix32_r2_arg->d[0][0] +
                p_tfttbatchvertex_r3_arg->un_0 + (int)p_v_arg_0 + ui_arg_4 + p_tuv_arg_8->x;
  LOGI("ProcessBonedVertsCount f_tmp: %f", f_tmp);
}

//00215E70
//乱写的
void ProcessShadowVertsCount(uint ui_r0_arg,
                             CFTTMatrix32 const *p_cfttmatrix32_r1_arg,
                             float const *p_f_r2_arg,
                             TFTTBatchVertexShadow *p_tfttbatchvertexshadow_r3_arg,
                             void const *p_v_arg_0,
                             uint ui_arg_4) {
  float tmp = ui_r0_arg + p_cfttmatrix32_r1_arg->d[0][0] + (*p_f_r2_arg) +
              p_tfttbatchvertexshadow_r3_arg->un_0 + ((int)p_v_arg_0) + ui_arg_4;
  LOGI("ProcessShadowVertsCount %f", tmp);
}

//00214CFC
//乱写的
void ProcessRigidVertsCount(uint ui_r0_arg,
                            CFTTMatrix32 const *p_cfttmatrix32_r1_arg,
                            CFTTMatrix32 const *p_cfttmatrix32_r2_arg,
                            TFTTBatchVertex *p_tfttbatchvertex_r3_arg,
                            void const *p_v_arg_0,
                            uint ui_arg_4,
                            TUV const *p_tuv_arg_8) {
  float tmp = ui_r0_arg + p_cfttmatrix32_r1_arg->d[0][0] + p_cfttmatrix32_r2_arg->d[0][0] +
              p_tfttbatchvertex_r3_arg->un_0 + ((int)p_v_arg_0) + ui_arg_4 + p_tuv_arg_8->x;
  LOGI("ProcessRigidVertsCount %f", tmp);
}

//00213594
//乱写的
void ProcessRigidVertsBumpCount(uint ui_r0_arg,
                                CFTTMatrix32 const *p_cfttmatrix32_r1_arg,
                                CFTTMatrix32 const *p_cfttmatrix32_r2_arg,
                                TFTTBatchVertexBump *p_tfttbatchvertexbump_r3_arg,
                                void const *p_v_arg_0,
                                uint ui_arg_4,
                                TUV const *p_tuv_arg_8) {
  float tmp = ui_r0_arg + p_cfttmatrix32_r1_arg->d[0][0] + p_cfttmatrix32_r2_arg->d[0][0] +
              p_tfttbatchvertexbump_r3_arg->un_0 + ((int)p_v_arg_0) + ui_arg_4 + p_tuv_arg_8->x;
  LOGI("ProcessRigidVertsBumpCount %f", tmp);
}

//-------------------------------------------------------------------------------------------------
//0020F240 //^_- 经过单元测试，功能正常
//0020F2B6 指令顺序不同
//0020F2DE 指令顺序不同
CFTTBatchModelManager::CFTTBatchModelManager(int d_r1_arg, bool b_r2_arg,
                                             int d_r3_arg, unsigned int ud_arg_0,
                                             bool b_arg_4, bool b_arg_8)
    : cfttbmmmatrixcache_0(b_arg_8, 0x200, 0) {
}
//-------------------------------------------------------------------------------------------------
//0020F378 //^_^
CFTTBatchModelManager::~CFTTBatchModelManager() {
}
//-------------------------------------------------------------------------------------------------
//0020F414 //^_^
bool CFTTBatchModelManager::PerformFlushOnFull() {
  return false;
}
//-------------------------------------------------------------------------------------------------
//0020F450 //^_^
void CFTTBatchModelManager::End() {
}
//-------------------------------------------------------------------------------------------------
//0020F480 //^_^
void CFTTBatchModelManager::Begin() {
}
//-------------------------------------------------------------------------------------------------
//0020F4EC //^_-
//单元测试等级: 目测游戏表现
//单元测试内容: 启动游戏到第2关（包括第2关）
//单元测试结果: 正常
//已测试分支:
//  0020F524
//  0020F558
void CFTTBatchModelManager::FlushThreaded() {
}
//-------------------------------------------------------------------------------------------------
//0020F5B4 //^_^
void CFTTBatchModelManager::Flush(int i_r1_arg, bool b_r2_arg, bool b_r3_arg) {
}
//-------------------------------------------------------------------------------------------------
//0020F670 //^_^
void CFTTBatchModelManager::ResetCache(bool b_r1_arg) {
}
//-------------------------------------------------------------------------------------------------
//0020F68C //^_^
void CFTTBatchModelManager::SetMatrix(CFTTMatrix32 const &p_cfttmatrix32_r1_arg) {
}
//-------------------------------------------------------------------------------------------------
//0020F694 //^_-
//单元测试等级: 目测游戏表现
//单元测试内容: 启动游戏到第2关（包括第2关）
//单元测试结果: 正常
//已测试分支:
//  0020F6D0
//  0020F6FA
//  0020F726
//  0020F72E
//  0020F79E
//  0020F6BC
//  0020F6C6
//  0020F7E4
int CFTTBatchModelManager::AddModelVerts(int i_r1_arg,
                                         CFTTModel *p_cfttmodel_r2_arg,
                                         int i_r3_arg,
                                         CFTTMatrix32 const *p_cfttmatrix32_arg_0,
                                         TUV *p_tuv_arg_4,
                                         TFTTCacheLookup tfttcachelookup_arg_8,
                                         callback_1 callback_arg_10,
                                         CFTTMatrix32 const *p_cfttmatrix32_arg_14) {
  return 0;
}
//-------------------------------------------------------------------------------------------------
//0020F8E4 //^_-
//单元测试等级: 目测游戏表现
//单元测试内容: 启动游戏到第2关（包括第2关）
//单元测试结果: 正常
//已测试分支:
//  0020F944
//  0020F97C
//  0020F994
//  0020F9B6
//  0020F91E
//  0020FA14
//  0020FA24
//  0020FA00
//  0020FA04
void CFTTBatchModelManager::SkinVertsBoned(CFTTModel *p_cfttmodel_r1_arg,
                                           int i_r2_arg, float *p_f_r3_arg,
                                           void *p_v_arg_0,
                                           CFTTMatrix32 const *p_cfttmatrix32_arg_4,
                                           CFTTBMMMatrixCache::StartCount cfttbmmmatrixcache_startcount_arg_8,
                                           CFTTBMMMatrixCache::StartCount cfttbmmmatrixcache_startcount_arg_10,
                                           TUV *p_tuv_arg_18,
                                           TFTTCacheLookup tfttcachelookup_arg_1c,
                                           callback_1 callback_1_arg_24) {
}
//-------------------------------------------------------------------------------------------------
//0020FA32 //^_^
void CFTTBatchModelManager::CopyVerts(CFTTModel *p_cfttmodel_r1_arg,
                                      int i_r2_arg, float *p_f_r3_arg,
                                      void *p_v_arg_0,
                                      CFTTMatrix32 const *p_cfttmatrix32_arg_4,
                                      CFTTBMMMatrixCache::StartCount cfttbmmmatrixcache_startcount_arg_8,
                                      CFTTBMMMatrixCache::StartCount cfttbmmmatrixcache_startcount_arg_10,
                                      TUV *p_tuv_arg_18,
                                      TFTTCacheLookup tfttcachelookup_arg_1c,
                                      callback_1 callback_1_arg_24) {
}
//-------------------------------------------------------------------------------------------------
//0020FAD8 //^_-
//单元测试等级: 目测游戏表现
//单元测试内容: 启动游戏到第2关（包括第2关）
//单元测试结果: 正常
//已测试分支:
//  0020FB00
//  0020FB0A
//  0020FB3C
//  0020FB4E
//  0020FB60
//  0020FCB6
//  0020FCC0
//  0020FCCE
//  0020FCD4
//  0020FCD8
//  0020FB76
//  0020FB80
//  0020FBCC
//  0020FBA2
//  0020FBD8
//  0020FB20
void CFTTBatchModelManager::AddModel(CFTTModel *p_cfttmodel_r1_arg,
                                     CFTTMatrix32 const *p_cfttmatrix32_r2_arg,
                                     TUV *p_tuv_r3_arg, TFTTCacheInstance tfttcacheinstance_arg_0,
                                     int i_arg_4, int i_arg_8,
                                     callback_1 callback_arg_c,
                                     CFTTMatrix32 const *p_cfttmatrix32_arg_10) {
  // __android_log_print(ANDROID_LOG_INFO, "sh_log", "CFTTBatchModelManager::AddModel end");
}
//-------------------------------------------------------------------------------------------------
//0020FD4A //^_-
//单元测试等级: 目测游戏表现
//单元测试内容: 启动游戏到第2关（包括第2关）
//单元测试结果: 正常
//已测试分支:
//  0020FD6A
//  0020FD76
//  0020FD8A
//  0020FD90
//  0020FD9C
//  0020FDA4
//  0020FDD6
void CFTTBatchModelManager::AddModelX(CFTTModel *p_cfttmodel_r1_arg,
                                      int i_r2_arg,
                                      CFTTMatrix32 const *p_cfttmatrix32_r3_arg,
                                      TUV *p_tuv_arg_0,
                                      callback_1 callback_1_arg_4,
                                      CFTTMatrix32 const *p_cfttmatrix32_arg_8) {
}
//-------------------------------------------------------------------------------------------------
//0020FE74
void CFTTBatchModelManager::SkinVertsBlend(CFTTModel *, int, float *, void *, CFTTMatrix32 const *, CFTTBMMMatrixCache::StartCount, CFTTBMMMatrixCache::StartCount, TUV *, TFTTCacheLookup, void (*)(void *, CFTTModel *, int, CFTTMatrix32 const *)) {
}
//-------------------------------------------------------------------------------------------------
//0020FE76
void CFTTBatchModelManager::SkinVerts(CFTTModel *, int, float *, void *, CFTTMatrix32 const *, CFTTBMMMatrixCache::StartCount, CFTTBMMMatrixCache::StartCount, TUV *, TFTTCacheLookup, void (*)(void *, CFTTModel *, int, CFTTMatrix32 const *)) {
}
//-------------------------------------------------------------------------------------------------
//0020FE6E //^_^
void CFTTBatchModelManager::SetFlushOnFullCB(pFlushOnFullCBCallBack pFlushOnFullCBCallBack_r1, void *p_v_r2) {
  this->field_D4 = pFlushOnFullCBCallBack_r1;
  this->field_D8 = p_v_r2;
}
//-------------------------------------------------------------------------------------------------
//0020F494 经过单元测试，功能正常
//^_^-
//0020F4D8处我的代码只取了一次this->field_70，而原始代码取了两次
CFTTBatchModelBin *CFTTBatchModelManager::AddBin(CFTTBatchModelBin *p_cfttbatchmodelbin_r1_arg,
                                                 int d_r2_arg,
                                                 uint ud_r3_arg,
                                                 int d_arg_0,  //shader mat id
                                                 int d_arg_4, bool b_arg_8, bool b_arg_c) {
  return 0;
}
//-------------------------------------------------------------------------------------------------
//目前还没有实现给dword_5C9840.field_4、dword_5C9840.field_8等字段赋值的函数
//接上，编译器优化会导致这些字段没了，所以自己写一个函数防止编译器优化
void TestUseBy_dword_5C9840() {
  printf("%p", &dword_5C9840.field_0);
  printf("%p", &dword_5C9840.field_4);
  printf("%p", &dword_5C9840.field_8);
  printf("%p", &dword_5C9840.field_c);
  // printf("%p", &dword_5C9840.useby_dword_5c9840_sub_14[0].p_cfttjobqueue_0);
}
//-------------------------------------------------------------------------------------------------
//0020F1EC //^_^
void CFTTBatchModelManager::InitialiseCodePath() {
}
//-------------------------------------------------------------------------------------------------