#include <fstream>
#include <iostream>
#include <string>

#define max_step 2000
#define fea_len 1377
#define pi_len 82

using namespace std;

int main(){
  // int out_games = 1;
  // int out_steps = 2;
  // int out_winner = 1;
  // unsigned char* out_feas = new unsigned char [fea_len*out_steps];
  // float* out_pis = new float [pi_len * out_steps];
  // for (int i = 0; i < out_steps; ++i){
  //   for (int j = 0; j < fea_len; ++j){
  //     out_feas[i*fea_len+j] = j % (i+2) == 0 ? 1 : 0;
  //   }
  // }
  // string fea_path = "fake.features";
  // ofstream output(fea_path, ios::out | ios::binary);
  // output.write((char*)&out_games, sizeof(int));
  // for (int game_id = 0; game_id < out_games; game_id ++){
  //   output.write((char*)&out_steps, sizeof(int));
  //   output.write((char*)&out_winner, sizeof(int));
  //   output.write((char*)out_feas, sizeof(unsigned char) * fea_len * out_steps);
  //   output.write((char*)out_pis, sizeof(float) * pi_len*out_steps);
  // }
  // output.close();
 
  int mv_games = -1;
  int mv_steps = -1;
  int mv_winner = -1;
  uint16_t* moves = new uint16_t [max_step];
  uint16_t* visit_counts = new uint16_t [(9*9+1)*max_step];

  string moves_path = "/share/selfplay_data_go9/agz9_selfplay_zhuque_25071.moves";
  // string moves_path = "/root/agz9_selfplay_zhuque_24773.moves";
  ifstream mv_input(moves_path, ios::in | ios::binary);
  mv_input.read((char*)&mv_games, sizeof(int));
  cout << "mv_games: " << mv_games << endl;
  for (int game_id = 0; game_id < mv_games; game_id ++){
    mv_input.read((char*)&mv_steps, sizeof(int));
    mv_input.read((char*)&mv_winner, sizeof(int));
    mv_input.read((char*)moves, sizeof(uint16_t) * mv_steps);
    mv_input.read((char*)visit_counts, sizeof(uint16_t) * (9*9+1)*mv_steps);
    cout << "game " << game_id << ", mv_steps: " << mv_steps << ", mv_winner: " << mv_winner << endl;
  }
  mv_input.close();
  cout << "================================== " << endl;

  int fea_games = -1;
  int fea_steps = -1;
  int fea_winner = -1;
  unsigned char* feas = new unsigned char[fea_len*max_step];
  float* pis = new float[pi_len*max_step];
  //string fea_path = "/share/data/go_9/train/using/agz9_selfplay_zhuque_23122.moves.features";
  string fea_path = "/share/data/go_9/train/using/agz9_selfplay_zhuque_24773.moves.features";
  ifstream fea_input(fea_path, ios::in | ios::binary);
  fea_input.read((char*)&fea_games, sizeof(int));
  cout << "fea_games: " << fea_games << endl;
  for (int game_id = 0; game_id < fea_games; game_id ++){
    fea_input.read((char*)&fea_steps, sizeof(int));
    fea_input.read((char*)&fea_winner, sizeof(int));
    if (fea_steps == 0){
      cout << "steps: " << fea_steps << endl;
    }
    fea_input.read((char*)feas, sizeof(unsigned char) * fea_len*fea_steps);
    fea_input.read((char*)pis, sizeof(float) * pi_len*fea_steps);
    cout << "game " << game_id << ", fea_steps: " << fea_steps << ", fea_winner: " << fea_winner << endl;
    // for (int step_id = 0; step_id < fea_steps; ++ step_id){
    //   cout << "  step: " << ", feas: " << int(feas[0]);
    //   for (int i = 1; i < fea_len && i < 10; ++i){
    //     cout << ", " << int(feas[step_id * fea_len + i]);
    //   }
    //   cout << endl;
    // }
  }
  fea_input.close();
  return 0;
}
