# -*- coding: utf-8 -*-
import os
import time
import oss2
import json

from resource import pcdtovideo_pji_deepcamera

import logging

path1 = '/mnt/disk001/dcl_data_process/src/python2/pjibot/'
path2 = '/mnt/disk001/dcl_data_process/src/python2/pjibot/resource/'
logging.basicConfig(filename=path1 + 'log/pcd-pjibot_guide.log', level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s')

key1 = 'pjibot/'
sleep_time = 30  # 每多少秒扫描一次

error_bag_json = "/mnt/disk001/dcl_data_process/src/python2/pjibot/pcd-errorBag.json"
def parse_json_to_string_array(file_path):
    try:
        # 打开并读取JSON文件（Python 2中不支持encoding参数，需要使用codecs模块或处理文件读取后的编码）
        with open(file_path, 'r') as file:
            # 读取文件内容
            file_content = file.read()
            # 解析JSON内容（Python 2中json.loads用于解析字符串）
            data = json.loads(file_content.decode('utf-8'))  # 假设文件是UTF-8编码，这里需要手动解码

        # 检查数据是否是一个列表，并且列表中的元素是否是字符串
        if isinstance(data, list):
            for item in data:
                if not isinstance(item, basestring):  # Python 2中字符串类型包括str和unicode，用basestring检查
                    raise ValueError("JSON数组中的元素不是字符串")
            return data
        else:
            return []
    except Exception as e:
        return []


def list_to_json_file(data, file_path):
    """
    将列表转换为JSON格式并写入指定的文件路径。
    如果文件已存在，则覆盖它。

    参数:
    data (list): 要转换为JSON的列表。
    file_path (str): 要写入JSON数据的文件路径。
    """
    # 将列表转换为JSON格式的字符串，并确保输出为UTF-8编码的字符串
    json_data = json.dumps(data, ensure_ascii=False, indent=4)
    json_data_utf8 = json_data.encode('utf-8')  # 编码为UTF-8

    # 以写入模式打开文件，如果文件已存在则覆盖
    with open(file_path, 'w') as file:
        # 将UTF-8编码的JSON字符串写入文件
        file.write(json_data_utf8)


def parse_to_pcd_mp4(merged_bag_file_path, parse_prefix2, local_parse_dir, local_delete_list):
    global bucket
    try:
        # local_pcd_mp4_dir1 = pcdtovideo_pji_deepcamera.parse1('/ob_camera_01/depth/points', merged_bag_file_path,
        #                                                       local_parse_dir + '/depth1/')
        # local_pcd_mp4_dir2 = pcdtovideo_pji_deepcamera.parse2('/ob_camera_02/depth/points', merged_bag_file_path,
        #                                                       local_parse_dir + '/depth2/')

        os.chdir(path2)
        command1 = 'sh run.sh ' + merged_bag_file_path + ' ' + local_parse_dir
        logging.info("调用命令lidar视频生成命令: %s" % str(command1))
        os.system(command1)

        file_name_with_extension = os.path.basename(merged_bag_file_path)
        file_name_without_extension = os.path.splitext(file_name_with_extension)[0]
        parent_directory = os.path.dirname(merged_bag_file_path)
        local_pcd_mp4_dir3 = parent_directory.replace('data_merge',
                                                      'data_parse') + '/' + file_name_without_extension + '_pcd_lidar'
        # local_pcd_mp4_dir3 = pcdtovideo_pji_lidar.parse(merged_bag_file_path, local_parse_dir + '/lidar/')

        # pcd_mp4_file_name1 = 'pcd_depthcamera'
        # pcd_mp4_file_name2 = 'pcd_depthcamera2'
        pcd_mp4_file_name3 = 'pcd_lidar'
        # local_delete_list.append(local_pcd_mp4_dir1 + '/' + pcd_mp4_file_name1 + '.mp4')
        # local_delete_list.append(local_pcd_mp4_dir2 + '/' + pcd_mp4_file_name2 + '.mp4')
        local_delete_list.append(local_pcd_mp4_dir3 + '/output_video_converted.mp4')
        # oss_csv_object_key1 = parse_prefix2 + pcd_mp4_file_name1 + '.mp4'
        # oss_csv_object_key2 = parse_prefix2 + pcd_mp4_file_name2 + '.mp4'
        oss_csv_object_key3 = parse_prefix2 + pcd_mp4_file_name3 + '.mp4'
        # bucket.put_object_from_file(oss_csv_object_key1, local_pcd_mp4_dir1 + '/' + pcd_mp4_file_name1 + '.mp4')
        # logging.info("上传点云视频到: %s", oss_csv_object_key1)
        # bucket.put_object_from_file(oss_csv_object_key2, local_pcd_mp4_dir2 + '/' + pcd_mp4_file_name2 + '.mp4')
        # logging.info("上传点云视频到: %s", oss_csv_object_key2)
        bucket.put_object_from_file(oss_csv_object_key3, local_pcd_mp4_dir3 + '/output_video_converted.mp4')
        logging.info("上传点云视频到: %s", oss_csv_object_key3)
    except Exception as e:
        error_bag_list = parse_json_to_string_array(error_bag_json)
        error_bag_list.append(parse_prefix2)
        list_to_json_file(error_bag_list, error_bag_json)
        logging.exception("生成点云视频报错: %s" % str(e))


# ------- 获取合并之后的bag包，解析出csv -------
if __name__ == '__main__':
    # 1 创建阿里云对象
    auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
    endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
    bucket = oss2.Bucket(auth, endpoint, 'pji-bucket1')
    while True:
        logging.info("开始新一轮扫描")
        try:
            local_delete_list = []
            oss_delete_list = []
            prefix_list = []
            # 2 获取已经上传完成的所有目录并分组
            for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
                try:
                    # 获取合并后的包
                    merged_bag_object_key = str(obj1.key)
                    # print(f'判断1{merged_bag_object_key}')
                    if 'data_merge' in str(obj1.key) and str(obj1.key).endswith('.bag'):
                        merged_bag_object_key_split = merged_bag_object_key.split('/')
                        merged_prefix = '/'.join(merged_bag_object_key_split[:-1])
                        parse_prefix = merged_prefix.replace('data_merge', 'data_parse')
                        parse_prefix_full = merged_bag_object_key.replace('data_merge', 'data_parse')[:-4] + '/'
                        # pcd1_done = False
                        # pcd2_done = False
                        pcd3_done = False
                        for obj2 in oss2.ObjectIterator(bucket, prefix=parse_prefix_full):
                            # if '/pcd_depthcamera.mp4' in str(obj2.key):
                            #     pcd1_done = True
                            if '/pcd_lidar.mp4' in str(obj2.key):
                                pcd3_done = True
                        if pcd3_done:
                            # logging.info( "%s 存在点云视频 pcd_depthcamera.mp4 和 pcd_lidar.mp4" % str(parse_prefix_full))
                            # logging.info("%s 存在点云视频 pcd_lidar.mp4 " % str(parse_prefix_full))
                            continue
                        error_bag_list = parse_json_to_string_array(error_bag_json)
                        if parse_prefix_full in error_bag_list:
                            continue
                        logging.info("%s 缺少点云视频 pcd_depthcamera.mp4 和 pcd_lidar.mp4" % str(parse_prefix_full))
                        # print(f'需要解析{merged_bag_object_key}')
                        local_merged_bag_path = path1 + 'pcd/' + merged_bag_object_key
                        local_merged_dir = '/'.join(local_merged_bag_path.split('/')[:-1])
                        local_parse_dir = local_merged_dir.replace('data_merge', 'data_parse')
                        if not os.path.exists(local_merged_dir):
                            os.makedirs(local_merged_dir)
                        if not os.path.exists(local_parse_dir):
                            os.makedirs(local_parse_dir)
                        merged_bag_full_name = merged_bag_object_key_split[-1]
                        merged_bag_name = merged_bag_full_name.split('.')[0]
                        bucket.get_object_to_file(merged_bag_object_key, local_merged_bag_path)
                        local_delete_list.append(local_merged_bag_path)
                        # 2 生成 pos_orig.csv 和 pos_hmi.csv
                        parse_to_pcd_mp4(local_merged_bag_path, parse_prefix_full, local_parse_dir, local_delete_list)
                except Exception as e:
                    logging.exception("报错: %s" % str(e))
            # 删除本地临时文件
            if len(local_delete_list) > 0:
                for local_delete in local_delete_list:
                    try:
                        os.remove(local_delete)
                    except Exception as e:
                        logging.exception("删除本地临时文件报错: %s" % str(e))
            time.sleep(sleep_time)
        except Exception as e:
            logging.exception("全局异常处理: %s" % str(e))
