import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * author:gongyu
 * description:only one
 */
public class DataConversion {
    public static void main(String[] args) {
        String[] destinationFilePath = {
                "E:/guandan/optional_actions_new/src/csvNewData/主动发牌.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/单张.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/对子.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/三张.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/三带二.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/顺子.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/三连对.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/钢板.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/四炸.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/五炸.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/同花.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/六炸.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/七炸.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/八炸.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/九炸.csv",
                "E:/guandan/optional_actions_new/src/csvNewData/十炸.csv",
        };
        String[] sourceFilePath = {
                "E:/guandan/optional_actions_new/src/csvOldData/主动发牌.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/单张.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/对子.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/三张.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/三带二.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/顺子.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/三连对.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/钢板.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/四炸.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/五炸.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/同花.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/六炸.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/七炸.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/八炸.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/九炸.csv",
                "E:/guandan/optional_actions_new/src/csvOldData/十炸.csv",
        };

        for (int i=0;i<destinationFilePath.length;i++) {
            try {
                // 创建CSVReader对象并传入文件路径
                BufferedReader reader = new BufferedReader(new FileReader(sourceFilePath[i]));
                FileWriter writer = new FileWriter(destinationFilePath[i]);
                System.out.println(sourceFilePath[i]);

                // 读取文件中的每一行数据
                String line;
                int j=1;
                while ((line = reader.readLine()) != null) {
                    String[] parts = line.split(",");
//                    System.out.println(j);
                    j++;
                    writer.append(String.valueOf((Integer.parseInt(parts[0]) + 1) % 13)).append(",");

                    long cards1 = Long.parseLong(parts[1]);
                    long cards2 = Long.parseLong(parts[2]);
                    long temp = cards1;
                    cards1 |= cards2;
                    cards2 &= temp;
                    writer.append(String.valueOf(transformNumber(findSetBitsIndexes(cards1)))).append(",");
                    writer.append(String.valueOf(transformNumber(findSetBitsIndexes(cards2)))).append(",");
                    int rankName = Integer.parseInt(parts[3]);
                    // 打印每一行的数据

                    switch (rankName) {
                        case 0 -> {
                            writer.append("0,0,");
                        }
                        case 1 -> {
                            writer.append("1,0,");
                        }
                        case 2 -> {
                            writer.append("1,1,");
                        }
                        case 3 -> {
                            writer.append("1,2,");
                        }
                        case 4 -> {
                            writer.append("1,3,");
                        }
                        case 5 -> {
                            writer.append("1,4,");
                        }
                        case 6 -> {
                            writer.append("1,6,");
                        }
                        case 7 -> {
                            writer.append("1,5,");
                        }
                        case 8 -> {
                            writer.append("2,0,");
                        }
                        case 9 -> {
                            writer.append("3,0,");
                        }
                        case 10 -> {
                            writer.append("4,0,");
                        }
                        case 11 -> {
                            writer.append("5,0,");
                        }
                        case 12 -> {
                            writer.append("6,0,");
                        }
                        case 13 -> {
                            writer.append("7,0,");
                        }
                        case 14 -> {
                            writer.append("8,0,");
                        }
                        case 15 -> {
                            writer.append("9,0,");
                        }
                    }
                    writer.append(parts[4]).append("\n");

                }
                writer.flush();
                writer.close();

                // 关闭CSVReader
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


//        for(int i=0;i< sourceFilePath.length;i++){
//            System.out.println(sourceFilePath[i]);
//            try (FileInputStream sourceFis = new FileInputStream(new File(sourceFilePath[i]))) {
//                XSSFWorkbook sourceWorkbook = new XSSFWorkbook(sourceFis);
//                SXSSFWorkbook destinationWorkbook = new SXSSFWorkbook();
//                XSSFSheet sourceSheet = sourceWorkbook.getSheetAt(0);
//                String sheetName = sourceSheet.getSheetName();
//                Sheet destinationSheet = destinationWorkbook.createSheet(sheetName);
//                Row newRow = destinationSheet.createRow(destinationSheet.getLastRowNum() + 1);
//                Cell newCell0 = newRow.createCell(0);
//                Cell newCell1 = newRow.createCell(1);
//                Cell newCell2 = newRow.createCell(2);
//                Cell newCell3 = newRow.createCell(3);
//                Cell newCell4 = newRow.createCell(4);
//                Cell newCell5 = newRow.createCell(5);
//                newCell0.setCellValue("XIndex");
//                newCell1.setCellValue("Cards1");
//                newCell2.setCellValue("Cards2");
//                newCell3.setCellValue("RankIndex");
//                newCell4.setCellValue("TypeIndex");
//                newCell5.setCellValue("RankTypeIndex");
//
//                copySheet(sourceSheet, destinationSheet);
//                FileOutputStream fos = new FileOutputStream(new File(destinationFilePath[i]));
//                destinationWorkbook.write(fos);
//                fos.close();
//            } catch (IOException ex) {
//                ex.printStackTrace();
//            }
//        }

    }

    public static ArrayList<Integer> findSetBitsIndexes(long num) {
        ArrayList<Integer> indexes = new ArrayList<>();
        int index = 0;
        while (num != 0) {
            if ((num & 1) == 1) {  // 检查最低位是否为1
                indexes.add(index);
            }
            num >>= 1;  // 右移一位，检查下一位
            index++;
        }
        return indexes;
    }

    public static long transformNumber(ArrayList<Integer> indexes) {
        long newNum = 0L;
        for (int i : indexes) {
            if (i <= 48 && i % 4 == 0) {
                newNum |= (1L << (39 + i / 4));
            }else if(i <= 49 && i % 4 == 1){
                newNum |= (1L << (i / 4));
            }else if(i <= 50 && i % 4 == 2){
                newNum |= (1L << (13 + i / 4));
            }
            else if(i <= 51 && i % 4 == 3){
                newNum |= (1L << (26 + i / 4));
            }
            else if(i == 52){
                newNum |= (1L << 53);
            }
            else if(i == 53){
                newNum |= (1L << 52);
            }
        }
        return newNum;
    }


    // 复制工作表数据的辅助方法
//    private static void copySheet(XSSFSheet sourceSheet, Sheet destinationSheet) {
////        for (int rowNum = 1; rowNum <= sourceSheet.getLastRowNum(); rowNum++) { // 跳过第一行标题
//        for (int rowNum = 1; rowNum <= sourceSheet.getLastRowNum(); rowNum++) { // 跳过第一行标题
////            System.out.println(rowNum);
//            XSSFRow sourceRow = sourceSheet.getRow(rowNum);
//            Row newRow = destinationSheet.createRow(destinationSheet.getLastRowNum() + 1); // 新行从目标工作表的最后一行后面开始
//            if (sourceRow != null) {
//                XSSFCell sourceCell0 = sourceRow.getCell(0);
//                XSSFCell sourceCell1 = sourceRow.getCell(1);
//                XSSFCell sourceCell2 = sourceRow.getCell(2);
//                XSSFCell sourceCell3 = sourceRow.getCell(3);
//                XSSFCell sourceCell4 = sourceRow.getCell(4);
//                Cell newCell0 = newRow.createCell(0);
//                Cell newCell1 = newRow.createCell(1);
//                Cell newCell2 = newRow.createCell(2);
//                Cell newCell3 = newRow.createCell(3);
//                Cell newCell4 = newRow.createCell(4);
//                Cell newCell5 = newRow.createCell(5);
//
//                newCell0.setCellValue(((int)sourceCell0.getNumericCellValue() + 1) % 13);
//                newCell5.setCellValue((int)sourceCell4.getNumericCellValue());
//                newCell1.setCellValue(transformNumber(findSetBitsIndexes((long)sourceCell1.getNumericCellValue())));
//                newCell2.setCellValue(transformNumber(findSetBitsIndexes((long)sourceCell2.getNumericCellValue())));
//                int rankName = (int)sourceCell3.getNumericCellValue();
//                switch (rankName) {
//                    case 0 -> {
//                        newCell3.setCellValue(0);
//                        newCell4.setCellValue(0);
//                    }
//                    case 1 -> {
//                        newCell3.setCellValue(1);
//                        newCell4.setCellValue(0);
//                    }
//                    case 2 -> {
//                        newCell3.setCellValue(1);
//                        newCell4.setCellValue(1);
//                    }
//                    case 3 -> {
//                        newCell3.setCellValue(1);
//                        newCell4.setCellValue(2);
//                    }
//                    case 4 -> {
//                        newCell3.setCellValue(1);
//                        newCell4.setCellValue(3);
//                    }
//                    case 5 -> {
//                        newCell3.setCellValue(1);
//                        newCell4.setCellValue(4);
//                    }
//                    case 6 -> {
//                        newCell3.setCellValue(1);
//                        newCell4.setCellValue(6);
//                    }
//                    case 7 -> {
//                        newCell3.setCellValue(1);
//                        newCell4.setCellValue(5);
//                    }
//                    case 8 -> {
//                        newCell3.setCellValue(2);
//                        newCell4.setCellValue(0);
//                    }
//                    case 9 -> {
//                        newCell3.setCellValue(3);
//                        newCell4.setCellValue(0);
//                    }
//                    case 10 -> {
//                        newCell3.setCellValue(4);
//                        newCell4.setCellValue(0);
//                    }
//                    case 11 -> {
//                        newCell3.setCellValue(5);
//                        newCell4.setCellValue(0);
//                    }
//                    case 12 -> {
//                        newCell3.setCellValue(6);
//                        newCell4.setCellValue(0);
//                    }
//                    case 13 -> {
//                        newCell3.setCellValue(7);
//                        newCell4.setCellValue(0);
//                    }
//                    case 14 -> {
//                        newCell3.setCellValue(8);
//                        newCell4.setCellValue(0);
//                    }
//                    case 15 -> {
//                        newCell3.setCellValue(9);
//                        newCell4.setCellValue(0);
//                    }
//                }
//            }
//        }
//    }
}
