#include "COMM.h"
#include "XSYS.h"
#include "SNDGAME.h"
COMM_Info COMM_tInfo;
const char *X_sCommFile[1234];
static int dword_47DF94, dword_47DF94_1, dword_47DF90, dword_47DF94_2, dword_47DF8C;

//0x001d25ac   ^_^
int COMM_CheckCommentaryTime(ECommType eCommType0, int int1) {
  int *r3 = &COMM_tInfo.byte_16C[int1];
  if (int1 == -1)
    r3 = &COMM_tInfo.info[eCommType0 + 1];
  return *r3;
}

//0x001d26cc
uint COMM_FindFile(const char *cc0) {
  char var_11C[0x100];
  if (strstr(cc0, ".wav")) {
    strlcpy((unsigned char *)var_11C, (unsigned char *)cc0, 0x100);
  } else {
    //loc_1D26FC
    snprintf(var_11C, 0x100, "%s.wav", cc0);
  }

  for (int r4 = 0; r4 <= 0x7A3; r4++) {
    if (!strcmp(X_sCommFile[r4], var_11C))
      return r4;
  }

  return -1;
}

int tGame;
ushort X_iCommCnt[70];
static short **X_iCommXRef;
/*0x001d243c COMM_PlayCommentary 96*/
void COMM_PlayCommentary(ECommType eCommType0, int int1, int int2) {
  /*PUSH.W          {R4-R10,LR}
	.text:001D2440                 MOV             R5, R0
	.text:001D2442                 MOV             R8, R1
	.text:001D2444                 ADDS            R0, #1
	.text:001D2446                 BEQ             locret_1D2532*/
  int r2, r6, r7;
  COMM_Info_S0_14 *r3;
  if (eCommType0 == ECommType_Err) {
    return;
  }

  /*	.text:001D2448                 LDR             R0, =(tGame_ptr - 0x1D244E)
	.text:001D244A                 ADD             R0, PC  ; tGame_ptr
	.text:001D244C                 LDR.W           R9, [R0] ; tGame
	.text:001D2450                 LDR.W           R0, [R9]
	.text:001D2454                 CMP             R0, #0xF1
	.text:001D2456                 BLT             loc_1D2494*/
  if (tGame >= 0xf1) {
    /*.text:001D2458                 LDR             R1, =(COMM_tInfo_ptr - 0x1D245E)
		.text:001D245A                 ADD             R1, PC  ; COMM_tInfo_ptr
		.text:001D245C                 LDR             R1, [R1] ; COMM_tInfo
		.text:001D245E                 ADD.W           R3, R1, R5,LSL#2
		.text:001D2462                 LDR             R3, [R3,#4]
		.text:001D2464                 SUBS            R3, R0, R3
		.text:001D2466                 CMP             R3, #0xF0
		.text:001D2468                 BLT             locret_1D2532*/
    if (tGame - COMM_tInfo.info[eCommType0] < 0xF0)
      return;
    /*	.text:001D246A                 CMP             R2, #0
		.text:001D246C                 BLT             loc_1D2494
		.text:001D246E                 CMP.W           R0, #0x258
		.text:001D2472                 BLE             loc_1D2494*/
    if (int2 >= 0 && tGame > 0x258) {
      /*	.text:001D2474                 LDR             R3, =(X_iCommXRef_ptr - 0x1D247A)
			.text:001D2476                 ADD             R3, PC  ; X_iCommXRef_ptr
			.text:001D2478                 LDR             R3, [R3] ; X_iCommXRef
			.text:001D247A                 LDR.W           R3, [R3,R5,LSL#2]
			.text:001D247E                 LDRH.W          R3, [R3,R2,LSL#1]
			.text:001D2482                 ADD.W           R1, R1, R3,LSL#2
			.text:001D2486                 LDR.W           R1, [R1,#0x16C]
			.text:001D248A                 SUBS            R1, R0, R1
			.text:001D248C                 CMP.W           R1, #0x258
			.text:001D2490                 BGE             loc_1D24E4
			.text:001D2492                 B               locret_1D2532*/
      if (tGame - COMM_tInfo.byte_16C[(X_iCommXRef[eCommType0])[int1]] >= 0x258)
        return;
    }
  }

  /*.text:001D2494                 ADDS            R1, R2, #1
	.text:001D2496                 BNE             loc_1D24E4*/
  if (int2 == -1) {
    /*
		.text:001D2498                 LDR             R0, =(X_iCommCnt_ptr - 0x1D24A0)
		.text:001D249A                 MOVS            R1, #0xA
		.text:001D249C                 ADD             R0, PC  ; X_iCommCnt_ptr
		.text:001D249E                 LDR.W           R10, [R0] ; X_iCommCnt
		.text:001D24A2                 LDR             R0, =(X_iCommXRef_ptr - 0x1D24A8)
		.text:001D24A4                 ADD             R0, PC  ; X_iCommXRef_ptr
		.text:001D24A6                 LDR             R4, [R0] ; X_iCommXRef
		.text:001D24A8                 LDR             R0, =(COMM_tInfo_ptr - 0x1D24AE)
		.text:001D24AA                 ADD             R0, PC  ; COMM_tInfo_ptr
		text:001D24AC                 LDR             R6, [R0] ; COMM_tInfo*/

    r7 = 0xA;
    do {
      /*.text:001D24AE                 LDRH.W          R0, [R10,R5,LSL#1]
			.text:001D24B2                 MOV             R7, R1
			.text:001D24B4                 BLX             j__Z17XSYS_RandomNoSynci ; XSYS_RandomNoSync(int)
			.text:001D24B8                 MOV             R2, R0*/
      r2 = XSYS_RandomNoSync(X_iCommCnt[eCommType0]);
      /*.text:001D24BA                 LDR.W           R0, [R9]
			.text:001D24BE                 CBZ             R7, loc_1D24E0*/
      if (!r7) {
        break;
      }
      /*.text:001D24C0                 CMP.W           R0, #0x258
			.text:001D24C4                 BLE             loc_1D24E0*/
      if (tGame <= 0x258) {
        break;
      }
      r7--;

      /*
			.text:001D24C6                 LDR.W           R1, [R4,R5,LSL#2]
			.text:001D24CA                 LDRH.W          R1, [R1,R2,LSL#1]
			.text:001D24CE                 ADD.W           R1, R6, R1,LSL#2
			.text:001D24D2                 LDR.W           R1, [R1,#0x16C]
			.text:001D24D6                 SUBS            R3, R0, R1
			.text:001D24D8                 SUBS            R1, R7, #1
			.text:001D24DA                 CMP.W           R3, #0x258
			.text:001D24DE                 BLT             loc_1D24AE*/
    } while (tGame - COMM_tInfo.byte_16C[(X_iCommXRef[eCommType0])[r2]] < 0x258);
    ////loc_1D24E0
    /*text:001D24E0                 CMP             R7, #2
		.text:001D24E2                 BLT             locret_1D2532*/
    if (r7 < 2)
      return;
  }
  //loc_1D24E4
  /*.text:001D24E4                 LDR             R1, =(COMM_tInfo_ptr - 0x1D24EE)
	.text:001D24E6                 MOVS            R7, #0
	.text:001D24E8                 MOVS            R6, #0
	.text:001D24EA                 ADD             R1, PC  ; COMM_tInfo_ptr
	.text:001D24EC                 LDR             R1, [R1] ; COMM_tInfo
	.text:001D24EE                 ADD.W           R3, R1, #0x11C
	.text:001D24F2                 B               loc_1D2504*/
  r3 = COMM_tInfo.struct_11C;
  r7 = 0;
  r6 = 0;

  /*.text:001D2504                 CMP             R6, #3
	.text:001D2506                 BLS             loc_1D24F4*/
  while (r6 <= 3) {
    /*.text:001D24F4                 LDR             R4, [R3]
		.text:001D24F6                 CBZ             R4, loc_1D2500*/
    if (r3->dword_0) {
      /*.text:001D24F8                 LDR             R4, [R3,#8]
			.text:001D24FA                 CMP             R4, R8
			.text:001D24FC                 BGT             locret_1D2532*/
      if (r3->dword_8 > int1) {
        return;
      }
      /*.text:001D24FE                 STR             R7, [R3]*/
      r3->dword_0 = r7;
    }
    /*.text:001D2500                 ADDS            R3, #0x14
		  .text:001D2502                 ADDS            R6, #1*/
    r3++;
    r6++;
  }
  /*.text:001D2508                 LDR             R3, =(X_iCommXRef_ptr - 0x1D2512)
	.text:001D250A                 ADD.W           R7, R1, R5,LSL#2
	.text:001D250E                 ADD             R3, PC  ; X_iCommXRef_ptr
	.text:001D2510                 STR             R0, [R7,#4]*/
  COMM_tInfo.info[eCommType0 + 1] = tGame;
  /*.text:001D2512                 LDR             R3, [R3] ; X_iCommXRef
	.text:001D2514                 LDR.W           R3, [R3,R5,LSL#2]
	.text:001D2518                 LDRH.W          R3, [R3,R2,LSL#1]
	.text:001D251C                 ADD.W           R1, R1, R3,LSL#2
	.text:001D2520                 STR.W           R0, [R1,#0x16C]*/
  COMM_tInfo.byte_16C[(X_iCommXRef[eCommType0])[r2]] = tGame;

  /*.text:001D2524                 MOV             R1, R2
	.text:001D2526                 MOV             R0, R5
	.text:001D2528                 MOV             R2, R8
	.text:001D252A                 POP.W           {R4-R10,LR}
	.text:001D252E                 B.W             j_j__Z37SNDGAME_Commentary_QueueTypeXPriorityiii ; j_SNDGAME_Commentary_QueueTypeXPriority(int,int,int)*/

  SNDGAME_Commentary_QueueTypeXPriority(eCommType0, r2, int1);

  /*.text:001D2532                 POP.W           {R4-R10,PC}*/
}

/*0x001d2564 COMM_PlayCommentaryDelay 18*/
void COMM_PlayCommentaryDelay(int i0, ECommType eCommType1, int i2, int i3, bool b4) {
  COMM_Info_S0_14 *p = COMM_tInfo.struct_11C;
  for (int R4 = 0; R4 <= 3; R4++) {
    if (p->dword_0 == 0) {
      p->dword_0 = i0;
      p->dword_4 = eCommType1;
      p->dword_8 = i2;
      p->dword_C = i3;
      p->char_10 = b4;
      return;
    }
    p++;
  }
}

/*0x001d2558  ^_^*/
void COMM_PlayCommentaryID(int i0, int i1) {
  if (i0 != -1)
    SNDGAME_Commentary_QueueXPriority(i0, i1);
}

//	0x001d2430   ^_^
int COMM_PlayCommentaryKeeper(int, int) {
  return 0;
}

//.0x001d2598 COMM_PlayCommentaryKick 9
void COMM_PlayCommentaryKick(ECommType eCommType0, int i1, int i2) {
  COMM_PlayCommentaryDelay(10, eCommType0, i1, i2, 1);
}

//	0x001d242c COMM_PlayCommentaryName 2   ^-^
int COMM_PlayCommentaryName(int, int, int) {
  return 0;
}

char *dword_3A27BC;
//	0x001d2658 COMM_Process 28
void COMM_Process() {
  /*.text:001D2658                 PUSH            {R3-R7,LR}
.text:001D265A                 LDR             R0, =(COMM_tInfo_ptr - 0x1D2666)
.text:001D265C                 MOVS            R4, #0
.text:001D265E                 MOVW            R7, #(dword_3A27BC - 0x39BB88)
.text:001D2662                 ADD             R0, PC  ; COMM_tInfo_ptr
.text:001D2664                 LDR             R5, [R0] ; COMM_tInfo
.text:001D2666                 LDR             R0, =(tGame_ptr - 0x1D266C)
.text:001D2668                 ADD             R0, PC  ; tGame_ptr
.text:001D266A                 LDR             R6, [R0] ; tGame
.text:001D266C                 B               loc_1D2696*/
  /*.text:001D2696                 CMP             R4, #0x50 ; 'P'
.text:001D2698                 BNE             loc_1D266E*/
  for (int i = 0; i < 4; i++) {
    /*.text:001D266E                 ADDS            R2, R5, R4
		.text:001D2670                 LDR.W           R0, [R2,#0x11C]
		.text:001D2674                 CBZ             R0, loc_1D2694*/
    int r0 = COMM_tInfo.struct_11C[i].dword_0;
    if (r0) {
      /*.text:001D2676                 SUBS            R0, #1
			.text:001D2678                 STR.W           R0, [R2,#0x11C]
			.text:001D267C                 BNE             loc_1D2694*/
      r0--;
      COMM_tInfo.struct_11C[i].dword_0 = r0;
      if (r0) {
        /*.text:001D267E                 LDRB.W          R0, [R2,#0x12C]
				.text:001D2682                 CBZ             R0, loc_1D268A*/

        if (COMM_tInfo.struct_11C[i].char_10
            /*	.text:001D2684                 LDR             R0, [R6,R7]
					.text:001D2686                 CMP             R0, #9
					.text:001D2688                 BLT             loc_1D2694*/
            || dword_3A27BC[tGame] >= 9) {
          /*text:001D268A                 ADD.W           R2, R2, #0x120
					.text:001D268E                 LDM             R2, {R0-R2}
					.text:001D2690                 BLX             j__Z19COMM_PlayCommentary9ECommTypeii ; COMM_PlayCommentary(ECommType,int,int)*/
          COMM_PlayCommentary((ECommType)COMM_tInfo.struct_11C[i].dword_4, COMM_tInfo.struct_11C[i].dword_8, COMM_tInfo.struct_11C[i].dword_C);
        }
      }
    }
    /*.text:001D2694                 ADDS            R4, #0x14*/
    i++;
  }

  /*.text:001D269A                 POP             {R3-R7,PC}*/
}

//0x001d2608 COMM_Reset 15
void COMM_Reset() {
  /* PUSH            {R4,LR}
	.text:001D260A                 LDR             R0, =(COMM_tInfo_ptr - 0x1D2614)
	.text:001D260C                 MOVW            R1, #0x217C
	.text:001D2610                 ADD             R0, PC  ; COMM_tInfo_ptr
	.text:001D2612                 LDR             R4, [R0] ; COMM_tInfo
	.text:001D2614                 MOV             R0, R4
	.text:001D2616                 BLX             __aeabi_memclr4
	.text:001D261A                 MOVW            R0, #(dword_47DF94 - 0x47BE20)
	.text:001D261E                 MOV.W           R1, #0xFFFFFFFF
	.text:001D2622                 STR             R1, [R4,R0]
	.text:001D2624                 MOVW            R0, #(dword_47DF90 - 0x47BE20)
	.text:001D2628                 STR             R1, [R4,R0]
	.text:001D262A                 MOVW            R0, #(dword_47DF8C - 0x47BE20)
	.text:001D262E                 STR             R1, [R4,R0]
	.text:001D2630                 POP             {R4,PC}*/

  memset(&COMM_tInfo, 0, sizeof(COMM_Info));
  dword_47DF94 = -1;
  dword_47DF90 = -1;
  dword_47DF8C = -1;
}

//0x001d2638 COMM_ResetAfterRewind 11     ^_^
void COMM_ResetAfterRewind() {
  /*.text:001D2638                 LDR             R1, =(COMM_tInfo_ptr - 0x1D2642)
		.text:001D263A                 MOVS            R0, #1
		.text:001D263C                 MOVS            R2, #0
		.text:001D263E                 ADD             R1, PC  ; COMM_tInfo_ptr
		.text:001D2640                 LDR             R1, [R1] ; COMM_tInfo
		.text:001D2642
		.text:001D2642 loc_1D2642                              ; CODE XREF: COMM_ResetAfterRewind(void)+16��j
		.text:001D2642                 CMP             R0, #0x47 ; 'G'
		.text:001D2644                 IT EQ
		.text:001D2646                 BXEQ            LR
		.text:001D2648                 STR.W           R2, [R1,R0,LSL#2]
		.text:001D264C                 ADDS            R0, #1
		.text:001D264E                 B               loc_1D2642*/
  for (int r0 = 1; r0 != 0x47; r0++) {
    COMM_tInfo.info[r0] = 0;
  }
}

//	0x001d2438 COMM_StopCommentary 1     ^_^
void COMM_StopCommentary() {
  SNDGAME_Commentary_Stop();
}
