# -*- coding: utf-8 -*-
import os
import time
import oss2
import xml.etree.ElementTree as ET
from rosbag import Bag, Compression
import shutil
import docker
import bagtocsv_orig
import bagtocsv_hmi
import get_drive
import parse_jinlong_image
import pcdtovideo_jilong_overlook
import pcdtovideo_jilong_forwardlook

key1 = 'kinglong/'
path1 = '/scenarios/'
sleep_time = 5  # 每多少秒扫描一次


def move_xosc_before_simulation(root_path1, root_path2):
    try:

        xodr1 = 'thq.xodr'
        xodr2 = 'OpenDRIVE.xodr'
        xodr3 = 'gelan.xodr'
        osgb1 = 'thq.opt.osgb'
        osgb2 = 'GJBM_20km_0822.opt.osgb'
        osgb3 = 'gelan.opt.osgb'

        tree1 = ET.parse(root_path1 + 'scenario_orig.xosc')
        root1 = tree1.getroot()
        for node0 in root1:
            if node0.tag == 'RoadNetwork':
                for node1 in node0:
                    # 打印根元素的标签名
                    src_file_prefix = '/root/rosmerge/resource/'
                    if 'LogicFile' == node1.tag:
                        if xodr1 in node1.get('filepath'):
                            dst_file = root_path1 + xodr1
                            shutil.copy(src_file_prefix + xodr1, dst_file)
                            node1.set('filepath', dst_file)
                        if xodr2 in node1.get('filepath'):
                            dst_file = root_path1 + xodr2
                            shutil.copy(src_file_prefix + xodr2, dst_file)
                            node1.set('filepath', dst_file)
                        if xodr3 in node1.get('filepath'):
                            dst_file = root_path1 + xodr3
                            shutil.copy(src_file_prefix + xodr3, dst_file)
                            node1.set('filepath', dst_file)
                    if 'SceneGraphFile' == node1.tag:
                        if osgb1 in node1.get('filepath'):
                            dst_file = root_path1 + osgb1
                            shutil.copy(src_file_prefix + osgb1, dst_file)
                            node1.set('filepath', dst_file)
                        if osgb2 in node1.get('filepath'):
                            dst_file = root_path1 + osgb2
                            shutil.copy(src_file_prefix + osgb2, dst_file)
                            node1.set('filepath', dst_file)
                        if osgb3 in node1.get('filepath'):
                            dst_file = root_path1 + osgb3
                            shutil.copy(src_file_prefix + osgb3, dst_file)
                            node1.set('filepath', dst_file)
            if node0.tag == 'Entities':
                for node1 in node0:
                    if node1.get("name") == 'Ego':
                        for node2 in node1:
                            if node2.tag == 'Vehicle':
                                node2.set('name', 'KingLong_snow white')
        tree1.write(root_path1 + 'openx0.xosc')

        tree2 = ET.parse(root_path2 + 'scenario_hmi.xosc')
        root2 = tree2.getroot()
        for node0 in root2:
            if node0.tag == 'RoadNetwork':
                for node1 in node0:
                    # 打印根元素的标签名
                    src_file_prefix = '/root/rosmerge/resource/'
                    if 'LogicFile' == node1.tag:
                        if xodr1 in node1.get('filepath'):
                            dst_file = root_path2 + xodr1
                            shutil.copy(src_file_prefix + xodr1, dst_file)
                            node1.set('filepath', dst_file)
                        if xodr2 in node1.get('filepath'):
                            dst_file = root_path2 + xodr2
                            shutil.copy(src_file_prefix + xodr2, dst_file)
                            node1.set('filepath', dst_file)
                        if xodr3 in node1.get('filepath'):
                            dst_file = root_path2 + xodr3
                            shutil.copy(src_file_prefix + xodr3, dst_file)
                            node1.set('filepath', dst_file)
                    if 'SceneGraphFile' == node1.tag:
                        if osgb1 in node1.get('filepath'):
                            dst_file = root_path2 + osgb1
                            shutil.copy(src_file_prefix + osgb1, dst_file)
                            node1.set('filepath', dst_file)
                        if osgb2 in node1.get('filepath'):
                            dst_file = root_path2 + osgb2
                            shutil.copy(src_file_prefix + osgb2, dst_file)
                            node1.set('filepath', dst_file)
                        if osgb3 in node1.get('filepath'):
                            dst_file = root_path2 + osgb3
                            shutil.copy(src_file_prefix + osgb3, dst_file)
                            node1.set('filepath', dst_file)
            if node0.tag == 'Entities':
                for node1 in node0:
                    if node1.get("name") == 'Ego':
                        for node2 in node1:
                            if node2.tag == 'Vehicle':
                                node2.set('name', 'KingLong_snow white')
        tree2.write(root_path2 + 'openx0.xosc')

    except Exception as e:
        print "修改xosc报错: %s" % str(e)
        # print(f'修改xosc报错：{e}')


def delete_files_in_dir(dir_path):
    # 遍历目录下的所有文件和子目录
    for root, dirs, files in os.walk(dir_path):
        for file in files:
            file_path = os.path.join(root, file)
            try:
                os.remove(file_path)
            except OSError as e:
                print("删除文件时出错：", file_path, "-", e.strerror)
            # 扫描目录上传mp4


def upload_simulation(parse_prefix, mp41, mp42):
    try:
        bucket.put_object_from_file(parse_prefix + 'scenario_orig.mp4', mp41)
        print '上传仿真视频到 %s' % parse_prefix + 'scenario_orig.mp4'
        bucket.put_object_from_file(parse_prefix + 'scenario_hmi.mp4', mp42)
        print '上传仿真视频到 %s' % parse_prefix + 'scenario_hmi.mp4'
        shutil.rmtree(path1)
    except Exception as e:
        print "上传视频报错: %s" % str(e)
        # print(f'上传视频报错{e}')


def simulation(parse_prefix, mp41, mp42):
    try:
        os.system("docker start vtd")
        # 实例化Docker客户端
        client = docker.from_env()
        while True:
            time.sleep(5)
            # 获取容器列表
            containers = client.containers.list()
            run = False
            # 打印容器列表
            for container in containers:
                if 'vtd' == container.name:
                    run = True
                    break
            if not run:
                break
        time.sleep(5)
        upload_simulation(parse_prefix, mp41, mp42)
    except Exception as e:
        print "生成仿真视频报错: %s" % str(e)
        # print(f'生成仿真视频报错{e}')


def is_upload_completed(bucket, prefix):
    target_number = str(prefix).split('_')[-1][:-1]
    count = 0
    for obj in oss2.ObjectIterator(bucket, prefix=prefix):
        if obj.key != prefix:
            count += 1
    return int(count) == int(target_number)


'''
cname：http://open-bucket.oss.icvdc.com
内网endpoint: oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com
oss桶名： open-bucket
keyid：n8glvFGS25MrLY7j
secret：xZ2Fozoarpfw0z28FUhtg8cu0yDc5d
'''
# ------- 获取合并之后的bag包，解析出csv -------
if __name__ == '__main__':
    # 1 创建阿里云对象
    auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
    # cname = 'http://open-bucket.oss.icvdc.com'
    # bucket = oss2.Bucket(auth, cname, 'open-bucket', is_cname=True)
    endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
    bucket = oss2.Bucket(auth, endpoint, 'open-bucket')
    while True:
        local_delete_list = []
        oss_delete_list = []
        prefix_list = []
        # 2 获取已经上传完成的所有目录并分组
        all_prefix_set = set()
        for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
            # 获取csv
            if 'data_parse' in str(obj1.key) and str(obj1.key).count('/') == 4:
                all_prefix_set.add('/'.join(str(obj1.key).split('/')[:-1]) + '/')
        for obj2_key in all_prefix_set:
            xosc1_done = False
            xosc2_done = False
            mp41_done = False
            mp42_done = False
            for obj3 in oss2.ObjectIterator(bucket, prefix=obj2_key):
                if '/scenario_orig.xosc' in str(obj3.key):
                    xosc1_done = True
                if '/scenario_hmi.xosc' in str(obj3.key):
                    xosc2_done = True
                if '/scenario_orig.mp4' in str(obj3.key):
                    mp41_done = True
                if '/scenario_hmi.mp4' in str(obj3.key):
                    mp42_done = True
            if not xosc1_done or not xosc2_done:
                continue
            if mp41_done and mp42_done:
                continue

            # print(f'需要生成xosc：{obj2_key}')
            print "需要生成仿真视频: %s" % obj2_key
            parse_prefix_full = obj2_key
            local_dir_full = path1 + parse_prefix_full
            if not os.path.exists(local_dir_full):
                os.makedirs(local_dir_full)
            # 下载两个csv
            root_path1 = local_dir_full + 'orig/'
            root_path2 = local_dir_full + 'hmi/'
            if not os.path.exists(root_path1):
                os.makedirs(root_path1)
            if not os.path.exists(root_path2):
                os.makedirs(root_path2)

            bucket.get_object_to_file(parse_prefix_full + 'scenario_orig.xosc', root_path1 + 'scenario_orig.xosc')
            bucket.get_object_to_file(parse_prefix_full + 'scenario_hmi.xosc', root_path2 + 'scenario_hmi.xosc')
            local_delete_list.append(root_path1 + 'scenario_orig.xosc')
            local_delete_list.append(root_path2 + 'scenario_hmi.xosc')
            move_xosc_before_simulation(root_path1, root_path2)
            simulation(parse_prefix_full, root_path1 + 'simulation.mp4', root_path2 + 'simulation.mp4')

        # 删除本地临时文件
        if len(local_delete_list) > 0:
            for local_delete in local_delete_list:
                try:
                    os.remove(local_delete)
                except Exception as e:
                    print "删除本地临时文件报错: %s" % str(e)
                    # print(f'删除本地临时文件报错{e}')

        time.sleep(sleep_time)
