import csv
import json
import os
import sys

pose = []
boneIndex_rotation = {
"c_eye_target.x":0,
"c_eye_target.l":1,
"c_eye_target.r":2,
"root.x": 3,
"spine_01.x": 4,
"spine_02.x": 5,
"neck.x": 6,
"head.x": 7,
"c_skull_01.x":8,
"c_jawbone.x":9,
"c_chin_01.x":10,
"c_chin_02.x":11,
"c_lips_bot.l":12,
"c_lips_bot.r":13,
"c_lips_bot.x":14,
"c_lips_bot_01.l":15,
"c_lips_bot_01.r":16,
"c_teeth_bot.l":17,
"c_teeth_bot.r":18,
"c_teeth_bot.x":19,
"tong_01.x":20,
"tong_02.x":21,
"tong_03.x":22,
"c_lips_smile.l":23,
"c_lips_smile.r":24,
"c_lips_top.l":25,
"c_lips_top.r":26,
"c_lips_top.x":27,
"c_lips_top_01.l":28,
"c_lips_top_01.r":29,
"c_teeth_top.l":30,
"c_teeth_top.r":31,
"c_teeth_top.x":32,
"c_skull_02.x":33,
"c_cheek_inflate.l":34,
"c_cheek_inflate.r":35,
"c_cheek_smile.l":36,
"c_cheek_smile.r":37,
"c_ear_01.l":38,
"c_ear_02.l":39,
"c_ear_01.r":40,
"c_ear_02.r":41,
"c_eye_offset.l":42,
"c_eye.l":43,
"c_eye_ref_track.l":44,
"c_eyelid_bot.l":45,
"c_eyelid_bot_01.l":46,
"c_eyelid_bot_02.l":47,
"c_eyelid_bot_03.l":48,
"c_eyelid_corner_01.l":49,
"c_eyelid_corner_02.l":50,
"c_eyelid_top.l":51,
"c_eyelid_top_01.l":52,
"c_eyelid_top_02.l":53,
"c_eyelid_top_03.l":54,
"c_eye_offset.r":55,
"c_eye.r":56,
"c_eye_ref_track.r":57,
"c_eyelid_bot.r":58,
"c_eyelid_bot_01.r":59,
"c_eyelid_bot_02.r":60,
"c_eyelid_bot_03.r":61,
"c_eyelid_corner_01.r":62,
"c_eyelid_corner_02.r":63,
"c_eyelid_top.r":64,
"c_eyelid_top_01.r":65,
"c_eyelid_top_02.r":66,
"c_eyelid_top_03.r":67,
"c_eyebrow_full.l":68,
"c_eyebrow_01.l":69,
"c_eyebrow_01_end.l":70,
"c_eyebrow_02.l":71,
"c_eyebrow_03.l":72,
"c_eyebrow_full.r":73,
"c_eyebrow_01.r":74,
"c_eyebrow_01_end.r":75,
"c_eyebrow_02.r":76,
"c_eyebrow_03.r":77,
"c_nose_01.x":78,
"c_nose_02.x":79,
"c_nose_03.x":80,
"c_skull_03.x":81,
"shoulder.l": 82,
"arm_stretch.l": 83,
"forearm_stretch.l": 84,
"hand.l": 85,
"index1_base.l": 86,
"index1.l": 87,
"index2.l": 88,
"index3.l": 89,
"middle1_base.l": 90,
"middle1.l": 91,
"middle2.l": 92,
"middle3.l": 93,
"pinky1_base.l": 94,
"pinky1.l": 95,
"pinky2.l": 96,
"pinky3.l": 97,
"ring1_base.l": 98,
"ring1.l": 99,
"ring2.l": 100,
"ring3.l": 101,
"thumb1._base": 102,
"thumb1.l": 103,
"thumb2.l": 104,
"thumb3.l": 105,
"shoulder.r": 106,
"arm_stretch.r": 107,
"forearm_stretch.r": 108,
"hand.r": 109,
"index1_base.r": 110,
"index1.r": 111,
"index2.r": 112,
"index3.r": 113,
"middle1_base.r": 114,
"middle1.r": 115,
"middle2.r": 116,
"middle3.r": 117,
"pinky1_base.r": 118,
"pinky1.r": 119,
"pinky2.r": 120,
"pinky3.r": 121,
"ring1_base.r": 122,
"ring1.r": 123,
"ring2.r": 124,
"ring3.r": 125,
"thumb1_base.r": 126,
"thumb1.r": 127,
"thumb2.r": 128,
"thumb3.r": 129,
"thigh_stretch.l": 130,
"leg_stretch.l": 131,
"foot.l": 132,
"toes_01.l": 133,
"thigh_stretch.r": 134,
"leg_stretch.r": 135,
"foot.r": 136,
"toes_01.r": 137
}

boneIndex_location = {
"c_skull_01.x":138,
"c_jawbone.x":139,
"c_chin_01.x":140,
"c_chin_02.x":141,
"c_lips_bot.l":142,
"c_lips_bot.r":143,
"c_lips_bot.x":144,
"c_lips_bot_01.l":145,
"c_lips_bot_01.r":146,
"c_teeth_bot.l":147,
"c_teeth_bot.r":148,
"c_teeth_bot.x":149,
"tong_01.x":150,
"tong_02.x":151,
"tong_03.x":152,
"c_lips_smile.l":153,
"c_lips_smile.r":154,
"c_lips_top.l":155,
"c_lips_top.r":156,
"c_lips_top.x":157,
"c_lips_top_01.l":158,
"c_lips_top_01.r":159,
"c_teeth_top.l":160,
"c_teeth_top.r":161,
"c_teeth_top.x":162,
"c_skull_02.x":163,
"c_cheek_inflate.l":164,
"c_cheek_inflate.r":165,
"c_cheek_smile.l":166,
"c_cheek_smile.r":167,
"c_ear_01.l":168,
"c_ear_02.l":169,
"c_ear_01.r":170,
"c_ear_02.r":171,
"c_eye_offset.l":172,
"c_eye.l":173,
"c_eye_ref_track.l":174,
"c_eyelid_bot.l":175,
"c_eyelid_bot_01.l":176,
"c_eyelid_bot_02.l":177,
"c_eyelid_bot_03.l":178,
"c_eyelid_corner_01.l":179,
"c_eyelid_corner_02.l":180,
"c_eyelid_top.l":181,
"c_eyelid_top_01.l":182,
"c_eyelid_top_02.l":183,
"c_eyelid_top_03.l":184,
"c_eye_offset.r":185,
"c_eye.r":186,
"c_eye_ref_track.r":187,
"c_eyelid_bot.r":188,
"c_eyelid_bot_01.r":189,
"c_eyelid_bot_02.r":190,
"c_eyelid_bot_03.r":191,
"c_eyelid_corner_01.r":192,
"c_eyelid_corner_02.r":193,
"c_eyelid_top.r":194,
"c_eyelid_top_01.r":195,
"c_eyelid_top_02.r":196,
"c_eyelid_top_03.r":197,
"c_eyebrow_full.l":198,
"c_eyebrow_01.l":199,
"c_eyebrow_01_end.l":200,
"c_eyebrow_02.l":201,
"c_eyebrow_03.l":202,
"c_eyebrow_full.r":203,
"c_eyebrow_01.r":204,
"c_eyebrow_01_end.r":205,
"c_eyebrow_02.r":206,
"c_eyebrow_03.r":207,
"c_nose_01.x":208,
"c_nose_02.x":209,
"c_nose_03.x":210,
"c_skull_03.x":211,
}

path = sys.argv[1]

for gltf in os.listdir(path):
    bone_parameter = {}
    bone_keypoint = {}
    with open(f'{path}{gltf}', 'r') as file:
        data = json.load(file)
        
        for node in data['nodes']:
            if "rotation" in node and "translation" in node:
                bone_parameter[node["name"]] = {"rotation" : node["rotation"], "translation" : node["translation"]}
    for bone in bone_parameter:
        for index in boneIndex_rotation:
            if bone == index:
                bone_keypoint[boneIndex_rotation[index]] = f"{bone_parameter[bone]['rotation'][0]} {bone_parameter[bone]['rotation'][1]} {bone_parameter[bone]['rotation'][2]} {bone_parameter[bone]['rotation'][3]}"

        for index in boneIndex_location:
            if bone == index:
                bone_keypoint[boneIndex_location[index]] = f"{bone_parameter[bone]['translation'][0]} {bone_parameter[bone]['translation'][1]} {bone_parameter[bone]['translation'][2]} 0"
    name = gltf.split(".")[0]
    temp = []
    with open(f'{sys.argv[2]}{name}.csv', 'w') as file:
        for i in range(0,212):
            if i in bone_keypoint.keys():
                file.write(f"{bone_keypoint[i]}\n")
            else:
                file.write("0 0 0 0\n")
        file.close()         

    
    