import os
import sys

# VtdRoot = "/home/hancheng/VIRES/VTD.2022.1"
# ROD = VtdRoot + "/Runtime/Tools/ROD/ROD"
# LicenceAddress = " -s 27500@192.168.11.179"
# RodProject = " --project " + VtdRoot + "/Runtime/Tools/ROD/DefaultProject/DefaultProject.rpj"
# SourceOsgbPath = VtdRoot + "/Runtime/Tools/ROD/DefaultProject/Database"
#
#
# def generateOsgb(root, file):
#
#     # generate osgb file
#     xodrFilePath = root + "/" + file
#     generate = ROD + LicenceAddress + RodProject + " --xodr " + xodrFilePath + " -G"
#     os.system(generate)
#
#     # move osgb file
#     sourceOsgbFileName = file[:-4] + "opt.osgb"
#     sourceOsgbFilePath = SourceOsgbPath + "/" + sourceOsgbFileName
#     destOsgbFilePath = root + "/" + sourceOsgbFileName[:-8] + "osgb"
#     os.system("mv " + sourceOsgbFilePath + " " + destOsgbFilePath)
#
#     # remove odr file
#     tempXodrFilePath = VtdRoot + "/Runtime/Tools/ROD/DefaultProject/Odr/" + file
#     os.system("rm " + tempXodrFilePath)
#     print("Complete: " + root + "/" + file[:-4] + 'osgb')

def generate_osgb(root_path, file):
    # generate osgb file
    VtdRoot = '/home/hancheng/VIRES/VTD.2022.1'
    ROD = VtdRoot + "/Runtime/Tools/ROD/ROD "
    LicenceAddress = '-s 27500@192.168.11.179'
    RodProject = " --project " + VtdRoot + "/Runtime/Tools/ROD/DefaultProject/DefaultProject.rpj"
    SourceOsgbPath = VtdRoot + "/Runtime/Tools/ROD/DefaultProject/Database"
    xodrFilePath = file
    generate = ROD + LicenceAddress + RodProject + " --xodr " + xodrFilePath + " -G"
    os.system(generate)

    # move osgb file
    sourceOsgbFileName = file[:-4] + "opt.osgb"
    sourceOsgbFilePath = SourceOsgbPath + "/" + sourceOsgbFileName.split('/')[-1]
    destOsgbFilePath = sourceOsgbFileName.replace('opt.osgb', 'osgb')
    os.system("mv " + sourceOsgbFilePath + " " + destOsgbFilePath)

    # remove odr file
    tempXodrFilePath = VtdRoot + "/Runtime/Tools/ROD/DefaultProject/Odr/" + file.split('/')[-1]
    os.system("rm " + tempXodrFilePath)
    print("Complete: " + root_path + "/" + file[:-4] + 'osgb')

def formatTwo():
    """
    data format:
    simulation
        file.xosc
        file.xodr
        file.osgb
    :return:
    """
    for root, dirs, files in os.walk(inputDirectory):
        for file in files:
            if ".xodr" == file[-5:]:

                # skip if osgb file exists
                if os.path.isfile(root + "/" + file[:-4] + 'osgb'):
                    print("Complete: " + root + "/" + file[:-4] + 'osgb')
                    continue

                generateOsgb(root, file)


def formatOne():
    """
    data format:
        openx
            xosc
                file.xosc
            xodr
                file.xodr
            osgb
                file.osgb
    :return:
    """
    for root, dirs, files in os.walk(inputDirectory):
        for file in files:
            if "xodr" == file[-4:]:

                # make osgb folder
                if not os.path.isdir(root[:-4] + 'osgb'):
                    os.mkdir(root[:-4] + 'osgb')

                # skip if osgb file exists
                if os.listdir(root[:-4] + 'osgb'):
                    print("Complete: " + root[:-4] + 'osgb' + "/" + file[:-4] + 'osgb')
                    continue

                generateOsgb(root, file)
                # osgb position move
                os.system("mv " + root + "/" + file[:-4] + 'osgb ' + root[:-4] + 'osgb' + "/" + file[:-4] + 'osgb')


if __name__ == '__main__':

    # if 2 != len(sys.argv):
    #     print('\nEnter root path as the only command line argument')
    #     # exit(0)
    #
    # # inputDirectory = sys.argv[1]
    # inputDirectory = '/home/hancheng/hc_project/simulation'
    #
    # # TODO: remove all existing odr/osgb files in the ROD project
    #
    # # formatOne()
    # formatTwo()
    root_path ='/media/hancheng/Simulation4/1013/here_bizhang1_2023-10-13-14-31-41/simulation'
    xodr_path ='/media/hancheng/Simulation4/1013/here_bizhang1_2023-10-13-14-31-41/simulation'
    generate_osgb(root_path, xodr_path)