#include "../../lib/mice/config.h"
#include "../../lib/mice/ipc.h"
#include "callbacks.h"

void mdkPcpVersion(pcpa_t* stream, void* data) { pcpaSetSendPacket(stream, KC_VERSION, "0104"); }

void mdkPcpStatus(pcpa_t* stream, void* data) { pcpaSetSendPacket(stream, KC_STATUS, "available"); }

void mdkPcpAbGameId(pcpa_t* stream, void* data) {
    char gameId[5];
    if (MiceIpcGetGameId(gameId)) {
        gameId[4] = '\0';
        pcpaSetSendPacket(stream, AB_GAMEID, gameId);
    } else {
        pcpaSetSendPacket(stream, AB_GAMEID, "----");
    }
}
void mdkPcpAbSystemFlag(pcpa_t* stream, void* data) {
    // systemflag 24 = billing + allnet, which should suffice for everything
    pcpaSetSendPacket(stream, AB_SYSTEMFLAG, "24");
}
void mdkPcpAbModelType(pcpa_t* stream, void* data) {
    // ST
    pcpaSetSendPacket(stream, AB_MODELTYPE, "02");
}
void mdkPcpAbFormatType(pcpa_t* stream, void* data) {
    pcpaSetSendPacket(stream, AB_FORMATTYPE, "01");
}
void mdkPcpAbRegion(pcpa_t* stream, void* data) {
    // All regions
    pcpaSetSendPacket(stream, AB_REGION, "FF");
}
void mdkPcpAbPlatformId(pcpa_t* stream, void* data) {
    // TODO: We _can_ do better here
    pcpaSetSendPacket(stream, AB_PLATFORMID, "AAS");
}
void mdkPcpAbNetworkAddress(pcpa_t* stream, void* data) {
    pcpaSetSendPacket(stream, AB_NETWORKADDRESS, "192.168.103.0");
}
void mdkPcpAbDvd(pcpa_t* stream, void* data) { pcpaSetSendPacket(stream, AB_DVD, "01"); }

void mdkPcpPbKeyId(pcpa_t* stream, void* data) {
    pcpaSetSendPacket(stream, BIL_KEYID, MiceConfig.sysconf.keychip_serial);
}
void mdkPcpPbMainId(pcpa_t* stream, void* data) {
    pcpaSetSendPacket(stream, BIL_MAINID, MiceConfig.sysconf.pcb_serial);
}
void mdkPcpPbPlayCount(pcpa_t* stream, void* data) {
    pcpaSetSendPacket(stream, BIL_PLAYCOUNT, "00000000");
}
void mdkPcpPbPlayLimit(pcpa_t* stream, void* data) {
    // 8192 plays before check-in
    pcpaSetSendPacket(stream, BIL_PLAYLIMIT, "00002000");
}
void mdkPcpPbNearfull(pcpa_t* stream, void* data) {
    // Accounting mode = 0, nearfull = 512
    pcpaSetSendPacket(stream, BIL_NEARFULL, "00000200");
}

void mdkPcpTdRestore(pcpa_t* stream, void* data) {
    char* restart = pcpaGetCommand(stream, "restart");
    if (restart && strcmp(restart, "1") == 0)
        pcpaSetSendPacket(stream, TRA_RESTORE, "1");
    else
        pcpaSetSendPacket(stream, TRA_RESTORE, "2");
}

void mdkPcpTdPut(pcpa_t* stream, void* data) { pcpaSetSendPacket(stream, TRA_PUT, "6410"); }
void mdkPcpTdGet(pcpa_t* stream, void* data) {
    pcpaSetSendPacket(stream, TRA_GET, "0");
    pcpaAddSendPacket(stream, "address", "0");
}

// Real billing public key. Will not work on networks that have a custom key set
static BYTE BILLING_PUBKEY[] = {
    0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
    0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xbb,
    0xe9, 0xcb, 0xd9, 0x95, 0x35, 0xda, 0x64, 0x27, 0x02, 0x11, 0x42, 0x8b, 0x9c, 0x6e, 0x5a,
    0xf0, 0x5a, 0xbb, 0x01, 0x63, 0x18, 0x9c, 0xbf, 0xdd, 0xc7, 0x25, 0xcb, 0x22, 0x1f, 0xe9,
    0x98, 0x57, 0x9b, 0x8a, 0xf2, 0xf2, 0x37, 0xac, 0xd4, 0xd8, 0xed, 0x33, 0x0b, 0x46, 0x0e,
    0x7a, 0xdc, 0xe8, 0x5d, 0x90, 0x12, 0xee, 0x88, 0x76, 0x47, 0x6d, 0x19, 0x94, 0x83, 0x68,
    0x55, 0x76, 0x04, 0x17, 0xd6, 0x69, 0x21, 0xd5, 0xfa, 0xf0, 0x53, 0x99, 0xca, 0x38, 0xb5,
    0x4d, 0x9b, 0x97, 0x83, 0x8b, 0x9b, 0x8a, 0x2a, 0x28, 0x77, 0x98, 0x3b, 0xa8, 0x7a, 0xda,
    0xd4, 0x05, 0xcc, 0xfb, 0xc2, 0x14, 0xab, 0xf1, 0x80, 0x92, 0x51, 0x4b, 0x40, 0x96, 0x60,
    0x31, 0x77, 0xa9, 0x95, 0x70, 0xaa, 0xdd, 0xe8, 0xf0, 0x10, 0x8d, 0x26, 0x0d, 0x86, 0xe0,
    0x26, 0xd9, 0x27, 0x52, 0x86, 0xdf, 0x1f, 0x02, 0x03, 0x01, 0x00, 0x01,
};
static BOOL BP_READ = FALSE;
// Not the real CA cert. This is the fake cert that a lot of people use
static BYTE CA_CERT[] = {
    0x30, 0x82, 0x03, 0x2d, 0x30, 0x82, 0x02, 0x15, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x43,
    0xae, 0xdc, 0x17, 0xc6, 0xea, 0xde, 0x6f, 0x69, 0x71, 0x6a, 0xba, 0xcc, 0x97, 0x99, 0x45, 0xcf,
    0x0d, 0xeb, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
    0x05, 0x00, 0x30, 0x25, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x07, 0x44,
    0x75, 0x6d, 0x6d, 0x79, 0x43, 0x41, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
    0x08, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x50, 0x4b, 0x49, 0x30, 0x20, 0x17, 0x0d, 0x31, 0x38, 0x31,
    0x30, 0x31, 0x35, 0x31, 0x39, 0x33, 0x30, 0x35, 0x31, 0x5a, 0x18, 0x0f, 0x32, 0x31, 0x31, 0x38,
    0x31, 0x30, 0x31, 0x35, 0x31, 0x39, 0x33, 0x30, 0x35, 0x31, 0x5a, 0x30, 0x25, 0x31, 0x10, 0x30,
    0x0e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x07, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x43, 0x41, 0x31,
    0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x50,
    0x4b, 0x49, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
    0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82,
    0x01, 0x01, 0x00, 0xde, 0x1c, 0x42, 0x76, 0x2f, 0xe1, 0x57, 0xad, 0xc7, 0x56, 0x1b, 0xc2, 0xcd,
    0x67, 0xf5, 0xa4, 0x9d, 0x3c, 0xc4, 0xf6, 0x5b, 0x01, 0xe0, 0x8d, 0x46, 0x6f, 0xb2, 0xdc, 0xad,
    0x78, 0xbf, 0x62, 0xaf, 0xab, 0x12, 0x71, 0x7c, 0x8a, 0xbb, 0xb6, 0x87, 0x88, 0xf5, 0x89, 0xc9,
    0x83, 0x4a, 0xec, 0x0b, 0x05, 0x2f, 0xff, 0xa8, 0x43, 0x4e, 0xd8, 0xb7, 0xc4, 0xfa, 0x25, 0x35,
    0xce, 0x6b, 0x65, 0x86, 0x30, 0x1f, 0x1a, 0xff, 0x12, 0x61, 0x25, 0xe0, 0xd6, 0xcb, 0x00, 0x6c,
    0x1e, 0x92, 0xbf, 0x8b, 0xef, 0xcf, 0x45, 0x34, 0xa4, 0xd0, 0x80, 0xbe, 0x72, 0x7f, 0x2b, 0xc5,
    0x0d, 0xee, 0x40, 0x17, 0x1c, 0x1d, 0x6b, 0xc3, 0x97, 0x51, 0xbc, 0xee, 0x3b, 0x03, 0xe3, 0x35,
    0x34, 0xef, 0x12, 0xea, 0x96, 0x13, 0x2f, 0x98, 0xc9, 0x84, 0xaf, 0x81, 0x68, 0xd5, 0x66, 0x3f,
    0xac, 0x3d, 0x78, 0xe4, 0x38, 0x73, 0xed, 0x19, 0xbc, 0xa1, 0x4e, 0x11, 0x7d, 0x04, 0xfe, 0xec,
    0xb5, 0xa9, 0xc5, 0x2e, 0x4b, 0x7f, 0x3a, 0x65, 0xc3, 0xc5, 0x47, 0x14, 0x45, 0xbc, 0x4a, 0xd4,
    0xa3, 0xc5, 0xa3, 0xc8, 0xc0, 0x30, 0x57, 0xd1, 0xd9, 0xa1, 0x56, 0xb7, 0x87, 0x7b, 0x40, 0xcb,
    0x02, 0x9f, 0x0e, 0xb6, 0x23, 0x17, 0xc0, 0xed, 0x74, 0xd1, 0xbd, 0xdc, 0x6d, 0xe2, 0x83, 0x3a,
    0x6f, 0xe0, 0x8a, 0xe8, 0xe0, 0x0b, 0x84, 0x4a, 0xb3, 0xc4, 0xde, 0xde, 0x31, 0xe6, 0xe8, 0x8e,
    0xe1, 0x4c, 0x70, 0x50, 0xe6, 0xe8, 0x96, 0x45, 0x6c, 0x98, 0xe0, 0x33, 0xa3, 0xb8, 0x35, 0x42,
    0x21, 0x22, 0xd3, 0x09, 0x47, 0x4d, 0x05, 0xf5, 0x64, 0x7d, 0xdd, 0x5e, 0x06, 0x91, 0x00, 0x5e,
    0x6f, 0xbf, 0xf5, 0x74, 0xd9, 0x90, 0x27, 0x98, 0x8e, 0x64, 0x79, 0x77, 0xcf, 0x0c, 0x32, 0x2a,
    0x51, 0x17, 0xf7, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x1d, 0x06, 0x03,
    0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xc4, 0x77, 0x5b, 0x5d, 0x0e, 0xdd, 0xa8, 0x5a, 0xb0,
    0x8f, 0x3c, 0xe5, 0xd3, 0x62, 0xfe, 0x51, 0xe7, 0xc4, 0xc0, 0xd8, 0x30, 0x1f, 0x06, 0x03, 0x55,
    0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xc4, 0x77, 0x5b, 0x5d, 0x0e, 0xdd, 0xa8, 0x5a,
    0xb0, 0x8f, 0x3c, 0xe5, 0xd3, 0x62, 0xfe, 0x51, 0xe7, 0xc4, 0xc0, 0xd8, 0x30, 0x0f, 0x06, 0x03,
    0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0d, 0x06,
    0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01,
    0x00, 0xa5, 0xc5, 0x86, 0xb3, 0xe0, 0x34, 0xe8, 0x06, 0xaa, 0xaf, 0xd5, 0x40, 0xc6, 0x10, 0xe9,
    0x8b, 0x8b, 0xd2, 0x57, 0x42, 0xbe, 0xfb, 0xdd, 0x76, 0x20, 0x95, 0x91, 0xbf, 0xaa, 0xd1, 0x47,
    0x2e, 0xc7, 0xcf, 0x86, 0xfb, 0x3a, 0xfe, 0x54, 0x39, 0x9e, 0xc0, 0x1e, 0xc8, 0x67, 0x40, 0xd5,
    0x90, 0xbd, 0xdf, 0x50, 0xa7, 0xe7, 0xa1, 0x84, 0xda, 0x0b, 0x8c, 0xbe, 0x63, 0xa1, 0x69, 0x3d,
    0x06, 0x6c, 0x0c, 0x3c, 0x60, 0x0f, 0x84, 0x10, 0x2f, 0x89, 0xd0, 0xe1, 0xf1, 0x05, 0x0b, 0x50,
    0x02, 0x03, 0x25, 0x60, 0xc6, 0x17, 0xc0, 0xad, 0x0c, 0x36, 0x96, 0xf2, 0x81, 0xd8, 0x8e, 0xc0,
    0x53, 0x05, 0x25, 0x63, 0x2a, 0xed, 0x4c, 0x6b, 0xf3, 0x95, 0x31, 0xcf, 0x6f, 0x17, 0x41, 0x23,
    0x31, 0x46, 0xf7, 0x2e, 0xd9, 0x4a, 0x66, 0x43, 0x54, 0xaa, 0x65, 0x02, 0x8e, 0x51, 0x3f, 0x57,
    0x66, 0xd8, 0x62, 0x3a, 0x95, 0x2c, 0xd4, 0xe8, 0x82, 0xb5, 0xce, 0xe0, 0xac, 0x57, 0xca, 0xa7,
    0xc1, 0x15, 0xd5, 0x33, 0x4c, 0x20, 0x7f, 0xa9, 0x7e, 0xd5, 0xf8, 0x73, 0x67, 0xb3, 0x30, 0xea,
    0x20, 0x51, 0xc5, 0xfb, 0x0c, 0x93, 0x92, 0x53, 0x12, 0x3b, 0x04, 0xd4, 0x22, 0xbb, 0xd6, 0xe1,
    0x1e, 0xb3, 0xa2, 0x35, 0xcb, 0xd3, 0x08, 0xd1, 0x76, 0xfb, 0x41, 0xa9, 0x74, 0xaf, 0x3c, 0x5a,
    0xb9, 0x1d, 0x62, 0x4f, 0x76, 0x0d, 0x1d, 0x7d, 0x5c, 0xb2, 0x54, 0x36, 0x6e, 0xef, 0x23, 0xce,
    0xde, 0x03, 0x09, 0x2e, 0xa9, 0x03, 0x90, 0x7b, 0x1a, 0x60, 0xbb, 0x36, 0x20, 0xa1, 0xd4, 0x5e,
    0xa2, 0x42, 0x6c, 0x70, 0x89, 0xff, 0x08, 0x69, 0x31, 0x63, 0x58, 0x62, 0x8c, 0xc3, 0x14, 0x6a,
    0xe1, 0x7a, 0xd3, 0x52, 0xbe, 0x8d, 0xec, 0x2a, 0x1b, 0x7d, 0xc2, 0xf4, 0x0a, 0xd3, 0xc9, 0x9c,
    0xca,
};
static BOOL CA_READ = FALSE;

void mdkPcpPbSignaturePubKey(pcpa_t* stream, void* data) {
    if (!BP_READ) {
        BP_READ = TRUE;
        HANDLE hFile = CreateFileA(MiceIpcRelativePath("billing.pub"), GENERIC_READ,
                                   FILE_SHARE_READ, NULL, 0, OPEN_EXISTING, NULL);
        if (hFile != INVALID_HANDLE_VALUE) {
            DWORD nRead;
            ReadFile(hFile, BILLING_PUBKEY, sizeof BILLING_PUBKEY, &nRead, NULL);
            CloseHandle(hFile);
        }
    }

    pcpaSetBinaryMode(stream, binary_mode_send);
    pcpaSetSendBinaryBuffer(stream, BILLING_PUBKEY, sizeof BILLING_PUBKEY);
    pcpaSetSendPacket(stream, BIL_SIGNATURE, "0");
    pcpaAddSendPacket(stream, "port", "40107");
    char sSize[8];
    sprintf_s(sSize, _countof(sSize), "%d", sizeof BILLING_PUBKEY);
    pcpaAddSendPacket(stream, "size", sSize);
}
void mdkPcpPbCaCertification(pcpa_t* stream, void* data) {
    if (!CA_READ) {
        CA_READ = TRUE;
        HANDLE hFile = CreateFileA(MiceIpcRelativePath("ca.crt"), GENERIC_READ, FILE_SHARE_READ,
                                   NULL, 0, OPEN_EXISTING, NULL);
        if (hFile != INVALID_HANDLE_VALUE) {
            DWORD nRead;
            ReadFile(hFile, CA_CERT, sizeof CA_CERT, &nRead, NULL);
            CloseHandle(hFile);
        }
    }

    pcpaSetBinaryMode(stream, binary_mode_send);
    pcpaSetSendBinaryBuffer(stream, CA_CERT, sizeof CA_CERT);
    pcpaSetSendPacket(stream, BIL_CACERT, "0");
    pcpaAddSendPacket(stream, "port", "40107");
    char sSize[8];
    sprintf_s(sSize, _countof(sSize), "%d", sizeof CA_CERT);
    pcpaAddSendPacket(stream, "size", sSize);
}

void mdkPcpTdLogicalErase(pcpa_t* stream, void* data) {
    pcpaSetSendPacket(stream, TRA_LOGICALERASE, "0");
}
void mdkPcpTdSectorErase(pcpa_t* stream, void* data) {
    pcpaSetSendPacket(stream, TRA_LOGICALERASE, "0");
}
