// CStageManager - [2020-04-13 11:57:47]
#ifndef _CSTAGEMANAGER_H_
#define _CSTAGEMANAGER_H_

#include "common.h"
#include "TStageOperation.h"
#include "global_func.h"
#include "TPhoto.h"
#include "CGfxPitch.h"
/*
AI 关卡

001EA500 CStageManager::Init()
001EA538 CStageManager::SetupStage(char const*,int)
001EA5C4 CStageManager::SetupStage(TStoryStage *)
001EA5F4 CStageManager::InitTeams()
001EA878 CStageManager::SetupSingleNISDefault(char const*)
001EA918 CStageManager::SetupSingleNIS(TStageOperation *,bool)
001EA9C0 CStageManager::SetupSingleNIS(char const*,int,int,int)
001EAA28 CStageManager::AddCommentary(int)
001EAA78 CStageManager::PlayStage(bool,bool)
001EAB9C CStageManager::InitElement()
001EAD24 CStageManager::InitStadium(int)
001EAD44 CStageManager::SetUserPlayer(int,bool,ECharModelType)
001EAF90 CStageManager::Process()
001EB1AC CStageManager::ProcessElementVideo()
001EB65C CStageManager::UpdateEnvRot(int)
001EB7C0 CStageManager::ProcessElement()
001EBE7C CStageManager::RenderHUD()
001EBEC0 CStageManager::GetPlayableIndex(int)
001EBEE0 CStageManager::IsFirstPlayableElement()
001EBF14 CStageManager::BackToFirstPlayableElement()
001EBFA8 CStageManager::NextElement(bool)
001EC078 CStageManager::CheckPostStageOp()
001EC0EC CStageManager::EnergyUsedOnFail()
001EC0F6 CStageManager::HasUsedEnergy()
001EC100 CStageManager::UpdateObjectives()
001EC48C CStageManager::GetMinutes()
001EC53C CStageManager::IsInternational(ETournamentType)
001EC548 CStageManager::LogAttempt(bool)
*/

class CStoryHUD;

enum ETournamentType : uint {
  ETournamentType_0,
  ETournamentType_1,
  ETournamentType_2,
  ETournamentType_3,
  ETournamentType_4,
  ETournamentType_5,
};

struct TStageObjective {
  int Type_0;
  int Data_4;
};

// 0x118
struct MagCover_ST {
  // wchar_t filed_0[0x7f];
  wchar_t filed_0[128];  //??? 可能有错误，需要后期验证 @wu
  float FontSize_100;
  int Col_104;
  int Align_108;
  int VertAlign_10c;
  int WrapWidth_110;
  bool UpperCase_114;
};

struct Commentary_st {
  int CommentaryTick_0;
  int Commentary_4;
};

// 0x2F0
struct Element_ST {
  wchar_t Minute_0[0x10];
  int Type_20;
  int TextID_24;
  wchar_t Text_28[0x100];
  bool ShowPrevText_228;
  Commentary_st Commentarys_22c[4];
  bool ShowSuccess_24c;

  union {
    char NISFile_250[0x80];
    char GOALFile_250[0x80];
  } NISFile_GOALFile_250;

  union {
    bool Skippable_2d0;
    int Version_2d0;
  } Skippable_Version_2d0;

  int Team_2d4;

  union {
    int Player0_2d8;
    int UserPlayer_2d8;
  } Player0_UserPlayer_2d8;

  union {
    int Player1_2dc;
    bool SimpleCelebrate_2dc;
  } Player1_SimpleCelebrate_2dc;

  union {
    int UserPlayer_2e0;
    int ShowFrames_2e0;
  } UserPlayer_ShowFrames_2e0;

  union {
    int UserModel_2e4;
    int Camera_2e4;
  } UserModel_Camera_2e4;

  int TutorialType_CrowdSFX_2e8;
  int CrowdSFXTick_2ec;
};

//??? 此处Player0_a0 偏移信息计算的不对
// 0xB0
struct PostOperation_st {
  int Operation_0;
  int Data0_4;
  int Data1_8;
  int Data2_c;
  int Commentary_10;
  int Music_14;
  char NIS_18[0x80];
  int Player0_98;
  int Player1_9c;
  int UserPlayer_a0;
  int UserModel_a4;
  int CrowdSFX_a8;
  int CrowdSFXTick_ac;
};

// 0x494C
struct TStruct_494C {
  wchar_t Headline_3b8[0x80];
  wchar_t SubHead_4b8[0x80];
  TStageObjective objectives_5b8[3];
  int filed_5d0;
  Element_ST Element_5d4[0x18];
  TStageOperation PostOperation_4c54;
};

struct TStruct_84 {
  int Version_0;
  int field_4;
  int field_8;
  int field_C;
  bool EnergyEnabled_10;
  bool RewindEnabled_11;
  bool PreSelectEnabled_12;
  bool OffsideEnabled_13;
  bool RestartOnFail_14;
  bool UserCaptain_15;
  int MusicIntro_18;
  int MusicOutro_1c;
  int Venue_20;
  int OppoTeamType_24;
  int filed_28;
  int OpponentTeam_2c;
  wchar_t Location_30[0x20];
  int TournamentType_70;
  int MatchType_74;
  int Weather_78;
  int SleeveLength_7c;
  int TimeOfDay_80;
};

// 4D04
struct TStoryStage {
  TStruct_84 t_0;
  MagCover_ST magCover_84[2];
  TPoint NextMatchPos_2B4;
  TStoryPhoto Photo_2bc;
  TStruct_494C tStr_3B8;

  // 0013F280 ^_^
  TStoryStage &operator=(TStoryStage const &other) {
    memcpy(&t_0, &other.t_0, sizeof(t_0));
    memcpy(&magCover_84, &other.magCover_84, sizeof(magCover_84));
    if (this != &other) {
      NextMatchPos_2B4 = other.NextMatchPos_2B4;
    }

    memcpy(&Photo_2bc, &other.Photo_2bc, sizeof(Photo_2bc));
    memcpy(&tStr_3B8, &other.tStr_3B8, sizeof(tStr_3B8));  // 494C
    return *this;
  };
};

//<0x190>
struct TEnvConfigStadium {
  char field_0[10][0x20];
  int field_140[8];
  int field_160;
  int field_164;
  char text_168[0x28];
};

//<0x38>
struct TEnvConfigItem {
  char text_0[0x20];
  TPoint3D point_20;
  int flag_2C;
  int field_30;
  float field_34;
};

//<0xfdc>
struct TEnvConfig {
  wchar_t text_0[24];
  TEnvConfigItem field_30[25];

  char field_5A8[0x888];
  int field_E30;
  TEnvConfigStadium stadium_E34;
  uint field_FC4;
  int field_FC8;
  int field_FCC;
  int field_FD0;
  EPitchPattern pattern_FD4;
  int field_FD8;
};

struct CStageManager_5CF0 {
  // uchar field_0[0x024B];
  uchar field_0[32];
  int field_20;         // 001E4BA8
  uchar field_24[552];  //这里少写了一个字节, 加上~
  bool field_024C;      // 001E51DE
  char field_024D[3];
  uchar field_0250[0x94];
  int field_02E4;
  int field_02E8;  // 001ED226
};

// sizeof:8
struct STMedal {
  char c0;
  char c1;
  char c2;
  char c3;
  char c4;
  char c5;
  char c6;
  char c7;
};

class CStoryHUD;
// 0x5e18
class CStageManager {
public:
  void Init();                                       //001EA500
  int SetupStage(char const *, int);                //001EA538
  void SetupStage(struct TStoryStage *);             //001EA5C4
  void InitTeams();                                  //001EA5F4
  void SetupSingleNISDefault(char const *);          //001EA878
  void SetupSingleNIS(TStageOperation *, bool);      //001EA918
  void SetupSingleNIS(char const *, int, int, int);  //001EA9C0
  void AddCommentary(int);                           //001EAA28
  void PlayStage(bool, bool);                        //001EAA78
  void InitElement();                                //001EAB9C
  void InitStadium(int);                             //001EAD24
  void SetUserPlayer(int, bool, ECharModelType);     //001EAD44
  void Process();                                    //001EAF90
  int ProcessElementVideo();                        //001EB1AC
  void UpdateEnvRot(int);                            //001EB65C
  int  ProcessElement();                             //001EB7C0
  void RenderHUD();                                  //001EBE7C
  int GetPlayableIndex(int);                        //001EBEC0
  void IsFirstPlayableElement();                     //001EBEE0
  void BackToFirstPlayableElement();                 //001EBF14
  int NextElement(bool);                            //001EBFA8
  int CheckPostStageOp();                           //001EC078
  void EnergyUsedOnFail();                           //001EC0EC
  void HasUsedEnergy();                              //001EC0F6
  void UpdateObjectives();                           //001EC100
  int GetMinutes();                                  //001EC48C
  static bool IsInternational(ETournamentType);             //001EC53C
  void LogAttempt(bool);                             //001EC548

  //数据结构没有分析完
  TStoryStage tStoryStage_0;
  TEnvConfig field_4d04;  // 这里的偏移从4d04开始且大小为0xfdc(4060) 2020.1.30
  int m_i_5CE0;
  int filed_5ce4;
  int filed_5ce8;  //当前管卡
  int filed_5cec;
  CStageManager_5CF0 *m_p_5CF0;
  CStoryHUD *pStory_5cf4;  // int filed_5cf4;
  int filed_5cf8;
  int filed_5cfc;
  int filed_5d00;
  STMedal filed_5d04[3];
  int filed_5d1c;
  short filed_5d20;
  int filed_5d24;
  int filed_5d28;
  int filed_5d2c;
  int filed_5d30;
  int filed_5d34;
  int filed_5d38;
  int filed_5d3c;
  int filed_5d40;
  int filed_5d44;
  int filed_5d48;
  int filed_5d4c;
  int filed_5d50;
  int filed_5d54;
  int filed_5d58;
  int filed_5d5c;
  int filed_5d60;
  int filed_5d64;
  int filed_5d68;
  int filed_5d6c;
  int filed_5d70;
  int filed_5d74;
  int filed_5d78;
  int filed_5d7c;
  int filed_5d80;
  int filed_5d84;
  int filed_5d88;
  int filed_5d8c;
  int filed_5d90;
  int filed_5d94;
  int filed_5d98;
  int filed_5d9c;
  int filed_5da0[2];
  // int filed_5da8; //001E4336 5dab处应该是个char,所以这里修改了(SJ.2020.2.21)
  uint8 uc_5DA8;
  uint8 uc_5DA9;
  uint8 uc_5DAA;
  uint8 uc_5DAB;
  uint8 uc_5DAC;
  uint8 uc_5DAD;
  uint8 uc_5DAE;
  uint8 uc_5DAF;
  int field_5DB0[15];
  int field_5DEC;
  int field_5DF0;
  int field_5DF4;
  int field_5DF8;
  int field_5DFC;
  int field_5E00;
  int field_5E04;
  int field_5E08;
  int field_5E0C;
  int field_5E10;
  uint8 uc_5E14;
  uint8 uc_5E15;
  uint8 uc_5E16;
  uint8 uc_5E17;
};

#endif  //_CSTAGEMANAGER_H_